Skip to content

Commit

Permalink
feature #20861 Add a --show-arguments flag to the debug:container com…
Browse files Browse the repository at this point in the history
…mand (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
  • Loading branch information
fabpot committed Jan 12, 2017
2 parents 7aeb31e + 5c151d0 commit a995383
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 9 deletions.
Expand Up @@ -44,6 +44,7 @@ protected function configure()
->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A service name (foo)'),
new InputOption('show-private', null, InputOption::VALUE_NONE, 'Used to show public *and* private services'),
new InputOption('show-arguments', null, InputOption::VALUE_NONE, 'Used to show arguments in services'),
new InputOption('tag', null, InputOption::VALUE_REQUIRED, 'Shows all services with a specific tag'),
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays tagged services for an application'),
new InputOption('parameter', null, InputOption::VALUE_REQUIRED, 'Displays a specific parameter for an application'),
Expand Down Expand Up @@ -118,6 +119,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$helper = new DescriptorHelper();
$options['format'] = $input->getOption('format');
$options['show_arguments'] = $input->getOption('show-arguments');
$options['raw_text'] = $input->getOption('raw');
$options['output'] = $io;
$helper->describe($output, $object, $options);
Expand Down
Expand Up @@ -99,6 +99,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
{
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$showPrivate = isset($options['show_private']) && $options['show_private'];
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$data = array('definitions' => array(), 'aliases' => array(), 'services' => array());

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
Expand All @@ -108,7 +109,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
} elseif ($service instanceof Definition) {
if (($showPrivate || $service->isPublic())) {
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service);
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, false, $showArguments);
}
} else {
$data['services'][$serviceId] = get_class($service);
Expand Down Expand Up @@ -208,7 +209,7 @@ protected function getRouteData(Route $route)
*
* @return array
*/
private function getContainerDefinitionData(Definition $definition, $omitTags = false)
private function getContainerDefinitionData(Definition $definition, $omitTags = false, $showArguments = false)
{
$data = array(
'class' => (string) $definition->getClass(),
Expand All @@ -232,6 +233,23 @@ private function getContainerDefinitionData(Definition $definition, $omitTags =
}
}

if ($showArguments) {
$data['arguments'] = array();

foreach ($definition->getArguments() as $argument) {
if ($argument instanceof Reference) {
$data['arguments'][] = array(
'type' => 'service',
'id' => (string) $argument,
);
} elseif ($argument instanceof Definition) {
$data['arguments'][] = $this->getContainerDefinitionData($argument, $omitTags, $showArguments);
} else {
$data['arguments'][] = $argument;
}
}
}

$data['file'] = $definition->getFile();

if ($factory = $definition->getFactory()) {
Expand Down
Expand Up @@ -129,6 +129,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o

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

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
Expand All @@ -149,7 +150,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$this->write("\n\nDefinitions\n-----------\n");
foreach ($services['definitions'] as $id => $service) {
$this->write("\n");
$this->describeContainerDefinition($service, array('id' => $id));
$this->describeContainerDefinition($service, array('id' => $id, 'show_arguments' => $showArguments));
}
}

Expand Down Expand Up @@ -195,6 +196,10 @@ protected function describeContainerDefinition(Definition $definition, array $op
}
}

if (isset($options['show_arguments']) && $options['show_arguments']) {
$output .= "\n".'- Arguments: '.($definition->getArguments() ? 'yes' : 'no');
}

if ($definition->getFile()) {
$output .= "\n".'- File: `'.$definition->getFile().'`';
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ protected function describeContainerService($service, array $options = array(),
*/
protected function describeContainerServices(ContainerBuilder $builder, array $options = array())
{
$this->writeDocument($this->getContainerServicesDocument($builder, isset($options['tag']) ? $options['tag'] : null, isset($options['show_private']) && $options['show_private']));
$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']));
}

/**
Expand Down Expand Up @@ -273,10 +273,11 @@ private function getContainerTagsDocument(ContainerBuilder $builder, $showPrivat
* @param mixed $service
* @param string $id
* @param ContainerBuilder|null $builder
* @param bool $showArguments
*
* @return \DOMDocument
*/
private function getContainerServiceDocument($service, $id, ContainerBuilder $builder = null)
private function getContainerServiceDocument($service, $id, ContainerBuilder $builder = null, $showArguments = false)
{
$dom = new \DOMDocument('1.0', 'UTF-8');

Expand All @@ -286,7 +287,7 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($builder->getDefinition((string) $service), (string) $service)->childNodes->item(0), true));
}
} elseif ($service instanceof Definition) {
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id)->childNodes->item(0), true));
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id, false, $showArguments)->childNodes->item(0), true));
} else {
$dom->appendChild($serviceXML = $dom->createElement('service'));
$serviceXML->setAttribute('id', $id);
Expand All @@ -300,10 +301,11 @@ private function getContainerServiceDocument($service, $id, ContainerBuilder $bu
* @param ContainerBuilder $builder
* @param string|null $tag
* @param bool $showPrivate
* @param bool $showArguments
*
* @return \DOMDocument
*/
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false)
private function getContainerServicesDocument(ContainerBuilder $builder, $tag = null, $showPrivate = false, $showArguments = false)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($containerXML = $dom->createElement('container'));
Expand All @@ -317,7 +319,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, $tag =
continue;
}

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

Expand All @@ -331,7 +333,7 @@ private function getContainerServicesDocument(ContainerBuilder $builder, $tag =
*
* @return \DOMDocument
*/
private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false)
private function getContainerDefinitionDocument(Definition $definition, $id = null, $omitTags = false, $showArguments = false)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($serviceXML = $dom->createElement('definition'));
Expand Down Expand Up @@ -382,6 +384,12 @@ private function getContainerDefinitionDocument(Definition $definition, $id = nu
}
}

if ($showArguments) {
foreach ($this->getArgumentNodes($definition->getArguments(), $dom) as $node) {
$serviceXML->appendChild($node);
}
}

if (!$omitTags) {
$tags = $definition->getTags();

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

/**
* @param array $arguments
*
* @return \DOMNode[]
*/
private function getArgumentNodes(array $arguments, \DOMDocument $dom)
{
$nodes = array();

foreach ($arguments as $argumentKey => $argument) {
$argumentXML = $dom->createElement('argument');

if (is_string($argumentKey)) {
$argumentXML->setAttribute('key', $argumentKey);
}

if ($argument instanceof Reference) {
$argumentXML->setAttribute('type', 'service');
$argumentXML->setAttribute('id', (string) $argument);
} elseif ($argument instanceof Definition) {
$argumentXML->appendChild($dom->importNode($this->getContainerDefinitionDocument($argument, null, false, true)->childNodes->item(0), true));
} elseif (is_array($argument)) {
$argumentXML->setAttribute('type', 'collection');

foreach ($this->getArgumentNodes($argument, $dom) as $childArgumenXML) {
$argumentXML->appendChild($childArgumenXML);
}
} else {
$argumentXML->appendChild(new \DOMText($argument));
}

$nodes[] = $argumentXML;
}

return $nodes;
}

/**
* @param Alias $alias
* @param string|null $id
Expand Down
Expand Up @@ -197,6 +197,7 @@ private function getContainerBuilderDescriptionTestData(array $objects)
'public' => array('show_private' => false),
'tag1' => array('show_private' => true, 'tag' => 'tag1'),
'tags' => array('group_by' => 'tags', 'show_private' => true),
'arguments' => array('show_private' => false, 'show_arguments' => true),
);

$data = array();
Expand Down
Expand Up @@ -105,6 +105,9 @@ public static function getContainerDefinitions()
->setSynthetic(false)
->setLazy(true)
->setAbstract(true)
->addArgument(new Reference('definition2'))
->addArgument('%parameter%')
->addArgument(new Definition('inline_service', array('arg1', 'arg2')))
->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
'definition_2' => $definition2
->setPublic(false)
Expand Down
@@ -0,0 +1,56 @@
{
"definitions": {
"definition_1": {
"class": "Full\\Qualified\\Class1",
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"abstract": true,
"file": null,
"factory_class": "Full\\Qualified\\FactoryClass",
"factory_method": "get",
"tags": [

],
"autowire": false,
"autowiring_types": [],
"arguments": [
{
"type": "service",
"id": "definition2"
},
"%parameter%",
{
"class": "inline_service",
"public": true,
"synthetic": false,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autowiring_types": [],
"arguments": [
"arg1",
"arg2"
],
"file": null,
"tags": []
}
]
}
},
"aliases": {
"alias_1": {
"service": "service_1",
"public": true
},
"alias_2": {
"service": "service_2",
"public": false
}
},
"services": {
"service_container": "Symfony\\Component\\DependencyInjection\\ContainerBuilder"
}
}
@@ -0,0 +1,41 @@
Public services
===============

Definitions
-----------

definition_1
~~~~~~~~~~~~
- Class: `Full\Qualified\Class1`
- Public: yes
- Synthetic: no
- Lazy: yes
- Shared: yes
- Abstract: yes
- Autowired: no
- Arguments: yes
- Factory Class: `Full\Qualified\FactoryClass`
- Factory Method: `get`
Aliases
-------
alias_1
~~~~~~~
- Service: `service_1`
- Public: yes
alias_2
~~~~~~~
- Service: `service_2`
- Public: no
Services
--------
- `service_container`: `Symfony\Component\DependencyInjection\ContainerBuilder`
@@ -0,0 +1,13 @@

Symfony Container Public Services
=================================

------------------- --------------------------------------------------------
 Service ID   Class name 
------------------- --------------------------------------------------------
alias_1 alias for "service_1"
alias_2 alias for "service_2"
definition_1 Full\Qualified\Class1
service_container Symfony\Component\DependencyInjection\ContainerBuilder
------------------- --------------------------------------------------------

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<container>
<alias id="alias_1" service="service_1" public="true"/>
<alias id="alias_2" service="service_2" public="false"/>
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="false" lazy="true" shared="true" abstract="true" autowired="false" file="">
<factory class="Full\Qualified\FactoryClass" method="get"/>
<argument type="service" id="definition2"/>
<argument>%parameter%</argument>
<argument>
<definition class="inline_service" public="true" synthetic="false" lazy="false" shared="true" abstract="false" autowired="false" file="">
<argument>arg1</argument>
<argument>arg2</argument>
</definition>
</argument>
</definition>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>
</container>

0 comments on commit a995383

Please sign in to comment.