CheckExceptionOnInvalidReferenceBehaviorPass.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Exception\ServiceNotFoundException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Checks that all references are pointing to a valid service.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class CheckExceptionOnInvalidReferenceBehaviorPass extends AbstractRecursivePass
  21. {
  22. private $serviceLocatorContextIds = [];
  23. public function process(ContainerBuilder $container)
  24. {
  25. $this->serviceLocatorContextIds = [];
  26. foreach ($container->findTaggedServiceIds('container.service_locator_context') as $id => $tags) {
  27. $this->serviceLocatorContextIds[$id] = $tags[0]['id'];
  28. $container->getDefinition($id)->clearTag('container.service_locator_context');
  29. }
  30. try {
  31. return parent::process($container);
  32. } finally {
  33. $this->serviceLocatorContextIds = [];
  34. }
  35. }
  36. protected function processValue($value, bool $isRoot = false)
  37. {
  38. if (!$value instanceof Reference) {
  39. return parent::processValue($value, $isRoot);
  40. }
  41. if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $value->getInvalidBehavior() || $this->container->has($id = (string) $value)) {
  42. return $value;
  43. }
  44. $currentId = $this->currentId;
  45. $graph = $this->container->getCompiler()->getServiceReferenceGraph();
  46. if (isset($this->serviceLocatorContextIds[$currentId])) {
  47. $currentId = $this->serviceLocatorContextIds[$currentId];
  48. $locator = $this->container->getDefinition($this->currentId)->getFactory()[0];
  49. $this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
  50. }
  51. if ('.' === $currentId[0] && $graph->hasNode($currentId)) {
  52. foreach ($graph->getNode($currentId)->getInEdges() as $edge) {
  53. if (!$edge->getValue() instanceof Reference || ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $edge->getValue()->getInvalidBehavior()) {
  54. continue;
  55. }
  56. $sourceId = $edge->getSourceNode()->getId();
  57. if ('.' !== $sourceId[0]) {
  58. $currentId = $sourceId;
  59. break;
  60. }
  61. if (isset($this->serviceLocatorContextIds[$sourceId])) {
  62. $currentId = $this->serviceLocatorContextIds[$sourceId];
  63. $locator = $this->container->getDefinition($this->currentId);
  64. $this->throwServiceNotFoundException($value, $currentId, $locator->getArgument(0));
  65. }
  66. }
  67. }
  68. $this->throwServiceNotFoundException($value, $currentId, $value);
  69. }
  70. private function throwServiceNotFoundException(Reference $ref, string $sourceId, $value): void
  71. {
  72. $id = (string) $ref;
  73. $alternatives = [];
  74. foreach ($this->container->getServiceIds() as $knownId) {
  75. if ('' === $knownId || '.' === $knownId[0]) {
  76. continue;
  77. }
  78. $lev = levenshtein($id, $knownId);
  79. if ($lev <= \strlen($id) / 3 || false !== strpos($knownId, $id)) {
  80. $alternatives[] = $knownId;
  81. }
  82. }
  83. $pass = new class() extends AbstractRecursivePass {
  84. public $ref;
  85. public $sourceId;
  86. public $alternatives;
  87. /**
  88. * @return mixed
  89. */
  90. public function processValue($value, bool $isRoot = false)
  91. {
  92. if ($this->ref !== $value) {
  93. return parent::processValue($value, $isRoot);
  94. }
  95. $sourceId = $this->sourceId;
  96. if (null !== $this->currentId && $this->currentId !== (string) $value) {
  97. $sourceId = $this->currentId.'" in the container provided to "'.$sourceId;
  98. }
  99. throw new ServiceNotFoundException((string) $value, $sourceId, null, $this->alternatives);
  100. }
  101. };
  102. $pass->ref = $ref;
  103. $pass->sourceId = $sourceId;
  104. $pass->alternatives = $alternatives;
  105. $pass->processValue($value, true);
  106. }
  107. }