CheckReferenceValidityPass.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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\Exception\RuntimeException;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Checks the validity of references.
  16. *
  17. * The following checks are performed by this pass:
  18. * - target definitions are not abstract
  19. *
  20. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  21. */
  22. class CheckReferenceValidityPass extends AbstractRecursivePass
  23. {
  24. protected function processValue($value, bool $isRoot = false)
  25. {
  26. if ($isRoot && $value instanceof Definition && ($value->isSynthetic() || $value->isAbstract())) {
  27. return $value;
  28. }
  29. if ($value instanceof Reference && $this->container->hasDefinition((string) $value)) {
  30. $targetDefinition = $this->container->getDefinition((string) $value);
  31. if ($targetDefinition->isAbstract()) {
  32. 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, $value));
  33. }
  34. }
  35. return parent::processValue($value, $isRoot);
  36. }
  37. }