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