Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a995383

Browse files
committed
feature #20861 Add a --show-arguments flag to the debug:container command (Cydonia7)
This PR was merged into the 3.3-dev branch. Discussion ---------- Add a --show-arguments flag to the debug:container command | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | not yet This PR adds a `--show-arguments` flag to the `debug:container` command that shows arguments in the services in the different available formats. (Ping @dunglas) Commits ------- 5c151d0 Add a --show-arguments flag to the container debug command
2 parents 7aeb31e + 5c151d0 commit a995383

File tree

10 files changed

+210
-9
lines changed

10 files changed

+210
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected function configure()
4444
->setDefinition(array(
4545
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
4646
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services'),
47+
new InputOption('show-arguments', null, InputOption::VALUE_NONE, 'Used to show arguments in services'),
4748
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Shows all services with a specific tag'),
4849
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
4950
new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
@@ -118,6 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
118119

119120
$helper = new DescriptorHelper();
120121
$options['format'] = $input->getOption('format');
122+
$options['show_arguments'] = $input->getOption('show-arguments');
121123
$options['raw_text'] = $input->getOption('raw');
122124
$options['output'] = $io;
123125
$helper->describe($output, $object, $options);

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
9999
{
100100
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
101101
$showPrivate = isset($options['show_private']) && $options['show_private'];
102+
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
102103
$data = array('definitions' => array(), 'aliases' => array(), 'services' => array());
103104

104105
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
@@ -108,7 +109,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
108109
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
109110
} elseif ($service instanceof Definition) {
110111
if (($showPrivate || $service->isPublic())) {
111-
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service);
112+
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, false, $showArguments);
112113
}
113114
} else {
114115
$data['services'][$serviceId] = get_class($service);
@@ -208,7 +209,7 @@ protected function getRouteData(Route $route)
208209
*
209210
* @return array
210211
*/
211-
private function getContainerDefinitionData(Definition $definition, $omitTags = false)
212+
private function getContainerDefinitionData(Definition $definition, $omitTags = false, $showArguments = false)
212213
{
213214
$data = array(
214215
'class' => (string) $definition->getClass(),
@@ -232,6 +233,23 @@ private function getContainerDefinitionData(Definition $definition, $omitTags =
232233
}
233234
}
234235

236+
if ($showArguments) {
237+
$data['arguments'] = array();
238+
239+
foreach ($definition->getArguments() as $argument) {
240+
if ($argument instanceof Reference) {
241+
$data['arguments'][] = array(
242+
'type' => 'service',
243+
'id' => (string) $argument,
244+
);
245+
} elseif ($argument instanceof Definition) {
246+
$data['arguments'][] = $this->getContainerDefinitionData($argument, $omitTags, $showArguments);
247+
} else {
248+
$data['arguments'][] = $argument;
249+
}
250+
}
251+
}
252+
235253
$data['file'] = $definition->getFile();
236254

237255
if ($factory = $definition->getFactory()) {

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
129129

130130
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
131131
$showPrivate = isset($options['show_private']) && $options['show_private'];
132+
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
132133
$services = array('definitions' => array(), 'aliases' => array(), 'services' => array());
133134

134135
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
@@ -149,7 +150,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
149150
$this->write("\n\nDefinitions\n-----------\n");
150151
foreach ($services['definitions'] as $id => $service) {
151152
$this->write("\n");
152-
$this->describeContainerDefinition($service, array('id' => $id));
153+
$this->describeContainerDefinition($service, array('id' => $id, 'show_arguments' => $showArguments));
153154
}
154155
}
155156

@@ -195,6 +196,10 @@ protected function describeContainerDefinition(Definition $definition, array $op
195196
}
196197
}
197198

199+
if (isset($options['show_arguments']) && $options['show_arguments']) {
200+
$output .= "\n".'- Arguments: '.($definition->getArguments() ? 'yes' : 'no');
201+
}
202+
198203
if ($definition->getFile()) {
199204
$output .= "\n".'- File: `'.$definition->getFile().'`';
200205
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function describeContainerService($service, array $options = array(),
7676
*/
7777
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
7878
{
79-
$this->writeDocument($this->getContainerServicesDocument($builder, isset($options['tag']) ? $options['tag'] : null, isset($options['show_private']) && $options['show_private']));
79+
$this->writeDocument($this->getContainerServicesDocument($builder, isset($options['tag']) ? $options['tag'] : null, isset($options['show_private']) && $options['show_private'], isset($options['show_arguments']) && $options['show_arguments']));
8080
}
8181

8282
/**
@@ -273,10 +273,11 @@ private function getContainerTagsDocument(ContainerBuilder $builder, $showPrivat
273273
* @param mixed $service
274274
* @param string $id
275275
* @param ContainerBuilder|null $builder
276+
* @param bool $showArguments
276277
*
277278
* @return \DOMDocument
278279
*/
279-
private function getContainerServiceDocument($service, $id, ContainerBuilder $builder = null)
280+
private function getContainerServiceDocument($service, $id, ContainerBuilder $builder = null, $showArguments = false)
280281
{
281282
$dom = new \DOMDocument('1.0', 'UTF-8');
282283

@@ -286,7 +287,7 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
286287
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($builder->getDefinition((string) $service), (string) $service)->childNodes->item(0), true));
287288
}
288289
} elseif ($service instanceof Definition) {
289-
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id)->childNodes->item(0), true));
290+
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id, false, $showArguments)->childNodes->item(0), true));
290291
} else {
291292
$dom->appendChild($serviceXML = $dom->createElement('service'));
292293
$serviceXML->setAttribute('id', $id);
@@ -300,10 +301,11 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
300301
* @param ContainerBuilder $builder
301302
* @param string|null $tag
302303
* @param bool $showPrivate
304+
* @param bool $showArguments
303305
*
304306
* @return \DOMDocument
305307
*/
306-
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false)
308+
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false)
307309
{
308310
$dom = new \DOMDocument('1.0', 'UTF-8');
309311
$dom->appendChild($containerXML = $dom->createElement('container'));
@@ -317,7 +319,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, $tag =
317319
continue;
318320
}
319321

320-
$serviceXML = $this->getContainerServiceDocument($service, $serviceId);
322+
$serviceXML = $this->getContainerServiceDocument($service, $serviceId, null, $showArguments);
321323
$containerXML->appendChild($containerXML->ownerDocument->importNode($serviceXML->childNodes->item(0), true));
322324
}
323325

@@ -331,7 +333,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, $tag =
331333
*
332334
* @return \DOMDocument
333335
*/
334-
private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false)
336+
private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false, $showArguments = false)
335337
{
336338
$dom = new \DOMDocument('1.0', 'UTF-8');
337339
$dom->appendChild($serviceXML = $dom->createElement('definition'));
@@ -382,6 +384,12 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
382384
}
383385
}
384386

387+
if ($showArguments) {
388+
foreach ($this->getArgumentNodes($definition->getArguments(), $dom) as $node) {
389+
$serviceXML->appendChild($node);
390+
}
391+
}
392+
385393
if (!$omitTags) {
386394
$tags = $definition->getTags();
387395

@@ -404,6 +412,43 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
404412
return $dom;
405413
}
406414

415+
/**
416+
* @param array $arguments
417+
*
418+
* @return \DOMNode[]
419+
*/
420+
private function getArgumentNodes(array $arguments, \DOMDocument $dom)
421+
{
422+
$nodes = array();
423+
424+
foreach ($arguments as $argumentKey => $argument) {
425+
$argumentXML = $dom->createElement('argument');
426+
427+
if (is_string($argumentKey)) {
428+
$argumentXML->setAttribute('key', $argumentKey);
429+
}
430+
431+
if ($argument instanceof Reference) {
432+
$argumentXML->setAttribute('type', 'service');
433+
$argumentXML->setAttribute('id', (string) $argument);
434+
} elseif ($argument instanceof Definition) {
435+
$argumentXML->appendChild($dom->importNode($this->getContainerDefinitionDocument($argument, null, false, true)->childNodes->item(0), true));
436+
} elseif (is_array($argument)) {
437+
$argumentXML->setAttribute('type', 'collection');
438+
439+
foreach ($this->getArgumentNodes($argument, $dom) as $childArgumenXML) {
440+
$argumentXML->appendChild($childArgumenXML);
441+
}
442+
} else {
443+
$argumentXML->appendChild(new \DOMText($argument));
444+
}
445+
446+
$nodes[] = $argumentXML;
447+
}
448+
449+
return $nodes;
450+
}
451+
407452
/**
408453
* @param Alias $alias
409454
* @param string|null $id

src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/AbstractDescriptorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ private function getContainerBuilderDescriptionTestData(array $objects)
197197
'public' => array('show_private' => false),
198198
'tag1' => array('show_private' => true, 'tag' => 'tag1'),
199199
'tags' => array('group_by' => 'tags', 'show_private' => true),
200+
'arguments' => array('show_private' => false, 'show_arguments' => true),
200201
);
201202

202203
$data = array();

src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public static function getContainerDefinitions()
105105
->setSynthetic(false)
106106
->setLazy(true)
107107
->setAbstract(true)
108+
->addArgument(new Reference('definition2'))
109+
->addArgument('%parameter%')
110+
->addArgument(new Definition('inline_service', array('arg1', 'arg2')))
108111
->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
109112
'definition_2' => $definition2
110113
->setPublic(false)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"definitions": {
3+
"definition_1": {
4+
"class": "Full\\Qualified\\Class1",
5+
"public": true,
6+
"synthetic": false,
7+
"lazy": true,
8+
"shared": true,
9+
"abstract": true,
10+
"file": null,
11+
"factory_class": "Full\\Qualified\\FactoryClass",
12+
"factory_method": "get",
13+
"tags": [
14+
15+
],
16+
"autowire": false,
17+
"autowiring_types": [],
18+
"arguments": [
19+
{
20+
"type": "service",
21+
"id": "definition2"
22+
},
23+
"%parameter%",
24+
{
25+
"class": "inline_service",
26+
"public": true,
27+
"synthetic": false,
28+
"lazy": false,
29+
"shared": true,
30+
"abstract": false,
31+
"autowire": false,
32+
"autowiring_types": [],
33+
"arguments": [
34+
"arg1",
35+
"arg2"
36+
],
37+
"file": null,
38+
"tags": []
39+
}
40+
]
41+
}
42+
},
43+
"aliases": {
44+
"alias_1": {
45+
"service": "service_1",
46+
"public": true
47+
},
48+
"alias_2": {
49+
"service": "service_2",
50+
"public": false
51+
}
52+
},
53+
"services": {
54+
"service_container": "Symfony\\Component\\DependencyInjection\\ContainerBuilder"
55+
}
56+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Public services
2+
===============
3+
4+
Definitions
5+
-----------
6+
7+
definition_1
8+
~~~~~~~~~~~~
9+
10+
- Class: `Full\Qualified\Class1`
11+
- Public: yes
12+
- Synthetic: no
13+
- Lazy: yes
14+
- Shared: yes
15+
- Abstract: yes
16+
- Autowired: no
17+
- Arguments: yes
18+
- Factory Class: `Full\Qualified\FactoryClass`
19+
- Factory Method: `get`
20+
21+
22+
Aliases
23+
-------
24+
25+
alias_1
26+
~~~~~~~
27+
28+
- Service: `service_1`
29+
- Public: yes
30+
31+
alias_2
32+
~~~~~~~
33+
34+
- Service: `service_2`
35+
- Public: no
36+
37+
38+
Services
39+
--------
40+
41+
- `service_container`: `Symfony\Component\DependencyInjection\ContainerBuilder`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
Symfony Container Public Services
3+
=================================
4+
5+
------------------- --------------------------------------------------------
6+
 Service ID   Class name 
7+
------------------- --------------------------------------------------------
8+
alias_1 alias for "service_1"
9+
alias_2 alias for "service_2"
10+
definition_1 Full\Qualified\Class1
11+
service_container Symfony\Component\DependencyInjection\ContainerBuilder
12+
------------------- --------------------------------------------------------
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container>
3+
<alias id="alias_1" service="service_1" public="true"/>
4+
<alias id="alias_2" service="service_2" public="false"/>
5+
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="false" lazy="true" shared="true" abstract="true" autowired="false" file="">
6+
<factory class="Full\Qualified\FactoryClass" method="get"/>
7+
<argument type="service" id="definition2"/>
8+
<argument>%parameter%</argument>
9+
<argument>
10+
<definition class="inline_service" public="true" synthetic="false" lazy="false" shared="true" abstract="false" autowired="false" file="">
11+
<argument>arg1</argument>
12+
<argument>arg2</argument>
13+
</definition>
14+
</argument>
15+
</definition>
16+
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>
17+
</container>

0 commit comments

Comments
 (0)