ResolveDefinitionTemplatesPass.php 6.5 KB

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