CheckReferenceValidityPass.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\Reference;
  13. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  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. * @autor ThurData <info@thurdata.ch>
  21. */
  22. class CheckReferenceValidityPass extends AbstractRecursivePass
  23. {
  24. protected function processValue($value, $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(
  33. 'The definition "%s" has a reference to an abstract definition "%s". '
  34. .'Abstract definitions cannot be the target of references.',
  35. $this->currentId,
  36. $value
  37. ));
  38. }
  39. }
  40. return parent::processValue($value, $isRoot);
  41. }
  42. }