DefinitionErrorExceptionPass.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Throws an exception for any Definitions that have errors and still exist.
  17. *
  18. * @author Ryan Weaver <ryan@knpuniversity.com>
  19. */
  20. class DefinitionErrorExceptionPass extends AbstractRecursivePass
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function processValue($value, bool $isRoot = false)
  26. {
  27. if (!$value instanceof Definition || !$value->hasErrors()) {
  28. return parent::processValue($value, $isRoot);
  29. }
  30. if ($isRoot && !$value->isPublic()) {
  31. $graph = $this->container->getCompiler()->getServiceReferenceGraph();
  32. $runtimeException = false;
  33. foreach ($graph->getNode($this->currentId)->getInEdges() as $edge) {
  34. if (!$edge->getValue() instanceof Reference || ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE !== $edge->getValue()->getInvalidBehavior()) {
  35. $runtimeException = false;
  36. break;
  37. }
  38. $runtimeException = true;
  39. }
  40. if ($runtimeException) {
  41. return parent::processValue($value, $isRoot);
  42. }
  43. }
  44. // only show the first error so the user can focus on it
  45. $errors = $value->getErrors();
  46. $message = reset($errors);
  47. throw new RuntimeException($message);
  48. }
  49. }