Drupal investigation

ResolveDefinitionTemplatesPass.php 8.6KB

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