Drupal investigation

CheckReferenceValidityPass.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  16. use Symfony\Component\DependencyInjection\Exception\ScopeCrossingInjectionException;
  17. use Symfony\Component\DependencyInjection\Exception\ScopeWideningInjectionException;
  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. * @param ContainerBuilder $container
  39. */
  40. public function process(ContainerBuilder $container)
  41. {
  42. $this->container = $container;
  43. $children = $this->container->getScopeChildren(false);
  44. $ancestors = array();
  45. $scopes = $this->container->getScopes(false);
  46. foreach ($scopes as $name => $parent) {
  47. $ancestors[$name] = array($parent);
  48. while (isset($scopes[$parent])) {
  49. $ancestors[$name][] = $parent = $scopes[$parent];
  50. }
  51. }
  52. foreach ($container->getDefinitions() as $id => $definition) {
  53. if ($definition->isSynthetic() || $definition->isAbstract()) {
  54. continue;
  55. }
  56. $this->currentId = $id;
  57. $this->currentScope = $scope = $definition->getScope(false);
  58. if (ContainerInterface::SCOPE_CONTAINER === $scope) {
  59. $this->currentScopeChildren = array_keys($scopes);
  60. $this->currentScopeAncestors = array();
  61. } elseif (ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
  62. $this->currentScopeChildren = isset($children[$scope]) ? $children[$scope] : array();
  63. $this->currentScopeAncestors = isset($ancestors[$scope]) ? $ancestors[$scope] : array();
  64. }
  65. $this->validateReferences($definition->getArguments());
  66. $this->validateReferences($definition->getMethodCalls());
  67. $this->validateReferences($definition->getProperties());
  68. }
  69. }
  70. /**
  71. * Validates an array of References.
  72. *
  73. * @param array $arguments An array of Reference objects
  74. *
  75. * @throws RuntimeException when there is a reference to an abstract definition.
  76. */
  77. private function validateReferences(array $arguments)
  78. {
  79. foreach ($arguments as $argument) {
  80. if (is_array($argument)) {
  81. $this->validateReferences($argument);
  82. } elseif ($argument instanceof Reference) {
  83. $targetDefinition = $this->getDefinition((string) $argument);
  84. if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
  85. throw new RuntimeException(sprintf(
  86. 'The definition "%s" has a reference to an abstract definition "%s". '
  87. .'Abstract definitions cannot be the target of references.',
  88. $this->currentId,
  89. $argument
  90. ));
  91. }
  92. $this->validateScope($argument, $targetDefinition);
  93. }
  94. }
  95. }
  96. /**
  97. * Validates the scope of a single Reference.
  98. *
  99. * @param Reference $reference
  100. * @param Definition $definition
  101. *
  102. * @throws ScopeWideningInjectionException when the definition references a service of a narrower scope
  103. * @throws ScopeCrossingInjectionException when the definition references a service of another scope hierarchy
  104. */
  105. private function validateScope(Reference $reference, Definition $definition = null)
  106. {
  107. if (ContainerInterface::SCOPE_PROTOTYPE === $this->currentScope) {
  108. return;
  109. }
  110. if (!$reference->isStrict(false)) {
  111. return;
  112. }
  113. if (null === $definition) {
  114. return;
  115. }
  116. if ($this->currentScope === $scope = $definition->getScope(false)) {
  117. return;
  118. }
  119. $id = (string) $reference;
  120. if (in_array($scope, $this->currentScopeChildren, true)) {
  121. throw new ScopeWideningInjectionException($this->currentId, $this->currentScope, $id, $scope);
  122. }
  123. if (!in_array($scope, $this->currentScopeAncestors, true)) {
  124. throw new ScopeCrossingInjectionException($this->currentId, $this->currentScope, $id, $scope);
  125. }
  126. }
  127. /**
  128. * Returns the Definition given an id.
  129. *
  130. * @param string $id Definition identifier
  131. *
  132. * @return Definition
  133. */
  134. private function getDefinition($id)
  135. {
  136. if (!$this->container->hasDefinition($id)) {
  137. return;
  138. }
  139. return $this->container->getDefinition($id);
  140. }
  141. }