681f8afdde744c612c9b89e408fad7b47a2aabcb
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / AnalyzeServiceReferencesPass.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\Definition;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18 /**
19  * Run this pass before passes that need to know more about the relation of
20  * your services.
21  *
22  * This class will populate the ServiceReferenceGraph with information. You can
23  * retrieve the graph in other passes from the compiler.
24  *
25  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26  */
27 class AnalyzeServiceReferencesPass implements RepeatablePassInterface
28 {
29     private $graph;
30     private $container;
31     private $currentId;
32     private $currentDefinition;
33     private $repeatedPass;
34     private $onlyConstructorArguments;
35
36     /**
37      * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
38      */
39     public function __construct($onlyConstructorArguments = false)
40     {
41         $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     public function setRepeatedPass(RepeatedPass $repeatedPass)
48     {
49         $this->repeatedPass = $repeatedPass;
50     }
51
52     /**
53      * Processes a ContainerBuilder object to populate the service reference graph.
54      *
55      * @param ContainerBuilder $container
56      */
57     public function process(ContainerBuilder $container)
58     {
59         $this->container = $container;
60         $this->graph = $container->getCompiler()->getServiceReferenceGraph();
61         $this->graph->clear();
62
63         foreach ($container->getDefinitions() as $id => $definition) {
64             if ($definition->isSynthetic() || $definition->isAbstract()) {
65                 continue;
66             }
67
68             $this->currentId = $id;
69             $this->currentDefinition = $definition;
70
71             $this->processArguments($definition->getArguments());
72             if ($definition->getFactoryService(false)) {
73                 $this->processArguments(array(new Reference($definition->getFactoryService(false))));
74             }
75             if (is_array($definition->getFactory())) {
76                 $this->processArguments($definition->getFactory());
77             }
78
79             if (!$this->onlyConstructorArguments) {
80                 $this->processArguments($definition->getMethodCalls());
81                 $this->processArguments($definition->getProperties());
82                 if ($definition->getConfigurator()) {
83                     $this->processArguments(array($definition->getConfigurator()));
84                 }
85             }
86         }
87
88         foreach ($container->getAliases() as $id => $alias) {
89             $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
90         }
91     }
92
93     /**
94      * Processes service definitions for arguments to find relationships for the service graph.
95      *
96      * @param array $arguments An array of Reference or Definition objects relating to service definitions
97      */
98     private function processArguments(array $arguments)
99     {
100         foreach ($arguments as $argument) {
101             if (is_array($argument)) {
102                 $this->processArguments($argument);
103             } elseif ($argument instanceof Reference) {
104                 $this->graph->connect(
105                     $this->currentId,
106                     $this->currentDefinition,
107                     $this->getDefinitionId((string) $argument),
108                     $this->getDefinition((string) $argument),
109                     $argument
110                 );
111             } elseif ($argument instanceof Definition) {
112                 $this->processArguments($argument->getArguments());
113                 $this->processArguments($argument->getMethodCalls());
114                 $this->processArguments($argument->getProperties());
115
116                 if (is_array($argument->getFactory())) {
117                     $this->processArguments($argument->getFactory());
118                 }
119                 if ($argument->getFactoryService(false)) {
120                     $this->processArguments(array(new Reference($argument->getFactoryService(false))));
121                 }
122             }
123         }
124     }
125
126     /**
127      * Returns a service definition given the full name or an alias.
128      *
129      * @param string $id A full id or alias for a service definition
130      *
131      * @return Definition|null The definition related to the supplied id
132      */
133     private function getDefinition($id)
134     {
135         $id = $this->getDefinitionId($id);
136
137         return null === $id ? null : $this->container->getDefinition($id);
138     }
139
140     private function getDefinitionId($id)
141     {
142         while ($this->container->hasAlias($id)) {
143             $id = (string) $this->container->getAlias($id);
144         }
145
146         if (!$this->container->hasDefinition($id)) {
147             return;
148         }
149
150         return $id;
151     }
152 }