[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> ResolveReferencesToAliasesPass.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\DependencyInjection\Compiler;
  13  
  14  use Symfony\Component\DependencyInjection\Alias;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  17  use Symfony\Component\DependencyInjection\Reference;
  18  
  19  /**
  20   * Replaces all references to aliases with references to the actual service.
  21   *
  22   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23   */
  24  class ResolveReferencesToAliasesPass implements CompilerPassInterface
  25  {
  26      private $container;
  27  
  28      /**
  29       * Processes the ContainerBuilder to replace references to aliases with actual service references.
  30       */
  31      public function process(ContainerBuilder $container)
  32      {
  33          $this->container = $container;
  34  
  35          foreach ($container->getDefinitions() as $definition) {
  36              if ($definition->isSynthetic() || $definition->isAbstract()) {
  37                  continue;
  38              }
  39  
  40              $definition->setArguments($this->processArguments($definition->getArguments()));
  41              $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
  42              $definition->setProperties($this->processArguments($definition->getProperties()));
  43              $definition->setFactory($this->processFactory($definition->getFactory()));
  44              $definition->setFactoryService($this->processFactoryService($definition->getFactoryService(false)), false);
  45          }
  46  
  47          foreach ($container->getAliases() as $id => $alias) {
  48              $aliasId = (string) $alias;
  49              if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
  50                  $container->setAlias($id, new Alias($defId, $alias->isPublic()));
  51              }
  52          }
  53      }
  54  
  55      /**
  56       * Processes the arguments to replace aliases.
  57       *
  58       * @param array $arguments An array of References
  59       *
  60       * @return array An array of References
  61       */
  62      private function processArguments(array $arguments)
  63      {
  64          foreach ($arguments as $k => $argument) {
  65              if (\is_array($argument)) {
  66                  $arguments[$k] = $this->processArguments($argument);
  67              } elseif ($argument instanceof Reference) {
  68                  $defId = $this->getDefinitionId($id = (string) $argument);
  69  
  70                  if ($defId !== $id) {
  71                      $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
  72                  }
  73              }
  74          }
  75  
  76          return $arguments;
  77      }
  78  
  79      private function processFactoryService($factoryService)
  80      {
  81          if (null === $factoryService) {
  82              return;
  83          }
  84  
  85          return $this->getDefinitionId($factoryService);
  86      }
  87  
  88      private function processFactory($factory)
  89      {
  90          if (null === $factory || !\is_array($factory) || !$factory[0] instanceof Reference) {
  91              return $factory;
  92          }
  93  
  94          $defId = $this->getDefinitionId($id = (string) $factory[0]);
  95  
  96          if ($defId !== $id) {
  97              $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
  98          }
  99  
 100          return $factory;
 101      }
 102  
 103      /**
 104       * Resolves an alias into a definition id.
 105       *
 106       * @param string $id The definition or alias id to resolve
 107       *
 108       * @return string The definition id with aliases resolved
 109       */
 110      private function getDefinitionId($id)
 111      {
 112          $seen = array();
 113          while ($this->container->hasAlias($id)) {
 114              if (isset($seen[$id])) {
 115                  throw new ServiceCircularReferenceException($id, array_keys($seen));
 116              }
 117              $seen[$id] = true;
 118              $id = (string) $this->container->getAlias($id);
 119          }
 120  
 121          return $id;
 122      }
 123  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1