CheckReferenceValidityPass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  15. use Symfony\Component\DependencyInjection\Exception\ScopeCrossingInjectionException;
  16. use Symfony\Component\DependencyInjection\Exception\ScopeWideningInjectionException;
  17. use Symfony\Component\DependencyInjection\Reference;
  18. /**
  19. * Checks the validity of references.
  20. *
  21. * The following checks are performed by this pass:
  22. * - target definitions are not abstract
  23. * - target definitions are of equal or wider scope
  24. * - target definitions are in the same scope hierarchy
  25. *
  26. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27. */
  28. class CheckReferenceValidityPass implements CompilerPassInterface
  29. {
  30. private $container;
  31. private $currentId;
  32. private $currentScope;
  33. private $currentScopeAncestors;
  34. private $currentScopeChildren;
  35. /**
  36. * Processes the ContainerBuilder to validate References.
  37. */
  38. public function process(ContainerBuilder $container)
  39. {
  40. $this->container = $container;
  41. $children = $this->container->getScopeChildren(false);
  42. $ancestors = array();
  43. $scopes = $this->container->getScopes(false);
  44. foreach ($scopes as $name => $parent) {
  45. $ancestors[$name] = array($parent);
  46. while (isset($scopes[$parent])) {
  47. $ancestors[$name][] = $parent = $scopes[$parent];
  48. }
  49. }
  50. foreach ($container->getDefinitions() as $id => $definition) {
  51. if ($definition->isSynthetic() || $definition->isAbstract()) {
  52. continue;
  53. }
  54. $this->currentId = $id;
  55. $this->currentScope = $scope = $definition->getScope(false);
  56. if (ContainerInterface::SCOPE_CONTAINER === $scope) {
  57. $this->currentScopeChildren = array_keys($scopes);
  58. $this->currentScopeAncestors = array();
  59. } elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
  60. $this->currentScopeChildren = isset($children[$scope]) ? $children[$scope] : array();
  61. $this->currentScopeAncestors = isset($ancestors[$scope]) ? $ancestors[$scope] : array();
  62. }
  63. $this->validateReferences($definition->getArguments());
  64. $this->validateReferences($definition->getMethodCalls());
  65. $this->validateReferences($definition->getProperties());
  66. }
  67. }
  68. /**
  69. * Validates an array of References.
  70. *
  71. * @param array $arguments An array of Reference objects
  72. *
  73. * @throws RuntimeException when there is a reference to an abstract definition
  74. */
  75. private function validateReferences(array $arguments)
  76. {
  77. foreach ($arguments as $argument) {
  78. if (\is_array($argument)) {
  79. $this->validateReferences($argument);
  80. } elseif ($argument instanceof Reference) {
  81. $targetDefinition = $this->getDefinition((string) $argument);
  82. if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
  83. throw new RuntimeException(sprintf('The definition "%s" has a reference to an abstract definition "%s". Abstract definitions cannot be the target of references.', $this->currentId, $argument));
  84. }
  85. $this->validateScope($argument, $targetDefinition);
  86. }
  87. }
  88. }
  89. /**
  90. * Validates the scope of a single Reference.
  91. *
  92. * @throws ScopeWideningInjectionException when the definition references a service of a narrower scope
  93. * @throws ScopeCrossingInjectionException when the definition references a service of another scope hierarchy
  94. */
  95. private function validateScope(Reference $reference, Definition $definition = null)
  96. {
  97. if (ContainerInterface::SCOPE_PROTOTYPE === $this->currentScope) {
  98. return;
  99. }
  100. if (!$reference->isStrict(false)) {
  101. return;
  102. }
  103. if (null === $definition) {
  104. return;
  105. }
  106. if ($this->currentScope === $scope = $definition->getScope(false)) {
  107. return;
  108. }
  109. $id = (string) $reference;
  110. if (\in_array($scope, $this->currentScopeChildren, true)) {
  111. throw new ScopeWideningInjectionException($this->currentId, $this->currentScope, $id, $scope);
  112. }
  113. if (!\in_array($scope, $this->currentScopeAncestors, true)) {
  114. throw new ScopeCrossingInjectionException($this->currentId, $this->currentScope, $id, $scope);
  115. }
  116. }
  117. /**
  118. * Returns the Definition given an id.
  119. *
  120. * @param string $id Definition identifier
  121. *
  122. * @return Definition
  123. */
  124. private function getDefinition($id)
  125. {
  126. if (!$this->container->hasDefinition($id)) {
  127. return;
  128. }
  129. return $this->container->getDefinition($id);
  130. }
  131. }