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\Exception\ServiceNotFoundException;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Reference;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 * Checks that all references are pointing to a valid service.
23 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
25 class CheckExceptionOnInvalidReferenceBehaviorPass implements CompilerPassInterface
30 public function process(ContainerBuilder $container)
32 $this->container = $container;
34 foreach ($container->getDefinitions() as $id => $definition) {
35 $this->sourceId = $id;
36 $this->processDefinition($definition);
40 private function processDefinition(Definition $definition)
42 $this->processReferences($definition->getArguments());
43 $this->processReferences($definition->getMethodCalls());
44 $this->processReferences($definition->getProperties());
47 private function processReferences(array $arguments)
49 foreach ($arguments as $argument) {
50 if (is_array($argument)) {
51 $this->processReferences($argument);
52 } elseif ($argument instanceof Definition) {
53 $this->processDefinition($argument);
54 } elseif ($argument instanceof Reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $argument->getInvalidBehavior()) {
55 $destId = (string) $argument;
57 if (!$this->container->has($destId)) {
58 throw new ServiceNotFoundException($destId, $this->sourceId);