Skip to content

Commit a318500

Browse files
Merge pull request #96 from php-http/fix_toolbar
Fix debug tool registration
2 parents bd4a71c + fcdf81f commit a318500

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

33

4+
## 1.2.2 - 2016-07-19
5+
6+
### Fixed
7+
8+
- Do not register debug tools when debugging is disabled (eg. in prod mode)
9+
10+
411
## 1.2.1 - 2016-07-19
512

613
### Fixed

DependencyInjection/HttplugExtension.php

+38-25
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public function load(array $configs, ContainerBuilder $container)
3939
$loader->load('services.xml');
4040
$loader->load('plugins.xml');
4141

42-
$enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
43-
if ($enabled) {
42+
$toolbar = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
43+
44+
if ($toolbar) {
4445
$loader->load('data-collector.xml');
45-
$config['_inject_collector_plugin'] = true;
4646

4747
if (!empty($config['toolbar']['formatter'])) {
4848
// Add custom formatter
@@ -66,17 +66,18 @@ public function load(array $configs, ContainerBuilder $container)
6666
}
6767

6868
$this->configurePlugins($container, $config['plugins']);
69-
$this->configureClients($container, $config);
70-
$this->configureAutoDiscoveryClients($container, $config);
69+
$this->configureClients($container, $config, $toolbar);
70+
$this->configureAutoDiscoveryClients($container, $config, $toolbar);
7171
}
7272

7373
/**
7474
* Configure client services.
7575
*
7676
* @param ContainerBuilder $container
7777
* @param array $config
78+
* @param bool $enableCollector
7879
*/
79-
private function configureClients(ContainerBuilder $container, array $config)
80+
private function configureClients(ContainerBuilder $container, array $config, $enableCollector)
8081
{
8182
// If we have a client named 'default'
8283
$first = isset($config['clients']['default']) ? 'default' : null;
@@ -87,7 +88,7 @@ private function configureClients(ContainerBuilder $container, array $config)
8788
$first = $name;
8889
}
8990

90-
$this->configureClient($container, $name, $arguments, $config['_inject_collector_plugin']);
91+
$this->configureClient($container, $name, $arguments, $enableCollector);
9192
}
9293

9394
// If we have clients configured
@@ -96,7 +97,7 @@ private function configureClients(ContainerBuilder $container, array $config)
9697
// Alias the first client to httplug.client.default
9798
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
9899
}
99-
} elseif (isset($config['_inject_collector_plugin'])) {
100+
} elseif ($enableCollector) {
100101
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'default');
101102
// No client was configured. Make sure to configure the auto discovery client with the PluginClient.
102103
$container->register('httplug.client', PluginClient::class)
@@ -218,14 +219,13 @@ private function configureClient(ContainerBuilder $container, $name, array $argu
218219
$serviceId = 'httplug.client.'.$name;
219220
$def = $container->register($serviceId, DummyClient::class);
220221

221-
// If there is no plugins nor should we use the data collector
222+
// If there are no plugins nor should we use the data collector
222223
if (empty($arguments['plugins']) && !$enableCollector) {
223224
$def->setFactory([new Reference($arguments['factory']), 'createClient'])
224225
->addArgument($arguments['config']);
225226
} else {
226-
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, $name);
227-
228-
$def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
227+
$def
228+
->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
229229
->addArgument(
230230
array_map(
231231
function ($id) {
@@ -236,13 +236,17 @@ function ($id) {
236236
)
237237
->addArgument(new Reference($arguments['factory']))
238238
->addArgument($arguments['config'])
239-
->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
239+
;
240240

241-
// tell the plugin journal what plugins we used
242-
$container->getDefinition('httplug.collector.plugin_journal')
243-
->addMethodCall('setPlugins', [$name, $arguments['plugins']]);
244-
}
241+
if ($enableCollector) {
242+
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, $name);
243+
$def->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
245244

245+
// tell the plugin journal what plugins we used
246+
$container->getDefinition('httplug.collector.plugin_journal')
247+
->addMethodCall('setPlugins', [$name, $arguments['plugins']]);
248+
}
249+
}
246250

247251
/*
248252
* Decorate the client with clients from client-common
@@ -287,15 +291,17 @@ private function registerDebugPlugin(ContainerBuilder $container, $name)
287291
*
288292
* @param ContainerBuilder $container
289293
* @param array $config
294+
* @param bool $enableCollector
290295
*/
291-
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
296+
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config, $enableCollector)
292297
{
293298
$httpClient = $config['discovery']['client'];
294299
if ($httpClient === 'auto') {
295300
$httpClient = $this->registerAutoDiscoverableClientWithDebugPlugin(
296301
$container,
297302
'client',
298-
[HttpClientDiscovery::class, 'find']
303+
[HttpClientDiscovery::class, 'find'],
304+
$enableCollector
299305
);
300306
} elseif ($httpClient) {
301307
$httpClient = new Reference($httpClient);
@@ -306,8 +312,9 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
306312
$asyncHttpClient = $this->registerAutoDiscoverableClientWithDebugPlugin(
307313
$container,
308314
'async_client',
309-
[HttpAsyncClientDiscovery::class, 'find']
310-
);
315+
[HttpAsyncClientDiscovery::class, 'find'],
316+
$enableCollector
317+
);
311318
} elseif ($asyncHttpClient) {
312319
$asyncHttpClient = new Reference($httpClient);
313320
}
@@ -321,21 +328,27 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
321328
* @param ContainerBuilder $container
322329
* @param string $name
323330
* @param callable $factory
331+
* @param bool $enableCollector
324332
*
325333
* @return Reference
326334
*/
327-
private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name, $factory)
335+
private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name, $factory, $enableCollector)
328336
{
329337
$definition = $container->register('httplug.auto_discovery_'.$name.'.pure', DummyClient::class);
330338
$definition->setPublic(false);
331339
$definition->setFactory($factory);
332340

333-
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name);
334-
$container->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class)
341+
$pluginDefinition = $container
342+
->register('httplug.auto_discovery_'.$name.'.plugin', PluginClient::class)
335343
->setPublic(false)
336344
->addArgument(new Reference('httplug.auto_discovery_'.$name.'.pure'))
337345
->addArgument([])
338-
->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
346+
;
347+
348+
if ($enableCollector) {
349+
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'auto_discovery_'.$name);
350+
$pluginDefinition->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]);
351+
}
339352

340353
return new Reference('httplug.auto_discovery_'.$name.'.plugin');
341354
}

0 commit comments

Comments
 (0)