4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
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;
20 * This replaces all ChildDefinition instances with their equivalent fully
21 * merged Definition instance.
23 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 * @author Nicolas Grekas <p@tchwork.com>
26 class ResolveChildDefinitionsPass extends AbstractRecursivePass
28 protected function processValue($value, $isRoot = false)
30 if (!$value instanceof Definition) {
31 return parent::processValue($value, $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);
38 if ($value instanceof ChildDefinition) {
39 $value = $this->resolveDefinition($value);
41 $this->container->setDefinition($this->currentId, $value);
45 return parent::processValue($value, $isRoot);
49 * Resolves the definition.
53 * @throws RuntimeException When the definition is invalid
55 private function resolveDefinition(ChildDefinition $definition)
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()));
68 private function doResolveDefinition(ChildDefinition $definition)
70 if (!$this->container->has($parent = $definition->getParent())) {
71 throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
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;
83 $this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
84 $def = new Definition();
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));
95 if ($parentDef->isDeprecated()) {
96 $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
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());
106 $def->setBindings($definition->getBindings() + $parentDef->getBindings());
108 // overwrite with values specified in the decorator
109 $changes = $definition->getChanges();
110 if (isset($changes['class'])) {
111 $def->setClass($definition->getClass());
113 if (isset($changes['factory'])) {
114 $def->setFactory($definition->getFactory());
116 if (isset($changes['configurator'])) {
117 $def->setConfigurator($definition->getConfigurator());
119 if (isset($changes['file'])) {
120 $def->setFile($definition->getFile());
122 if (isset($changes['public'])) {
123 $def->setPublic($definition->isPublic());
125 $def->setPrivate($definition->isPrivate() || $parentDef->isPrivate());
127 if (isset($changes['lazy'])) {
128 $def->setLazy($definition->isLazy());
130 if (isset($changes['deprecated'])) {
131 $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
133 if (isset($changes['autowired'])) {
134 $def->setAutowired($definition->isAutowired());
136 if (isset($changes['shared'])) {
137 $def->setShared($definition->isShared());
139 if (isset($changes['decorated_service'])) {
140 $decoratedService = $definition->getDecoratedService();
141 if (null === $decoratedService) {
142 $def->setDecoratedService($decoratedService);
144 $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
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);
155 $def->setArgument($k, $v);
160 foreach ($definition->getProperties() as $k => $v) {
161 $def->setProperty($k, $v);
164 // append method calls
165 if ($calls = $definition->getMethodCalls()) {
166 $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
169 // merge autowiring types
170 foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
171 $def->addAutowiringType($autowiringType);
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());
185 class_alias(ResolveChildDefinitionsPass::class, ResolveDefinitionTemplatesPass::class);