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