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