composer package updates
[openemr.git] / vendor / symfony / dependency-injection / Compiler / RemoveUnusedDefinitionsPass.php
blob61602afd8905f50a0cedf4e5f877338bbb6e9f3c
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 /**
17 * Removes unused service definitions from the container.
19 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21 class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
23 private $repeatedPass;
25 /**
26 * {@inheritdoc}
28 public function setRepeatedPass(RepeatedPass $repeatedPass)
30 $this->repeatedPass = $repeatedPass;
33 /**
34 * Processes the ContainerBuilder to remove unused definitions.
36 public function process(ContainerBuilder $container)
38 $compiler = $container->getCompiler();
39 $formatter = $compiler->getLoggingFormatter();
40 $graph = $compiler->getServiceReferenceGraph();
42 $hasChanged = false;
43 foreach ($container->getDefinitions() as $id => $definition) {
44 if ($definition->isPublic()) {
45 continue;
48 if ($graph->hasNode($id)) {
49 $edges = $graph->getNode($id)->getInEdges();
50 $referencingAliases = array();
51 $sourceIds = array();
52 foreach ($edges as $edge) {
53 $node = $edge->getSourceNode();
54 $sourceIds[] = $node->getId();
56 if ($node->isAlias()) {
57 $referencingAliases[] = $node->getValue();
60 $isReferenced = (count(array_unique($sourceIds)) - count($referencingAliases)) > 0;
61 } else {
62 $referencingAliases = array();
63 $isReferenced = false;
66 if (1 === count($referencingAliases) && false === $isReferenced) {
67 $container->setDefinition((string) reset($referencingAliases), $definition);
68 $definition->setPublic(true);
69 $container->removeDefinition($id);
70 $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias '.reset($referencingAliases)));
71 } elseif (0 === count($referencingAliases) && false === $isReferenced) {
72 $container->removeDefinition($id);
73 $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused'));
74 $hasChanged = true;
78 if ($hasChanged) {
79 $this->repeatedPass->setRepeat();