Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveChildDefinitionsPass.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\ChildDefinition;
15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18
19 /**
20  * This replaces all ChildDefinition 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 ResolveChildDefinitionsPass extends AbstractRecursivePass
27 {
28     protected function processValue($value, $isRoot = false)
29     {
30         if (!$value instanceof Definition) {
31             return parent::processValue($value, $isRoot);
32         }
33         if ($isRoot) {
34             // yes, we are specifically fetching the definition from the
35             // container to ensure we are not operating on stale data
36             $value = $this->container->getDefinition($this->currentId);
37         }
38         if ($value instanceof ChildDefinition) {
39             $value = $this->resolveDefinition($value);
40             if ($isRoot) {
41                 $this->container->setDefinition($this->currentId, $value);
42             }
43         }
44
45         return parent::processValue($value, $isRoot);
46     }
47
48     /**
49      * Resolves the definition.
50      *
51      * @return Definition
52      *
53      * @throws RuntimeException When the definition is invalid
54      */
55     private function resolveDefinition(ChildDefinition $definition)
56     {
57         try {
58             return $this->doResolveDefinition($definition);
59         } catch (ExceptionInterface $e) {
60             $r = new \ReflectionProperty($e, 'message');
61             $r->setAccessible(true);
62             $r->setValue($e, sprintf('Service "%s": %s', $this->currentId, $e->getMessage()));
63
64             throw $e;
65         }
66     }
67
68     private function doResolveDefinition(ChildDefinition $definition)
69     {
70         if (!$this->container->has($parent = $definition->getParent())) {
71             throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
72         }
73
74         $parentDef = $this->container->findDefinition($parent);
75         if ($parentDef instanceof ChildDefinition) {
76             $id = $this->currentId;
77             $this->currentId = $parent;
78             $parentDef = $this->resolveDefinition($parentDef);
79             $this->container->setDefinition($parent, $parentDef);
80             $this->currentId = $id;
81         }
82
83         $this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
84         $def = new Definition();
85
86         // merge in parent definition
87         // purposely ignored attributes: abstract, shared, tags, autoconfigured
88         $def->setClass($parentDef->getClass());
89         $def->setArguments($parentDef->getArguments());
90         $def->setMethodCalls($parentDef->getMethodCalls());
91         $def->setProperties($parentDef->getProperties());
92         if ($parentDef->getAutowiringTypes(false)) {
93             $def->setAutowiringTypes($parentDef->getAutowiringTypes(false));
94         }
95         if ($parentDef->isDeprecated()) {
96             $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
97         }
98         $def->setFactory($parentDef->getFactory());
99         $def->setConfigurator($parentDef->getConfigurator());
100         $def->setFile($parentDef->getFile());
101         $def->setPublic($parentDef->isPublic());
102         $def->setLazy($parentDef->isLazy());
103         $def->setAutowired($parentDef->isAutowired());
104         $def->setChanges($parentDef->getChanges());
105
106         $def->setBindings($definition->getBindings() + $parentDef->getBindings());
107
108         // overwrite with values specified in the decorator
109         $changes = $definition->getChanges();
110         if (isset($changes['class'])) {
111             $def->setClass($definition->getClass());
112         }
113         if (isset($changes['factory'])) {
114             $def->setFactory($definition->getFactory());
115         }
116         if (isset($changes['configurator'])) {
117             $def->setConfigurator($definition->getConfigurator());
118         }
119         if (isset($changes['file'])) {
120             $def->setFile($definition->getFile());
121         }
122         if (isset($changes['public'])) {
123             $def->setPublic($definition->isPublic());
124         } else {
125             $def->setPrivate($definition->isPrivate() || $parentDef->isPrivate());
126         }
127         if (isset($changes['lazy'])) {
128             $def->setLazy($definition->isLazy());
129         }
130         if (isset($changes['deprecated'])) {
131             $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
132         }
133         if (isset($changes['autowired'])) {
134             $def->setAutowired($definition->isAutowired());
135         }
136         if (isset($changes['shared'])) {
137             $def->setShared($definition->isShared());
138         }
139         if (isset($changes['decorated_service'])) {
140             $decoratedService = $definition->getDecoratedService();
141             if (null === $decoratedService) {
142                 $def->setDecoratedService($decoratedService);
143             } else {
144                 $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
145             }
146         }
147
148         // merge arguments
149         foreach ($definition->getArguments() as $k => $v) {
150             if (is_numeric($k)) {
151                 $def->addArgument($v);
152             } elseif (0 === strpos($k, 'index_')) {
153                 $def->replaceArgument((int) substr($k, strlen('index_')), $v);
154             } else {
155                 $def->setArgument($k, $v);
156             }
157         }
158
159         // merge properties
160         foreach ($definition->getProperties() as $k => $v) {
161             $def->setProperty($k, $v);
162         }
163
164         // append method calls
165         if ($calls = $definition->getMethodCalls()) {
166             $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
167         }
168
169         // merge autowiring types
170         foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
171             $def->addAutowiringType($autowiringType);
172         }
173
174         // these attributes are always taken from the child
175         $def->setAbstract($definition->isAbstract());
176         $def->setTags($definition->getTags());
177         // autoconfigure is never taken from parent (on purpose)
178         // and it's not legal on an instanceof
179         $def->setAutoconfigured($definition->isAutoconfigured());
180
181         return $def;
182     }
183 }
184
185 class_alias(ResolveChildDefinitionsPass::class, ResolveDefinitionTemplatesPass::class);