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