296051302d9e3f8b185889d6730a6df3db0a840a
[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\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\DependencyInjection\Definition;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18 use Symfony\Component\DependencyInjection\ExpressionLanguage;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\ExpressionLanguage\Expression;
22
23 /**
24  * Run this pass before passes that need to know more about the relation of
25  * your services.
26  *
27  * This class will populate the ServiceReferenceGraph with information. You can
28  * retrieve the graph in other passes from the compiler.
29  *
30  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
31  */
32 class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
33 {
34     private $graph;
35     private $currentDefinition;
36     private $onlyConstructorArguments;
37     private $lazy;
38     private $expressionLanguage;
39
40     /**
41      * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
42      */
43     public function __construct($onlyConstructorArguments = false)
44     {
45         $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function setRepeatedPass(RepeatedPass $repeatedPass)
52     {
53         // no-op for BC
54     }
55
56     /**
57      * Processes a ContainerBuilder object to populate the service reference graph.
58      */
59     public function process(ContainerBuilder $container)
60     {
61         $this->container = $container;
62         $this->graph = $container->getCompiler()->getServiceReferenceGraph();
63         $this->graph->clear();
64         $this->lazy = false;
65
66         foreach ($container->getAliases() as $id => $alias) {
67             $targetId = $this->getDefinitionId((string) $alias);
68             $this->graph->connect($id, $alias, $targetId, $this->getDefinition($targetId), null);
69         }
70
71         parent::process($container);
72     }
73
74     protected function processValue($value, $isRoot = false)
75     {
76         $lazy = $this->lazy;
77
78         if ($value instanceof ArgumentInterface) {
79             $this->lazy = true;
80             parent::processValue($value->getValues());
81             $this->lazy = $lazy;
82
83             return $value;
84         }
85         if ($value instanceof Expression) {
86             $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
87
88             return $value;
89         }
90         if ($value instanceof Reference) {
91             $targetId = $this->getDefinitionId((string) $value);
92             $targetDefinition = $this->getDefinition($targetId);
93
94             $this->graph->connect(
95                 $this->currentId,
96                 $this->currentDefinition,
97                 $targetId,
98                 $targetDefinition,
99                 $value,
100                 $this->lazy || ($targetDefinition && $targetDefinition->isLazy()),
101                 ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
102             );
103
104             return $value;
105         }
106         if (!$value instanceof Definition) {
107             return parent::processValue($value, $isRoot);
108         }
109         if ($isRoot) {
110             if ($value->isSynthetic() || $value->isAbstract()) {
111                 return $value;
112             }
113             $this->currentDefinition = $value;
114         }
115         $this->lazy = false;
116
117         $this->processValue($value->getFactory());
118         $this->processValue($value->getArguments());
119
120         if (!$this->onlyConstructorArguments) {
121             $this->processValue($value->getProperties());
122             $this->processValue($value->getMethodCalls());
123             $this->processValue($value->getConfigurator());
124         }
125         $this->lazy = $lazy;
126
127         return $value;
128     }
129
130     /**
131      * Returns a service definition given the full name or an alias.
132      *
133      * @param string $id A full id or alias for a service definition
134      *
135      * @return Definition|null The definition related to the supplied id
136      */
137     private function getDefinition($id)
138     {
139         return null === $id ? null : $this->container->getDefinition($id);
140     }
141
142     private function getDefinitionId($id)
143     {
144         while ($this->container->hasAlias($id)) {
145             $id = (string) $this->container->getAlias($id);
146         }
147
148         if (!$this->container->hasDefinition($id)) {
149             return;
150         }
151
152         return $this->container->normalizeId($id);
153     }
154
155     private function getExpressionLanguage()
156     {
157         if (null === $this->expressionLanguage) {
158             if (!class_exists(ExpressionLanguage::class)) {
159                 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
160             }
161
162             $providers = $this->container->getExpressionLanguageProviders();
163             $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
164                 if ('""' === substr_replace($arg, '', 1, -1)) {
165                     $id = stripcslashes(substr($arg, 1, -1));
166                     $id = $this->getDefinitionId($id);
167
168                     $this->graph->connect(
169                         $this->currentId,
170                         $this->currentDefinition,
171                         $id,
172                         $this->getDefinition($id)
173                     );
174                 }
175
176                 return sprintf('$this->get(%s)', $arg);
177             });
178         }
179
180         return $this->expressionLanguage;
181     }
182 }