Version 1
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveDefinitionTemplatesPass.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\DefinitionDecorator;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18
19 /**
20  * This replaces all DefinitionDecorator instances with their equivalent fully
21  * merged Definition instance.
22  *
23  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24  * @author Nicolas Grekas <p@tchwork.com>
25  */
26 class ResolveDefinitionTemplatesPass implements CompilerPassInterface
27 {
28     private $compiler;
29     private $formatter;
30     private $currentId;
31
32     /**
33      * Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
34      *
35      * @param ContainerBuilder $container
36      */
37     public function process(ContainerBuilder $container)
38     {
39         $this->compiler = $container->getCompiler();
40         $this->formatter = $this->compiler->getLoggingFormatter();
41
42         $container->setDefinitions($this->resolveArguments($container, $container->getDefinitions(), true));
43     }
44
45     /**
46      * Resolves definition decorator arguments.
47      *
48      * @param ContainerBuilder $container The ContainerBuilder
49      * @param array            $arguments An array of arguments
50      * @param bool             $isRoot    If we are processing the root definitions or not
51      *
52      * @return array
53      */
54     private function resolveArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
55     {
56         foreach ($arguments as $k => $argument) {
57             if ($isRoot) {
58                 // yes, we are specifically fetching the definition from the
59                 // container to ensure we are not operating on stale data
60                 $arguments[$k] = $argument = $container->getDefinition($k);
61                 $this->currentId = $k;
62             }
63             if (is_array($argument)) {
64                 $arguments[$k] = $this->resolveArguments($container, $argument);
65             } elseif ($argument instanceof Definition) {
66                 if ($argument instanceof DefinitionDecorator) {
67                     $arguments[$k] = $argument = $this->resolveDefinition($container, $argument);
68                     if ($isRoot) {
69                         $container->setDefinition($k, $argument);
70                     }
71                 }
72                 $argument->setArguments($this->resolveArguments($container, $argument->getArguments()));
73                 $argument->setMethodCalls($this->resolveArguments($container, $argument->getMethodCalls()));
74                 $argument->setProperties($this->resolveArguments($container, $argument->getProperties()));
75
76                 $configurator = $this->resolveArguments($container, array($argument->getConfigurator()));
77                 $argument->setConfigurator($configurator[0]);
78
79                 $factory = $this->resolveArguments($container, array($argument->getFactory()));
80                 $argument->setFactory($factory[0]);
81             }
82         }
83
84         return $arguments;
85     }
86
87     /**
88      * Resolves the definition.
89      *
90      * @param ContainerBuilder    $container  The ContainerBuilder
91      * @param DefinitionDecorator $definition
92      *
93      * @return Definition
94      *
95      * @throws \RuntimeException When the definition is invalid
96      */
97     private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
98     {
99         if (!$container->hasDefinition($parent = $definition->getParent())) {
100             throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId));
101         }
102
103         $parentDef = $container->getDefinition($parent);
104         if ($parentDef instanceof DefinitionDecorator) {
105             $id = $this->currentId;
106             $this->currentId = $parent;
107             $parentDef = $this->resolveDefinition($container, $parentDef);
108             $container->setDefinition($parent, $parentDef);
109             $this->currentId = $id;
110         }
111
112         $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent));
113         $def = new Definition();
114
115         // merge in parent definition
116         // purposely ignored attributes: scope, abstract, tags
117         $def->setClass($parentDef->getClass());
118         $def->setArguments($parentDef->getArguments());
119         $def->setMethodCalls($parentDef->getMethodCalls());
120         $def->setProperties($parentDef->getProperties());
121         $def->setAutowiringTypes($parentDef->getAutowiringTypes());
122         if ($parentDef->getFactoryClass(false)) {
123             $def->setFactoryClass($parentDef->getFactoryClass(false));
124         }
125         if ($parentDef->getFactoryMethod(false)) {
126             $def->setFactoryMethod($parentDef->getFactoryMethod(false));
127         }
128         if ($parentDef->getFactoryService(false)) {
129             $def->setFactoryService($parentDef->getFactoryService(false));
130         }
131         if ($parentDef->isDeprecated()) {
132             $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
133         }
134         $def->setFactory($parentDef->getFactory());
135         $def->setConfigurator($parentDef->getConfigurator());
136         $def->setFile($parentDef->getFile());
137         $def->setPublic($parentDef->isPublic());
138         $def->setLazy($parentDef->isLazy());
139         $def->setAutowired($parentDef->isAutowired());
140
141         // overwrite with values specified in the decorator
142         $changes = $definition->getChanges();
143         if (isset($changes['class'])) {
144             $def->setClass($definition->getClass());
145         }
146         if (isset($changes['factory_class'])) {
147             $def->setFactoryClass($definition->getFactoryClass(false));
148         }
149         if (isset($changes['factory_method'])) {
150             $def->setFactoryMethod($definition->getFactoryMethod(false));
151         }
152         if (isset($changes['factory_service'])) {
153             $def->setFactoryService($definition->getFactoryService(false));
154         }
155         if (isset($changes['factory'])) {
156             $def->setFactory($definition->getFactory());
157         }
158         if (isset($changes['configurator'])) {
159             $def->setConfigurator($definition->getConfigurator());
160         }
161         if (isset($changes['file'])) {
162             $def->setFile($definition->getFile());
163         }
164         if (isset($changes['public'])) {
165             $def->setPublic($definition->isPublic());
166         }
167         if (isset($changes['lazy'])) {
168             $def->setLazy($definition->isLazy());
169         }
170         if (isset($changes['deprecated'])) {
171             $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
172         }
173         if (isset($changes['autowire'])) {
174             $def->setAutowired($definition->isAutowired());
175         }
176         if (isset($changes['decorated_service'])) {
177             $decoratedService = $definition->getDecoratedService();
178             if (null === $decoratedService) {
179                 $def->setDecoratedService($decoratedService);
180             } else {
181                 $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
182             }
183         }
184
185         // merge arguments
186         foreach ($definition->getArguments() as $k => $v) {
187             if (is_numeric($k)) {
188                 $def->addArgument($v);
189                 continue;
190             }
191
192             if (0 !== strpos($k, 'index_')) {
193                 throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
194             }
195
196             $index = (int) substr($k, strlen('index_'));
197             $def->replaceArgument($index, $v);
198         }
199
200         // merge properties
201         foreach ($definition->getProperties() as $k => $v) {
202             $def->setProperty($k, $v);
203         }
204
205         // append method calls
206         if (count($calls = $definition->getMethodCalls()) > 0) {
207             $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
208         }
209
210         // merge autowiring types
211         foreach ($definition->getAutowiringTypes() as $autowiringType) {
212             $def->addAutowiringType($autowiringType);
213         }
214
215         // these attributes are always taken from the child
216         $def->setAbstract($definition->isAbstract());
217         $def->setScope($definition->getScope(false), false);
218         $def->setShared($definition->isShared());
219         $def->setTags($definition->getTags());
220
221         return $def;
222     }
223 }