ResolveChildDefinitionsPass.php 7.3 KB

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