Skip to content

Add a --show-arguments flag to the debug:container command #20861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 {
Copy link
Member

@chalasr chalasr Jan 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed out, there should be something like:

} elseif ($argument instanceof Definition) {
    $argumentXML->setAttribute('type', 'service');
}

$argumentXML->appendChild(new \DOMText($argument));
}

$nodes[] = $argumentXML;
}

return $nodes;
}

/**
* @param Alias $alias
* @param string|null $id
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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
------------------- --------------------------------------------------------

Original file line number Diff line number Diff line change
@@ -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>