Skip to content

Commit bc7aaa1

Browse files
committed
Merge Profiling back to main extension
Separating profiling into a separate class does not make the code cleaner or more readable. In this commit: - Profiling moved back to main extension - Toolbar (profiling) defaults to kernel.debug
1 parent a1aae48 commit bc7aaa1

File tree

9 files changed

+159
-138
lines changed

9 files changed

+159
-138
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Change Log
22

33

4+
## UNRELEASED
5+
6+
### Deprecated
7+
8+
- `auto` value in `toolbar.enabled` config
9+
10+
411
## 1.2.2 - 2016-07-19
512

613
### Fixed

DependencyInjection/Configuration.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@
1919
*/
2020
class Configuration implements ConfigurationInterface
2121
{
22+
/**
23+
* Whether to use the debug mode.
24+
*
25+
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
26+
*
27+
* @var bool
28+
*/
29+
private $debug;
30+
31+
/**
32+
* @param bool $debug
33+
*/
34+
public function __construct($debug)
35+
{
36+
$this->debug = (bool) $debug;
37+
}
38+
2239
/**
2340
* {@inheritdoc}
2441
*/
@@ -75,12 +92,17 @@ public function getConfigTreeBuilder()
7592
->end()
7693
->arrayNode('toolbar')
7794
->addDefaultsIfNotSet()
78-
->info('Extend the debug profiler with inforation about requests.')
95+
->info('Extend the debug profiler with information about requests.')
7996
->children()
80-
->enumNode('enabled')
81-
->info('If "auto" (default), the toolbar is activated when kernel.debug is true. You can force the toolbar on and off by changing this option.')
82-
->values([true, false, 'auto'])
83-
->defaultValue('auto')
97+
->booleanNode('enabled') // @deprecated value auto in 1.3.0
98+
->beforeNormalization()
99+
->ifString()
100+
->then(function ($v) {
101+
return 'auto' === $v ? $this->debug : $v;
102+
})
103+
->end()
104+
->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
105+
->defaultValue($this->debug)
84106
->end()
85107
->scalarNode('formatter')->defaultNull()->end()
86108
->scalarNode('captured_body_length')

DependencyInjection/HttplugExtension.php

Lines changed: 108 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Http\Discovery\HttpClientDiscovery;
1010
use Http\HttplugBundle\ClientFactory\DummyClient;
1111
use Http\HttplugBundle\ClientFactory\PluginClientFactory;
12+
use Http\HttplugBundle\Collector\DebugPlugin;
1213
use Http\Message\Authentication\BasicAuth;
1314
use Http\Message\Authentication\Bearer;
1415
use Http\Message\Authentication\Wsse;
@@ -38,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
3839
$loader->load('services.xml');
3940
$loader->load('plugins.xml');
4041

42+
// Register default services
4143
foreach ($config['classes'] as $service => $class) {
4244
if (!empty($class)) {
4345
$container->register(sprintf('httplug.%s.default', $service), $class);
@@ -49,27 +51,37 @@ public function load(array $configs, ContainerBuilder $container)
4951
$container->setAlias(sprintf('httplug.%s', $type), $id);
5052
}
5153

52-
$this->configurePlugins($container, $config['plugins']);
53-
$serviceIds = $this->configureClients($container, $config);
54-
$autoServiceIds = $this->configureAutoDiscoveryClients($container, $config);
54+
// Configure toolbar
55+
if ($config['toolbar']['enabled']) {
56+
$loader->load('data-collector.xml');
57+
58+
if (!empty($config['toolbar']['formatter'])) {
59+
// Add custom formatter
60+
$container
61+
->getDefinition('httplug.collector.debug_collector')
62+
->replaceArgument(0, new Reference($config['toolbar']['formatter']))
63+
;
64+
}
5565

56-
$toolbar = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
57-
if ($toolbar) {
58-
(new ProfilerExtension())->load($config, $container, array_unique(array_merge($serviceIds, $autoServiceIds)));
66+
$container
67+
->getDefinition('httplug.formatter.full_http_message')
68+
->addArgument($config['toolbar']['captured_body_length'])
69+
;
5970
}
71+
72+
$this->configurePlugins($container, $config['plugins']);
73+
$this->configureClients($container, $config);
74+
$this->configureAutoDiscoveryClients($container, $config);
6075
}
6176

6277
/**
6378
* Configure client services.
6479
*
6580
* @param ContainerBuilder $container
6681
* @param array $config
67-
*
68-
* @return array with client service names
6982
*/
7083
private function configureClients(ContainerBuilder $container, array $config)
7184
{
72-
$serviceIds = [];
7385
$first = null;
7486

7587
foreach ($config['clients'] as $name => $arguments) {
@@ -78,7 +90,7 @@ private function configureClients(ContainerBuilder $container, array $config)
7890
$first = $name;
7991
}
8092

81-
$serviceIds[] = $this->configureClient($container, $name, $arguments);
93+
$this->configureClient($container, $name, $arguments, $config['toolbar']['enabled']);
8294
}
8395

8496
// If we have clients configured
@@ -89,8 +101,6 @@ private function configureClients(ContainerBuilder $container, array $config)
89101
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
90102
}
91103
}
92-
93-
return $serviceIds;
94104
}
95105

96106
/**
@@ -198,14 +208,29 @@ private function configureAuthentication(ContainerBuilder $container, array $con
198208
* @param ContainerBuilder $container
199209
* @param string $name
200210
* @param array $arguments
201-
*
202-
* @return string The service id of the client.
211+
* @param bool $profiling
203212
*/
204-
private function configureClient(ContainerBuilder $container, $name, array $arguments)
213+
private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling)
205214
{
206215
$serviceId = 'httplug.client.'.$name;
207-
$definition = $container->register($serviceId, DummyClient::class);
208-
$definition->setFactory([PluginClientFactory::class, 'createPluginClient'])
216+
217+
$pluginClientOptions = [];
218+
219+
if ($profiling) {
220+
// Tell the plugin journal what plugins we used
221+
$container
222+
->getDefinition('httplug.collector.plugin_journal')
223+
->addMethodCall('setPlugins', [$name, $arguments['plugins']])
224+
;
225+
226+
$debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId);
227+
228+
$pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)];
229+
}
230+
231+
$container
232+
->register($serviceId, DummyClient::class)
233+
->setFactory([PluginClientFactory::class, 'createPluginClient'])
209234
->addArgument(
210235
array_map(
211236
function ($id) {
@@ -216,31 +241,30 @@ function ($id) {
216241
)
217242
->addArgument(new Reference($arguments['factory']))
218243
->addArgument($arguments['config'])
219-
->addArgument([])
244+
->addArgument($pluginClientOptions)
220245
;
221246

222-
// Tell the plugin journal what plugins we used
223-
$container->getDefinition('httplug.collector.plugin_journal')
224-
->addMethodCall('setPlugins', [$name, $arguments['plugins']]);
225247

226248
/*
227249
* Decorate the client with clients from client-common
228250
*/
229251
if ($arguments['flexible_client']) {
230-
$container->register($serviceId.'.flexible', FlexibleHttpClient::class)
252+
$container
253+
->register($serviceId.'.flexible', FlexibleHttpClient::class)
231254
->addArgument(new Reference($serviceId.'.flexible.inner'))
232255
->setPublic(false)
233-
->setDecoratedService($serviceId);
256+
->setDecoratedService($serviceId)
257+
;
234258
}
235259

236260
if ($arguments['http_methods_client']) {
237-
$container->register($serviceId.'.http_methods', HttpMethodsClient::class)
261+
$container
262+
->register($serviceId.'.http_methods', HttpMethodsClient::class)
238263
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')])
239264
->setPublic(false)
240-
->setDecoratedService($serviceId);
265+
->setDecoratedService($serviceId)
266+
;
241267
}
242-
243-
return $serviceId;
244268
}
245269

246270
/**
@@ -249,45 +273,44 @@ function ($id) {
249273
*
250274
* @param ContainerBuilder $container
251275
* @param array $config
252-
*
253-
* @return array of service ids.
254276
*/
255277
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
256278
{
257-
$serviceIds = [];
258-
259279
$httpClient = $config['discovery']['client'];
280+
260281
if (!empty($httpClient)) {
261282
if ($httpClient === 'auto') {
262283
$httpClient = $this->registerAutoDiscoverableClient(
263284
$container,
264285
'auto_discovered_client',
265-
[HttpClientDiscovery::class, 'find']
286+
[HttpClientDiscovery::class, 'find'],
287+
$config['toolbar']['enabled']
266288
);
267289
}
268290

269-
$serviceIds[] = $httpClient;
270291
$httpClient = new Reference($httpClient);
271292
}
272293

273294
$asyncHttpClient = $config['discovery']['async_client'];
295+
274296
if (!empty($asyncHttpClient)) {
275297
if ($asyncHttpClient === 'auto') {
276298
$asyncHttpClient = $this->registerAutoDiscoverableClient(
277299
$container,
278300
'auto_discovered_async',
279-
[HttpAsyncClientDiscovery::class, 'find']
301+
[HttpAsyncClientDiscovery::class, 'find'],
302+
$config['toolbar']['enabled']
280303
);
281304
}
282-
$serviceIds[] = $asyncHttpClient;
283-
$asyncHttpClient = new Reference($httpClient);
305+
306+
$asyncHttpClient = new Reference($asyncHttpClient);
284307
}
285308

286-
$container->getDefinition('httplug.strategy')
309+
$container
310+
->getDefinition('httplug.strategy')
287311
->addArgument($httpClient)
288-
->addArgument($asyncHttpClient);
289-
290-
return $serviceIds;
312+
->addArgument($asyncHttpClient)
313+
;
291314
}
292315

293316
/**
@@ -296,17 +319,58 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
296319
* @param ContainerBuilder $container
297320
* @param string $name
298321
* @param callable $factory
322+
* @param bool $profiling
299323
*
300324
* @return string service id
301325
*/
302-
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory)
326+
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling)
303327
{
304328
$serviceId = 'httplug.auto_discovery.'.$name;
305-
$definition = $container->register($serviceId, DummyClient::class);
306-
$definition
329+
330+
$pluginClientOptions = [];
331+
332+
if ($profiling) {
333+
$debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId);
334+
335+
$pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)];
336+
}
337+
338+
$container
339+
->register($serviceId, DummyClient::class)
307340
->setFactory([PluginClientFactory::class, 'createPluginClient'])
308-
->setArguments([[], $factory, [], []]);
341+
->setArguments([[], $factory, [], $pluginClientOptions])
342+
;
309343

310344
return $serviceId;
311345
}
346+
347+
/**
348+
* Create a new plugin service for this client.
349+
*
350+
* @param ContainerBuilder $container
351+
* @param string $name
352+
*
353+
* @return string
354+
*/
355+
private function registerDebugPlugin(ContainerBuilder $container, $name)
356+
{
357+
$serviceIdDebugPlugin = $name.'.debug_plugin';
358+
359+
$container
360+
->register($serviceIdDebugPlugin, DebugPlugin::class)
361+
->addArgument(new Reference('httplug.collector.debug_collector'))
362+
->addArgument(substr($name, strrpos($name, '.') + 1))
363+
->setPublic(false)
364+
;
365+
366+
return $serviceIdDebugPlugin;
367+
}
368+
369+
/**
370+
* {@inheritdoc}
371+
*/
372+
public function getConfiguration(array $config, ContainerBuilder $container)
373+
{
374+
return new Configuration($container->getParameter('kernel.debug'));
375+
}
312376
}

0 commit comments

Comments
 (0)