1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Http\HttplugBundle\Collector\DebugPlugin; |
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class is responsible for loading profiler tools and the toolbar. This extension should generally not be used |
13
|
|
|
* in production. |
14
|
|
|
* |
15
|
|
|
* @author David Buchmann <[email protected]> |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ProfilerExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param array $config Processed configuration |
22
|
|
|
* @param ContainerBuilder $container |
23
|
|
|
* @param array $clientServiceIds ids of all clients |
24
|
|
|
*/ |
25
|
|
|
public function load(array $config, ContainerBuilder $container, array $clientServiceIds) |
26
|
|
|
{ |
27
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
28
|
|
|
$loader->load('data-collector.xml'); |
29
|
|
|
|
30
|
|
|
if (!empty($config['toolbar']['formatter'])) { |
31
|
|
|
// Add custom formatter |
32
|
|
|
$container->getDefinition('httplug.collector.debug_collector') |
33
|
|
|
->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$container->getDefinition('httplug.formatter.full_http_message') |
37
|
|
|
->addArgument($config['toolbar']['captured_body_length']); |
38
|
|
|
|
39
|
|
|
foreach ($clientServiceIds as $clientId) { |
40
|
|
|
$pluginClientDefinition = $container->getDefinition($clientId); |
41
|
|
|
$serviceIdDebugPlugin = $this->registerDebugPlugin($container, $clientId); |
42
|
|
|
|
43
|
|
|
$argument = $this->mergeDebugPluginArguments($pluginClientDefinition->getArgument(3), [new Reference($serviceIdDebugPlugin)]); |
44
|
|
|
$pluginClientDefinition->replaceArgument(3, $argument); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new plugin service for this client. |
50
|
|
|
* |
51
|
|
|
* @param ContainerBuilder $container |
52
|
|
|
* @param string $name |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
private function registerDebugPlugin(ContainerBuilder $container, $name) |
57
|
|
|
{ |
58
|
|
|
$serviceIdDebugPlugin = $name.'.debug_plugin'; |
59
|
|
|
$container->register($serviceIdDebugPlugin, DebugPlugin::class) |
60
|
|
|
->addArgument(new Reference('httplug.collector.debug_collector')) |
61
|
|
|
->addArgument(substr($name, strrpos($name, '.') + 1)) |
62
|
|
|
->setPublic(false); |
63
|
|
|
|
64
|
|
|
return $serviceIdDebugPlugin; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $existing |
69
|
|
|
* @param array $newArgument |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
private function mergeDebugPluginArguments($existing, $newArgument) |
74
|
|
|
{ |
75
|
|
|
if (empty($existing)) { |
76
|
|
|
$mergedArgument = ['debug_plugins' => $newArgument]; |
77
|
|
|
} elseif (empty($existing['debug_plugins'])) { |
78
|
|
|
$mergedArgument['debug_plugins'] = $newArgument; |
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
$mergedArgument['debug_plugins'] = array_merge($existing['debug_plugins'], $newArgument); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $mergedArgument; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.