ResolveDefinitionTemplatesPass.php 8.6 KB

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