ResolveChildDefinitionsPass.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, $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. $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
  92. }
  93. $def->setFactory($parentDef->getFactory());
  94. $def->setConfigurator($parentDef->getConfigurator());
  95. $def->setFile($parentDef->getFile());
  96. $def->setPublic($parentDef->isPublic());
  97. $def->setLazy($parentDef->isLazy());
  98. $def->setAutowired($parentDef->isAutowired());
  99. $def->setChanges($parentDef->getChanges());
  100. $def->setBindings($definition->getBindings() + $parentDef->getBindings());
  101. // overwrite with values specified in the decorator
  102. $changes = $definition->getChanges();
  103. if (isset($changes['class'])) {
  104. $def->setClass($definition->getClass());
  105. }
  106. if (isset($changes['factory'])) {
  107. $def->setFactory($definition->getFactory());
  108. }
  109. if (isset($changes['configurator'])) {
  110. $def->setConfigurator($definition->getConfigurator());
  111. }
  112. if (isset($changes['file'])) {
  113. $def->setFile($definition->getFile());
  114. }
  115. if (isset($changes['public'])) {
  116. $def->setPublic($definition->isPublic());
  117. } else {
  118. $def->setPrivate($definition->isPrivate() || $parentDef->isPrivate());
  119. }
  120. if (isset($changes['lazy'])) {
  121. $def->setLazy($definition->isLazy());
  122. }
  123. if (isset($changes['deprecated'])) {
  124. $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
  125. }
  126. if (isset($changes['autowired'])) {
  127. $def->setAutowired($definition->isAutowired());
  128. }
  129. if (isset($changes['shared'])) {
  130. $def->setShared($definition->isShared());
  131. }
  132. if (isset($changes['decorated_service'])) {
  133. $decoratedService = $definition->getDecoratedService();
  134. if (null === $decoratedService) {
  135. $def->setDecoratedService($decoratedService);
  136. } else {
  137. $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2], $decoratedService[3] ?? ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
  138. }
  139. }
  140. // merge arguments
  141. foreach ($definition->getArguments() as $k => $v) {
  142. if (is_numeric($k)) {
  143. $def->addArgument($v);
  144. } elseif (0 === strpos($k, 'index_')) {
  145. $def->replaceArgument((int) substr($k, \strlen('index_')), $v);
  146. } else {
  147. $def->setArgument($k, $v);
  148. }
  149. }
  150. // merge properties
  151. foreach ($definition->getProperties() as $k => $v) {
  152. $def->setProperty($k, $v);
  153. }
  154. // append method calls
  155. if ($calls = $definition->getMethodCalls()) {
  156. $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
  157. }
  158. $def->addError($parentDef);
  159. $def->addError($definition);
  160. // these attributes are always taken from the child
  161. $def->setAbstract($definition->isAbstract());
  162. $def->setTags($definition->getTags());
  163. // autoconfigure is never taken from parent (on purpose)
  164. // and it's not legal on an instanceof
  165. $def->setAutoconfigured($definition->isAutoconfigured());
  166. return $def;
  167. }
  168. }