4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Definition;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
20 * Checks the validity of references.
22 * The following checks are performed by this pass:
23 * - target definitions are not abstract
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
27 class CheckReferenceValidityPass implements CompilerPassInterface
33 * Processes the ContainerBuilder to validate References.
35 * @param ContainerBuilder $container
37 public function process(ContainerBuilder $container)
39 $this->container = $container;
41 foreach ($container->getDefinitions() as $id => $definition) {
42 if ($definition->isSynthetic() || $definition->isAbstract()) {
46 $this->currentId = $id;
48 $this->validateReferences($definition->getArguments());
49 $this->validateReferences($definition->getMethodCalls());
50 $this->validateReferences($definition->getProperties());
55 * Validates an array of References.
57 * @param array $arguments An array of Reference objects
59 * @throws RuntimeException when there is a reference to an abstract definition.
61 private function validateReferences(array $arguments)
63 foreach ($arguments as $argument) {
64 if (is_array($argument)) {
65 $this->validateReferences($argument);
66 } elseif ($argument instanceof Reference) {
67 $targetDefinition = $this->getDefinition((string) $argument);
69 if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
70 throw new RuntimeException(sprintf(
71 'The definition "%s" has a reference to an abstract definition "%s". '
72 .'Abstract definitions cannot be the target of references.',
82 * Returns the Definition given an id.
84 * @param string $id Definition identifier
88 private function getDefinition($id)
90 if (!$this->container->hasDefinition($id)) {
94 return $this->container->getDefinition($id);