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\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;
21 * This replaces all DefinitionDecorator instances with their equivalent fully
22 * merged Definition instance.
24 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
25 * @author Nicolas Grekas <p@tchwork.com>
27 class ResolveDefinitionTemplatesPass implements CompilerPassInterface
34 * Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
36 * @param ContainerBuilder $container
38 public function process(ContainerBuilder $container)
40 $this->compiler = $container->getCompiler();
41 $this->formatter = $this->compiler->getLoggingFormatter();
43 $container->setDefinitions($this->resolveArguments($container, $container->getDefinitions(), true));
47 * Resolves definition decorator arguments.
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
55 private function resolveArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
57 foreach ($arguments as $k => $argument) {
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;
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);
70 $container->setDefinition($k, $argument);
73 $argument->setArguments($this->resolveArguments($container, $argument->getArguments()));
74 $argument->setMethodCalls($this->resolveArguments($container, $argument->getMethodCalls()));
75 $argument->setProperties($this->resolveArguments($container, $argument->getProperties()));
77 $configurator = $this->resolveArguments($container, array($argument->getConfigurator()));
78 $argument->setConfigurator($configurator[0]);
80 $factory = $this->resolveArguments($container, array($argument->getFactory()));
81 $argument->setFactory($factory[0]);
89 * Resolves the definition.
91 * @param ContainerBuilder $container The ContainerBuilder
92 * @param DefinitionDecorator $definition
96 * @throws \RuntimeException When the definition is invalid
98 private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
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()));
111 private function doResolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
113 if (!$container->has($parent = $definition->getParent())) {
114 throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
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;
126 $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent));
127 $def = new Definition();
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%'));
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());
146 // overwrite with values specified in the decorator
147 $changes = $definition->getChanges();
148 if (isset($changes['class'])) {
149 $def->setClass($definition->getClass());
151 if (isset($changes['factory'])) {
152 $def->setFactory($definition->getFactory());
154 if (isset($changes['configurator'])) {
155 $def->setConfigurator($definition->getConfigurator());
157 if (isset($changes['file'])) {
158 $def->setFile($definition->getFile());
160 if (isset($changes['public'])) {
161 $def->setPublic($definition->isPublic());
163 if (isset($changes['lazy'])) {
164 $def->setLazy($definition->isLazy());
166 if (isset($changes['deprecated'])) {
167 $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
169 if (isset($changes['autowire'])) {
170 $def->setAutowired($definition->isAutowired());
172 if (isset($changes['decorated_service'])) {
173 $decoratedService = $definition->getDecoratedService();
174 if (null === $decoratedService) {
175 $def->setDecoratedService($decoratedService);
177 $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
182 foreach ($definition->getArguments() as $k => $v) {
183 if (is_numeric($k)) {
184 $def->addArgument($v);
188 if (0 !== strpos($k, 'index_')) {
189 throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
192 $index = (int) substr($k, strlen('index_'));
193 $def->replaceArgument($index, $v);
197 foreach ($definition->getProperties() as $k => $v) {
198 $def->setProperty($k, $v);
201 // append method calls
202 if (count($calls = $definition->getMethodCalls()) > 0) {
203 $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
206 // merge autowiring types
207 foreach ($definition->getAutowiringTypes() as $autowiringType) {
208 $def->addAutowiringType($autowiringType);
211 // these attributes are always taken from the child
212 $def->setAbstract($definition->isAbstract());
213 $def->setShared($definition->isShared());
214 $def->setTags($definition->getTags());