Add a --show-arguments flag to the container debug command

This commit is contained in:
Thomas Royer 2016-12-10 21:05:22 +01:00
parent 7aeb31e78d
commit 5c151d0439
10 changed files with 210 additions and 9 deletions

View File

@ -44,6 +44,7 @@ class ContainerDebugCommand extends ContainerAwareCommand
->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'),
@ -118,6 +119,7 @@ EOF
$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);

View File

@ -99,6 +99,7 @@ class JsonDescriptor extends Descriptor
{
$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) {
@ -108,7 +109,7 @@ class JsonDescriptor extends Descriptor
$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);
@ -208,7 +209,7 @@ class JsonDescriptor extends Descriptor
*
* @return array
*/
private function getContainerDefinitionData(Definition $definition, $omitTags = false)
private function getContainerDefinitionData(Definition $definition, $omitTags = false, $showArguments = false)
{
$data = array(
'class' => (string) $definition->getClass(),
@ -232,6 +233,23 @@ class JsonDescriptor extends Descriptor
}
}
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()) {

View File

@ -129,6 +129,7 @@ class MarkdownDescriptor extends Descriptor
$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) {
@ -149,7 +150,7 @@ class MarkdownDescriptor extends Descriptor
$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));
}
}
@ -195,6 +196,10 @@ class MarkdownDescriptor extends Descriptor
}
}
if (isset($options['show_arguments']) && $options['show_arguments']) {
$output .= "\n".'- Arguments: '.($definition->getArguments() ? 'yes' : 'no');
}
if ($definition->getFile()) {
$output .= "\n".'- File: `'.$definition->getFile().'`';
}

View File

@ -76,7 +76,7 @@ class XmlDescriptor extends Descriptor
*/
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']));
}
/**
@ -273,10 +273,11 @@ class XmlDescriptor extends Descriptor
* @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');
@ -286,7 +287,7 @@ class XmlDescriptor extends Descriptor
$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);
@ -300,10 +301,11 @@ class XmlDescriptor extends Descriptor
* @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'));
@ -317,7 +319,7 @@ class XmlDescriptor extends Descriptor
continue;
}
$serviceXML = $this->getContainerServiceDocument($service, $serviceId);
$serviceXML = $this->getContainerServiceDocument($service, $serviceId, null, $showArguments);
$containerXML->appendChild($containerXML->ownerDocument->importNode($serviceXML->childNodes->item(0), true));
}
@ -331,7 +333,7 @@ class XmlDescriptor extends Descriptor
*
* @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'));
@ -382,6 +384,12 @@ class XmlDescriptor extends Descriptor
}
}
if ($showArguments) {
foreach ($this->getArgumentNodes($definition->getArguments(), $dom) as $node) {
$serviceXML->appendChild($node);
}
}
if (!$omitTags) {
$tags = $definition->getTags();
@ -404,6 +412,43 @@ class XmlDescriptor extends Descriptor
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

View File

@ -197,6 +197,7 @@ abstract class AbstractDescriptorTest extends \PHPUnit_Framework_TestCase
'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();

View File

@ -105,6 +105,9 @@ class ObjectsProvider
->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)

View File

@ -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"
}
}

View File

@ -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`

View File

@ -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
------------------- --------------------------------------------------------

View File

@ -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>