vendor/symfony/src/Symfony/Component/DependencyInjection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony framework.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * This source file is subject to the MIT license that is bundled
       
     9  * with this source code in the file LICENSE.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\DependencyInjection\Compiler;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\Definition;
       
    15 
       
    16 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
       
    17 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    18 use Symfony\Component\DependencyInjection\Reference;
       
    19 use Symfony\Component\DependencyInjection\ContainerBuilder;
       
    20 
       
    21 /**
       
    22  * Checks that all references are pointing to a valid service.
       
    23  *
       
    24  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
       
    25  */
       
    26 class CheckExceptionOnInvalidReferenceBehaviorPass implements CompilerPassInterface
       
    27 {
       
    28     private $container;
       
    29     private $sourceId;
       
    30 
       
    31     public function process(ContainerBuilder $container)
       
    32     {
       
    33         $this->container = $container;
       
    34 
       
    35         foreach ($container->getDefinitions() as $id => $definition) {
       
    36             $this->sourceId = $id;
       
    37             $this->processDefinition($definition);
       
    38         }
       
    39     }
       
    40 
       
    41     private function processDefinition(Definition $definition)
       
    42     {
       
    43         $this->processReferences($definition->getArguments());
       
    44         $this->processReferences($definition->getMethodCalls());
       
    45         $this->processReferences($definition->getProperties());
       
    46     }
       
    47 
       
    48     private function processReferences(array $arguments)
       
    49     {
       
    50         foreach ($arguments as $argument) {
       
    51             if (is_array($argument)) {
       
    52                 $this->processReferences($argument);
       
    53             } else if ($argument instanceof Definition) {
       
    54                 $this->processDefinition($argument);
       
    55             } else if ($argument instanceof Reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $argument->getInvalidBehavior()) {
       
    56                 $destId = (string) $argument;
       
    57 
       
    58                 if (!$this->container->has($destId)) {
       
    59                     throw new ServiceNotFoundException($destId, $this->sourceId);
       
    60                 }
       
    61             }
       
    62         }
       
    63     }
       
    64 }