8000 [FrameworkBundle] Display original definition for aliases in debug:container by chalasr · Pull Request #21129 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Display original definition for aliases in debug:container #21129

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 4, 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 @@ -54,7 +54,7 @@ public function describe(OutputInterface $output, $object, array $options = arra
$this->describeContainerTags($object, $options);
break;
case $object instanceof ContainerBuilder && isset($options['id']):
$this->describeContainerService($this->resolveServiceDefinition($object, $options['id']), $options);
$this->describeContainerService($this->resolveServiceDefinition($object, $options['id']), $options, $object);
break;
case $object instanceof ContainerBuilder && isset($options['parameter']):
$this->describeContainerParameter($object->resolveEnvPlaceholders($object->getParameter($options['parameter'])), $options);
Expand Down Expand Up @@ -140,8 +140,9 @@ abstract protected function describeContainerTags(ContainerBuilder $builder, arr
*
* @param Definition|Alias|object $service
* @param array $options
Copy link
Member

Choose a reason for hiding this comment

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

The PHPDoc must be updated (new arg)

* @param ContainerBuilder|null $builder
*/
abstract protected function describeContainerService($service, array $options = array());
abstract protected function describeContainerService($service, array $options = array(), ContainerBuilder $builder = null);

/**
* Describes container services.
Expand All @@ -165,10 +166,11 @@ abstract protected function describeContainerDefinition(Definition $definition,
/**
* Describes a service alias.
*
* @param Alias $alias
* @param array $options
* @param Alias $alias
* @param array $options
* @param ContainerBuilder|null $builder
*/
abstract protected function describeContainerAlias(Alias $alias, array $options = array());
abstract protected function describeContainerAlias(Alias $alias, array $options = array(), ContainerBuilder $builder = null);

/**
* Describes a container parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
/**
* {@inheritdoc}
*/
protected function describeContainerService($service, array $options = array())
protected function describeContainerService($service, array $options = array(), ContainerBuilder $builder = null)
{
if (!isset($options['id'])) {
throw new \InvalidArgumentException('An "id" option must be provided.');
}

if ($service instanceof Alias) {
$this->writeData($this->getContainerAliasData($service), $options);
$this->describeContainerAlias($service, $options, $builder);
} elseif ($service instanceof Definition) {
$this->writeData($this->getContainerDefinitionData($service), $options);
} else {
Expand Down Expand Up @@ -129,9 +129,16 @@ protected function describeContainerDefinition(Definition $definition, array $op
/**
* {@inheritdoc}
*/
protected function describeContainerAlias(Alias $alias, array $options = array())
protected function describeContainerAlias(Alias $alias, array $options = array(), ContainerBuilder $builder = null)
{
$this->writeData($this->getContainerAliasData($alias), $options);
if (!$builder) {
return $this->writeData($this->getContainerAliasData($alias), $options);
}

$this->writeData(
array($this->getContainerAliasData($alias), $this->getContainerDefinitionData($builder->getDefinition((string) $alias))),
array_merge($options, array('id' => (string) $alias))
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
/**
* {@inheritdoc}
*/
protected function describeContainerService($service, array $options = array())
protected function describeContainerService($service, array $options = array(), ContainerBuilder $builder = null)
{
if (!isset($options['id'])) {
throw new \InvalidArgumentException('An "id" option must be provided.');
Expand All @@ -106,7 +106,7 @@ protected function describeContainerService($service, array $options = array())
$childOptions = array('id' => $options['id'], 'as_array' => true);

if ($service instanceof Alias) {
$this->describeContainerAlias($service, $childOptions);
$this->describeContainerAlias($service, $childOptions, $builder);
} elseif ($service instanceof Definition) {
$this->describeContainerDefinition($service, $childOptions);
} else {
Expand Down Expand Up @@ -236,12 +236,23 @@ protected function describeContainerDefinition(Definition $definition, array $op
/**
* {@inheritdoc}
*/
protected function describeContainerAlias(Alias $alias, array $options = array())
protected function describeContainerAlias(Alias $alias, array $options = array(), ContainerBuilder $builder = null)
{
$output = '- Service: `'.$alias.'`'
."\n".'- Public: '.($alias->isPublic() ? 'yes' : 'no');

$this->write(isset($options['id']) ? sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output) : $output);
if (!isset($options['id'])) {
return $this->write($output);
}

$this->write(sprintf("%s\n%s\n\n%s\n", $options['id'], str_repeat('~', strlen($options['id'])), $output));

if (!$builder) {
return;
}

$this->write("\n");
$this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, array('id' => (string) $alias)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
/**
* {@inheritdoc}
*/
protected function describeContainerService($service, array $options = array())
protected function describeContainerService($service, array $options = array(), ContainerBuilder $builder = null)
{
if (!isset($options['id'])) {
throw new \InvalidArgumentException('An "id" option must be provided.');
}

if ($service instanceof Alias) {
$this->describeContainerAlias($service, $options);
$this->describeContainerAlias($service, $options, $builder);
} elseif ($service instanceof Definition) {
$this->describeContainerDefinition($service, $options);
} else {
Expand Down Expand Up @@ -333,9 +333,15 @@ protected function describeContainerDefinition(Definition $definition, array $op
/**
* {@inheritdoc}
*/
protected function describeContainerAlias(Alias $alias, array $options = array())
protected function describeContainerAlias(Alias $alias, array $options = array(), ContainerBuilder $builder = null)
{
$options['output']->comment(sprintf('This service is an alias for the service <info>%s</info>', (string) $alias));

if (!$builder) {
return;
}

return $this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, array('id' => (string) $alias)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
/**
* {@inheritdoc}
*/
protected function describeContainerService($service, array $options = array())
protected function describeContainerService($service, array $options = array(), ContainerBuilder $builder = null)
{
if (!isset($options['id'])) {
throw new \InvalidArgumentException('An "id" option must be provided.');
}

$this->writeDocument($this->getContainerServiceDocument($service, $options['id']));
$this->writeDocument($this->getContainerServiceDocument($service, $options['id'], $builder));
}

/**
Expand All @@ -90,9 +90,18 @@ protected function describeContainerDefinition(Definition $definition, array $op
/**
* {@inheritdoc}
*/
protected function describeContainerAlias(Alias $alias, array $options = array())
protected function describeContainerAlias(Alias $alias, array $options = array(), ContainerBuilder $builder = null)
{
$this->writeDocument($this->getContainerAliasDocument($alias, isset($options['id']) ? $options['id'] : null));
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($dom->importNode($this->getContainerAliasDocument($alias, isset($options['id']) ? $options['id'] : null)->childNodes->item(0), true));

if (!$builder) {
return $this->writeDocument($dom);
}

$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($builder->getDefinition((string) $alias), (string) $alias)->childNodes->item(0), true));

$this->writeDocument($dom);
}

/**
Expand Down Expand Up @@ -261,17 +270,21 @@ private function getContainerTagsDocument(ContainerBuilder $builder, $showPrivat
}

/**
* @param mixed $service
* @param string $id
* @param mixed $service
* @param string $id
* @param ContainerBuilder|null $builder
*
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

* @return \DOMDocument
*/
private function getContainerServiceDocument($service, $id)
private function getContainerServiceDocument($service, $id, ContainerBuilder $builder = null)
{
$dom = new \DOMDocument('1.0', 'UTF-8');

if ($service instanceof Alias) {
$dom->appendChild($dom->importNode($this->getContainerAliasDocument($service, $id)->childNodes->item(0), true));
if ($builder) {
$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));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ public function getDescribeContainerAliasTestData()
return $this->getDescriptionTestData(ObjectsProvider::getContainerAliases());
}

/** @dataProvider getDescribeContainerDefinitionWhichIsAnAliasTestData */
public function testDescribeContainerDefinitionWhichIsAnAlias(Alias $alias, $expectedDescription, ContainerBuilder $builder, $options = array())
{
$this->assertDescription($expectedDescription, $builder, $options);
}

public function getDescribeContainerDefinitionWhichIsAnAliasTestData()
{
$builder = current(ObjectsProvider::getContainerBuilders());
$builder->setDefinition('service_1', $builder->getDefinition('definition_1'));
$builder->setDefinition('service_2', $builder->getDefinition('definition_2'));

$aliases = ObjectsProvider::getContainerAliases();
$aliasesWithDefinitions = array();
foreach ($aliases as $name => $alias) {
$aliasesWithDefinitions[str_replace('alias_', 'alias_with_definition_', $name)] = $alias;
}

$i = 0;
$data = $this->getDescriptionTestData($aliasesWithDefinitions);
foreach ($aliases as $name => $alias) {
$data[$i][] = $builder;
$data[$i][] = array('id' => $name);
++$i;
}

return $data;
}

/** @dataProvider getDescribeContainerParameterTestData */
public function testDescribeContainerParameter($parameter, $expectedDescription, array $options)
{
Expand Down Expand Up @@ -146,7 +175,7 @@ private function assertDescription($expectedDescription, $describedObject, array
if ('json' === $this->getFormat()) {
$this->assertEquals(json_decode($expectedDescription), json_decode($output->fetch()));
} else {
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
$this->assertEquals(trim(preg_replace('/[\s\t\r]+/', ' ', $expectedDescription)), trim(preg_replace('/[\s\t\r]+/', ' ', str_replace(PHP_EOL, "\n", $output->fetch()))));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"service": "service_1",
"public": true
},
{
"class": "Full\\Qualified\\Class1",
"public": true,
"synthetic": false,
"lazy": true,
"shared": true,
"abstract": true,
"autowire": false,
"autowiring_types": [],
"file": null,
"factory_class": "Full\\Qualified\\FactoryClass",
"factory_method": "get",
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
alias_1
~~~~~~~

- Service: `service_1`
- Public: yes

service_1
~~~~~~~~~

- Class: `Full\Qualified\Class1`
- Public: yes
- Synthetic: no
- Lazy: yes
- Shared: yes
- Abstract: yes
- Autowired: no
- Factory Class: `Full\Qualified\FactoryClass`
- Factory Method: `get`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
 // This service is an alias for the service service_1

Information for Service "service_1"
===================================

------------------ -----------------------------
 Option   Value 
------------------ -----------------------------
Service ID service_1
Class Full\Qualified\Class1
Tags -
Public yes
Synthetic no
Lazy yes
Shared yes
Abstract yes
Autowired no
Autowiring Types -
Factory Class Full\Qualified\FactoryClass
Factory Method get
------------------ -----------------------------
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<alias id="alias_1" service="service_1" public="true"/>
<definition id="service_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"/>
</definition>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"service": "service_2",
"public": false
},
{
"class": "Full\\Qualified\\Class2",
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autowiring_types": [],
"file": "\/path\/to\/file",
"factory_service": "factory.service",
"factory_method": "get",
"calls": [
"setMailer"
],
"tags": [
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"attr2": "val2"
}
},
{
"name": "tag1",
"parameters": {
"attr3": "val3"
}
},
{
"name": "tag2",
"parameters": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
alias_2
~~~~~~~

- Service: `service_2`
- Public: no

service_2
~~~~~~~~~

- Class: `Full\Qualified\Class2`
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Abstract: no
- Autowired: no
- File: `/path/to/file`
- Factory Service: `factory.service`
- Factory Method: `get`
- Call: `setMailer`
- Tag: `tag1`
- Attr1: val1
- Attr2: val2
- Tag: `tag1`
- Attr3: val3
- Tag: `tag2`
Loading
0