Skip to content

Commit 4f26535

Browse files
author
Bert ter Heide
committed
Expand usage of service edges
1 parent fca51e4 commit 4f26535

22 files changed

+64
-110
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Component\DependencyInjection\Alias;
1818
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
19+
use Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraphEdge;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
2021
use Symfony\Component\DependencyInjection\ContainerInterface;
2122
use Symfony\Component\DependencyInjection\Definition;
23+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2224
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2325
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2426
use Symfony\Component\Routing\Route;
@@ -36,11 +38,6 @@ abstract class Descriptor implements DescriptorInterface
3638
*/
3739
protected $output;
3840

39-
/**
40-
* @var array
41-
*/
42-
private $usages = [];
43-
4441
/**
4542
* {@inheritdoc}
4643
*/
@@ -369,4 +366,15 @@ private function getContainerEnvVars(ContainerBuilder $container): array
369366

370367
return array_values($envs);
371368
}
369+
370+
protected function getServiceEdges(ContainerBuilder $builder, string $serviceId): array
371+
{
372+
try {
373+
return array_map(function (ServiceReferenceGraphEdge $edge) {
374+
return $edge->getSourceNode()->getId();
375+
}, $builder->getCompiler()->getServiceReferenceGraph()->getNode($serviceId)->getInEdges());
376+
} catch (InvalidArgumentException $exception) {
377+
return [];
378+
}
379+
}
372380
}

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
6060
foreach ($this->findDefinitionsByTag($builder, $showHidden) as $tag => $definitions) {
6161
$data[$tag] = [];
6262
foreach ($definitions as $definition) {
63-
$data[$tag][] = $this->getContainerDefinitionData($definition, true);
63+
$data[$tag][] = $this->getContainerDefinitionData($definition, true, false, $builder, isset($options['id']) ? $options['id'] : null);
6464
}
6565
}
6666

@@ -76,7 +76,7 @@ protected function describeContainerService($service, array $options = [], Conta
7676
if ($service instanceof Alias) {
7777
$this->describeContainerAlias($service, $options, $builder);
7878
} elseif ($service instanceof Definition) {
79-
$this->writeData($this->getContainerDefinitionData($service, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments']), $options);
79+
$this->writeData($this->getContainerDefinitionData($service, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $builder, $options['id']), $options);
8080
} else {
8181
$this->writeData(\get_class($service), $options);
8282
}
@@ -106,7 +106,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
106106
if ($service instanceof Alias) {
107107
$data['aliases'][$serviceId] = $this->getContainerAliasData($service);
108108
} elseif ($service instanceof Definition) {
109-
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments);
109+
$data['definitions'][$serviceId] = $this->getContainerDefinitionData($service, $omitTags, $showArguments, $builder, $serviceId);
110110
} else {
111111
$data['services'][$serviceId] = \get_class($service);
112112
}
@@ -117,7 +117,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
117117

118118
protected function describeContainerDefinition(Definition $definition, array $options = [], ContainerBuilder $builder = null)
119119
{
120-
$this->writeData($this->getContainerDefinitionData($definition, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments']), $options);
120+
$this->writeData($this->getContainerDefinitionData($definition, isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $builder, isset($options['id']) ? $options['id'] : null), $options);
121121
}
122122

123123
protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $builder = null)
@@ -129,7 +129,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
129129
}
130130

131131
$this->writeData(
132-
[$this->getContainerAliasData($alias), $this->getContainerDefinitionData($builder->getDefinition((string) $alias), isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'])],
132+
[$this->getContainerAliasData($alias), $this->getContainerDefinitionData($builder->getDefinition((string) $alias), isset($options['omit_tags']) && $options['omit_tags'], isset($options['show_arguments']) && $options['show_arguments'], $builder, (string) $alias)],
133133
array_merge($options, ['id' => (string) $alias])
134134
);
135135
}
@@ -227,7 +227,7 @@ private function getContainerDefinitionData(Definition $definition, bool $omitTa
227227
}
228228

229229
if ($showArguments) {
230-
$data['arguments'] = $this->describeValue($definition->getArguments(), $omitTags, $showArguments);
230+
$data['arguments'] = $this->describeValue($definition->getArguments(), $omitTags, $showArguments, $builder, $id);
231231
}
232232

233233
$data['file'] = $definition->getFile();
@@ -264,23 +264,11 @@ private function getContainerDefinitionData(Definition $definition, bool $omitTa
264264
}
265265
}
266266

267-
if (null !== $builder && null !== $id) {
268-
$data['usage'] = $this->formatInEdges($builder->getCompiler()->getServiceReferenceGraph()->getNode($id)->getInEdges());
269-
}
267+
$data['usages'] = null !== $builder && null !== $id ? $this->getServiceEdges($builder, $id) : [];
270268

271269
return $data;
272270
}
273271

274-
/**
275-
* @param ServiceReferenceGraphEdge[] $inEdges
276-
*/
277-
protected function formatInEdges(array $inEdges): array
278-
{
279-
return array_map(function (ServiceReferenceGraphEdge $edge) {
280-
return $edge->getSourceNode()->getId();
281-
}, $inEdges);
282-
}
283-
284272
private function getContainerAliasData(Alias $alias): array
285273
{
286274
return [
@@ -386,12 +374,12 @@ private function getCallableData($callable): array
386374
throw new \InvalidArgumentException('Callable is not describable.');
387375
}
388376

389-
private function describeValue($value, bool $omitTags, bool $showArguments)
377+
private function describeValue($value, bool $omitTags, bool $showArguments, ContainerBuilder $builder = null, string $id = null)
390378
{
391379
if (\is_array($value)) {
392380
$data = [];
393381
foreach ($value as $k => $v) {
394-
$data[$k] = $this->describeValue($v, $omitTags, $showArguments);
382+
$data[$k] = $this->describeValue($v, $omitTags, $showArguments, $builder, $id);
395383
}
396384

397385
return $data;
@@ -409,11 +397,11 @@ private function describeValue($value, bool $omitTags, bool $showArguments)
409397
}
410398

411399
if ($value instanceof ArgumentInterface) {
412-
return $this->describeValue($value->getValues(), $omitTags, $showArguments);
400+
return $this->describeValue($value->getValues(), $omitTags, $showArguments, $builder, $id);
413401
}
414402

415403
if ($value instanceof Definition) {
416-
return $this->getContainerDefinitionData($value, $omitTags, $showArguments);
404+
return $this->getContainerDefinitionData($value, $omitTags, $showArguments, $builder, $id);
417405
}
418406

419407
return $value;

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected function describeContainerTags(ContainerBuilder $builder, array $optio
8484
$this->write("\n\n".$tag."\n".str_repeat('-', \strlen($tag)));
8585
foreach ($definitions as $serviceId => $definition) {
8686
$this->write("\n\n");
87-
$this->describeContainerDefinition($definition, ['omit_tags' => true, 'id' => $serviceId]);
87+
$this->describeContainerDefinition($definition, ['omit_tags' => true, 'id' => $serviceId], $builder);
8888
}
8989
}
9090
}
@@ -100,7 +100,7 @@ protected function describeContainerService($service, array $options = [], Conta
100100
if ($service instanceof Alias) {
101101
$this->describeContainerAlias($service, $childOptions, $builder);
102102
} elseif ($service instanceof Definition) {
103-
$this->describeContainerDefinition($service, $childOptions);
103+
$this->describeContainerDefinition($service, $childOptions, $builder);
104104
} else {
105105
$this->write(sprintf('**`%s`:** `%s`', $options['id'], \get_class($service)));
106106
}
@@ -173,7 +173,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
173173
$this->write("\n\nDefinitions\n-----------\n");
174174
foreach ($services['definitions'] as $id => $service) {
175175
$this->write("\n");
176-
$this->describeContainerDefinition($service, ['id' => $id, 'show_arguments' => $showArguments]);
176+
$this->describeContainerDefinition($service, ['id' => $id, 'show_arguments' => $showArguments], $builder);
177177
}
178178
}
179179

@@ -251,24 +251,12 @@ protected function describeContainerDefinition(Definition $definition, array $op
251251
}
252252
}
253253

254-
if (null !== $builder && isset($options['id'])) {
255-
$inEdges = $this->formatInEdges($builder->getCompiler()->getServiceReferenceGraph()->getNode($options['id'])->getInEdges());
256-
$output .= "\n".'- Usage: '.($inEdges ? implode(', ', $inEdges) : 'none');
257-
}
254+
$inEdges = null !== $builder && isset($options['id']) ? $this->getServiceEdges($builder, $options['id']) : [];
255+
$output .= "\n".'- Usages: '.($inEdges ? implode(', ', $inEdges) : 'none');
258256

259257
$this->write(isset($options['id']) ? sprintf("### %s\n\n%s\n", $options['id'], $output) : $output);
260258
}
261259

262-
/**
263-
* @param ServiceReferenceGraphEdge[] $inEdges
264-
*/
265-
protected function formatInEdges(array $inEdges): array
266-
{
267-
return array_map(function (ServiceReferenceGraphEdge $edge) {
268-
return $edge->getSourceNode()->getId();
269-
}, $inEdges);
270-
}
271-
272260
protected function describeContainerAlias(Alias $alias, array $options = [], ContainerBuilder $builder = null)
273261
{
274262
$output = '- Service: `'.$alias.'`'
@@ -287,7 +275,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
287275
}
288276

289277
$this->write("\n");
290-
$this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, ['id' => (string) $alias]));
278+
$this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, ['id' => (string) $alias]), $builder);
291279
}
292280

293281
protected function describeContainerParameter($parameter, array $options = [])

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
2121
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2222
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
23-
use Symfony\Component\DependencyInjection\Compiler\ServiceReferenceGraphEdge;
2423
use Symfony\Component\DependencyInjection\ContainerBuilder;
2524
use Symfony\Component\DependencyInjection\Definition;
2625
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@@ -356,24 +355,12 @@ protected function describeContainerDefinition(Definition $definition, array $op
356355
$tableRows[] = ['Arguments', implode("\n", $argumentsInformation)];
357356
}
358357

359-
if (null !== $builder && isset($options['id'])) {
360-
$inEdges = $this->formatInEdges($builder->getCompiler()->getServiceReferenceGraph()->getNode($options['id'])->getInEdges());
361-
$tableRows[] = ['Usages', $inEdges ? implode(', ', $inEdges) : 'none'];
362-
}
358+
$inEdges = null !== $builder && isset($options['id']) ? $this->getServiceEdges($builder, $options['id']) : [];
359+
$tableRows[] = ['Usages', $inEdges ? implode(', ', $inEdges) : 'none'];
363360

364361
$options['output']->table($tableHeaders, $tableRows);
365362
}
366363

367-
/**
368-
* @param ServiceReferenceGraphEdge[] $inEdges
369-
*/
370-
protected function formatInEdges(array $inEdges): array
371-
{
372-
return array_map(function (ServiceReferenceGraphEdge $edge) {
373-
return $edge->getSourceNode()->getId();
374-
}, $inEdges);
375-
}
376-
377364
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
378365
{
379366
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
@@ -412,7 +399,7 @@ protected function describeContainerAlias(Alias $alias, array $options = [], Con
412399
return;
413400
}
414401

415-
$this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, ['id' => (string) $alias]));
402+
$this->describeContainerDefinition($builder->getDefinition((string) $alias), array_merge($options, ['id' => (string) $alias]), $builder);
416403
}
417404

418405
protected function describeContainerParameter($parameter, array $options = [])

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private function getContainerTagsDocument(ContainerBuilder $builder, bool $showH
246246
$tagXML->setAttribute('name', $tag);
247247

248248
foreach ($definitions as $serviceId => $definition) {
249-
$definitionXML = $this->getContainerDefinitionDocument($definition, $serviceId, true);
249+
$definitionXML = $this->getContainerDefinitionDocument($definition, $serviceId, true, false, $builder);
250250
$tagXML->appendChild($dom->importNode($definitionXML->childNodes->item(0), true));
251251
}
252252
}
@@ -264,7 +264,7 @@ private function getContainerServiceDocument($service, string $id, ContainerBuil
264264
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($builder->getDefinition((string) $service), (string) $service, false, $showArguments, $builder)->childNodes->item(0), true));
265265
}
266266
} elseif ($service instanceof Definition) {
267-
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id, false, $showArguments)->childNodes->item(0), true));
267+
$dom->appendChild($dom->importNode($this->getContainerDefinitionDocument($service, $id, false, $showArguments, $builder)->childNodes->item(0), true));
268268
} else {
269269
$dom->appendChild($serviceXML = $dom->createElement('service'));
270270
$serviceXML->setAttribute('id', $id);
@@ -355,7 +355,7 @@ private function getContainerDefinitionDocument(Definition $definition, string $
355355
}
356356

357357
if ($showArguments) {
358-
foreach ($this->getArgumentNodes($definition->getArguments(), $dom) as $node) {
358+
foreach ($this->getArgumentNodes($definition->getArguments(), $dom, $builder) as $node) {
359359
$serviceXML->appendChild($node);
360360
}
361361
}
@@ -378,12 +378,12 @@ private function getContainerDefinitionDocument(Definition $definition, string $
378378
}
379379

380380
if (null !== $builder && null !== $id) {
381-
$inEdges = $builder->getCompiler()->getServiceReferenceGraph()->getNode($id)->getInEdges();
382-
if ($inEdges) {
381+
$edges = $this->getServiceEdges($builder, $id);
382+
if ($edges) {
383383
$serviceXML->appendChild($usagesXML = $dom->createElement('usages'));
384-
foreach ($inEdges as $usage) {
384+
foreach ($edges as $edge) {
385385
$usagesXML->appendChild($usageXML = $dom->createElement('usage'));
386-
$usageXML->appendChild(new \DOMText($usage));
386+
$usageXML->appendChild(new \DOMText($edge));
387387
}
388388
}
389389
}
@@ -394,7 +394,7 @@ private function getContainerDefinitionDocument(Definition $definition, string $
394394
/**
395395
* @return \DOMNode[]
396396
*/
397-
private function getArgumentNodes(array $arguments, \DOMDocument $dom): array
397+
private function getArgumentNodes(array $arguments, \DOMDocument $dom, ContainerBuilder $builder = null): array
398398
{
399399
$nodes = [];
400400

@@ -415,15 +415,15 @@ private function getArgumentNodes(array $arguments, \DOMDocument $dom): array
415415
} elseif ($argument instanceof IteratorArgument || $argument instanceof ServiceLocatorArgument) {
416416
$argumentXML->setAttribute('type', $argument instanceof IteratorArgument ? 'iterator' : 'service_locator');
417417

418-
foreach ($this->getArgumentNodes($argument->getValues(), $dom) as $childArgumentXML) {
418+
foreach ($this->getArgumentNodes($argument->getValues(), $dom, $builder) as $childArgumentXML) {
419419
$argumentXML->appendChild($childArgumentXML);
420420
}
421421
} elseif ($argument instanceof Definition) {
422-
$argumentXML->appendChild($dom->importNode($this->getContainerDefinitionDocument($argument, null, false, true)->childNodes->item(0), true));
422+
$argumentXML->appendChild($dom->importNode($this->getContainerDefinitionDocument($argument, null, false, true, $builder)->childNodes->item(0), true));
423423
} elseif (\is_array($argument)) {
424424
$argumentXML->setAttribute('type', 'collection');
425425

426-
foreach ($this->getArgumentNodes($argument, $dom) as $childArgumentXML) {
426+
foreach ($this->getArgumentNodes($argument, $dom, $builder) as $childArgumentXML) {
427427
$argumentXML->appendChild($childArgumentXML);
428428
}
429429
} else {

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/alias_with_definition_1.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"factory_class": "Full\\Qualified\\FactoryClass",
1717
"factory_method": "get",
1818
"tags": [],
19-
"usages": []
19+
"usages": [
20+
"alias_1"
21+
]
2022
}
2123
]

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/alias_with_definition_1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
- Autoconfigured: no
1616
- Factory Class: `Full\Qualified\FactoryClass`
1717
- Factory Method: `get`
18-
- Usages: none
18+
- Usages: alias_1

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/alias_with_definition_1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
Autoconfigured no
1919
Factory Class Full\Qualified\FactoryClass
2020
Factory Method get
21-
Used by none
21+
Usages alias_1
2222
---------------- -----------------------------

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/alias_with_definition_1.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
<alias id="alias_1" service="service_1" public="true"/>
33
<definition id="service_1" class="Full\Qualified\Class1" public="true" synthetic="false" lazy="true" shared="true" abstract="true" autowired="false" autoconfigured="false" file="">
44
<factory class="Full\Qualified\FactoryClass" method="get"/>
5+
<usages>
6+
<usage>alias_1</usage>
7+
</usages>
58
</definition>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/alias_with_definition_2.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
}
3939
],
4040
"usages": [
41-
"definition_1",
42-
"service_1"
41+
".alias_2"
4342
]
4443
}
4544
]

0 commit comments

Comments
 (0)