ResolveDefinitionTemplatesPass.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. */
  21. class ResolveDefinitionTemplatesPass implements CompilerPassInterface
  22. {
  23. private $container;
  24. private $compiler;
  25. private $formatter;
  26. /**
  27. * Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
  28. *
  29. * @param ContainerBuilder $container
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $this->container = $container;
  34. $this->compiler = $container->getCompiler();
  35. $this->formatter = $this->compiler->getLoggingFormatter();
  36. foreach (array_keys($container->getDefinitions()) as $id) {
  37. // yes, we are specifically fetching the definition from the
  38. // container to ensure we are not operating on stale data
  39. $definition = $container->getDefinition($id);
  40. if (!$definition instanceof DefinitionDecorator || $definition->isAbstract()) {
  41. continue;
  42. }
  43. $this->resolveDefinition($id, $definition);
  44. }
  45. }
  46. /**
  47. * Resolves the definition
  48. *
  49. * @param string $id The definition identifier
  50. * @param DefinitionDecorator $definition
  51. *
  52. * @return Definition
  53. *
  54. * @throws \RuntimeException When the definition is invalid
  55. */
  56. private function resolveDefinition($id, DefinitionDecorator $definition)
  57. {
  58. if (!$this->container->hasDefinition($parent = $definition->getParent())) {
  59. throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
  60. }
  61. $parentDef = $this->container->getDefinition($parent);
  62. if ($parentDef instanceof DefinitionDecorator) {
  63. $parentDef = $this->resolveDefinition($parent, $parentDef);
  64. }
  65. $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent));
  66. $def = new Definition();
  67. // merge in parent definition
  68. // purposely ignored attributes: scope, abstract, tags
  69. $def->setClass($parentDef->getClass());
  70. $def->setArguments($parentDef->getArguments());
  71. $def->setMethodCalls($parentDef->getMethodCalls());
  72. $def->setProperties($parentDef->getProperties());
  73. $def->setFactoryClass($parentDef->getFactoryClass());
  74. $def->setFactoryMethod($parentDef->getFactoryMethod());
  75. $def->setFactoryService($parentDef->getFactoryService());
  76. $def->setConfigurator($parentDef->getConfigurator());
  77. $def->setFile($parentDef->getFile());
  78. $def->setPublic($parentDef->isPublic());
  79. $def->setLazy($parentDef->isLazy());
  80. // overwrite with values specified in the decorator
  81. $changes = $definition->getChanges();
  82. if (isset($changes['class'])) {
  83. $def->setClass($definition->getClass());
  84. }
  85. if (isset($changes['factory_class'])) {
  86. $def->setFactoryClass($definition->getFactoryClass());
  87. }
  88. if (isset($changes['factory_method'])) {
  89. $def->setFactoryMethod($definition->getFactoryMethod());
  90. }
  91. if (isset($changes['factory_service'])) {
  92. $def->setFactoryService($definition->getFactoryService());
  93. }
  94. if (isset($changes['configurator'])) {
  95. $def->setConfigurator($definition->getConfigurator());
  96. }
  97. if (isset($changes['file'])) {
  98. $def->setFile($definition->getFile());
  99. }
  100. if (isset($changes['public'])) {
  101. $def->setPublic($definition->isPublic());
  102. }
  103. if (isset($changes['lazy'])){
  104. $def->setLazy($definition->isLazy());
  105. }
  106. // merge arguments
  107. foreach ($definition->getArguments() as $k => $v) {
  108. if (is_numeric($k)) {
  109. $def->addArgument($v);
  110. continue;
  111. }
  112. if (0 !== strpos($k, 'index_')) {
  113. throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
  114. }
  115. $index = (integer) substr($k, strlen('index_'));
  116. $def->replaceArgument($index, $v);
  117. }
  118. // merge properties
  119. foreach ($definition->getProperties() as $k => $v) {
  120. $def->setProperty($k, $v);
  121. }
  122. // append method calls
  123. if (count($calls = $definition->getMethodCalls()) > 0) {
  124. $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
  125. }
  126. // these attributes are always taken from the child
  127. $def->setAbstract($definition->isAbstract());
  128. $def->setScope($definition->getScope());
  129. $def->setTags($definition->getTags());
  130. // set new definition on container
  131. $this->container->setDefinition($id, $def);
  132. return $def;
  133. }
  134. }