ResolveChildDefinitionsPass.php 6.7 KB

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