[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> AnalyzeServiceReferencesPass.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\ContainerBuilder;
  15  use Symfony\Component\DependencyInjection\Definition;
  16  use Symfony\Component\DependencyInjection\Reference;
  17  
  18  /**
  19   * Run this pass before passes that need to know more about the relation of
  20   * your services.
  21   *
  22   * This class will populate the ServiceReferenceGraph with information. You can
  23   * retrieve the graph in other passes from the compiler.
  24   *
  25   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26   */
  27  class AnalyzeServiceReferencesPass implements RepeatablePassInterface
  28  {
  29      private $graph;
  30      private $container;
  31      private $currentId;
  32      private $currentDefinition;
  33      private $onlyConstructorArguments;
  34  
  35      /**
  36       * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  37       */
  38      public function __construct($onlyConstructorArguments = false)
  39      {
  40          $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
  41      }
  42  
  43      /**
  44       * {@inheritdoc}
  45       */
  46      public function setRepeatedPass(RepeatedPass $repeatedPass)
  47      {
  48          // no-op for BC
  49      }
  50  
  51      /**
  52       * Processes a ContainerBuilder object to populate the service reference graph.
  53       */
  54      public function process(ContainerBuilder $container)
  55      {
  56          $this->container = $container;
  57          $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  58          $this->graph->clear();
  59  
  60          foreach ($container->getDefinitions() as $id => $definition) {
  61              if ($definition->isSynthetic() || $definition->isAbstract()) {
  62                  continue;
  63              }
  64  
  65              $this->currentId = $id;
  66              $this->currentDefinition = $definition;
  67  
  68              $this->processArguments($definition->getArguments());
  69              if ($definition->getFactoryService(false)) {
  70                  $this->processArguments(array(new Reference($definition->getFactoryService(false))));
  71              }
  72              if (\is_array($definition->getFactory())) {
  73                  $this->processArguments($definition->getFactory());
  74              }
  75  
  76              if (!$this->onlyConstructorArguments) {
  77                  $this->processArguments($definition->getMethodCalls());
  78                  $this->processArguments($definition->getProperties());
  79                  if ($definition->getConfigurator()) {
  80                      $this->processArguments(array($definition->getConfigurator()));
  81                  }
  82              }
  83          }
  84  
  85          foreach ($container->getAliases() as $id => $alias) {
  86              $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
  87          }
  88      }
  89  
  90      /**
  91       * Processes service definitions for arguments to find relationships for the service graph.
  92       *
  93       * @param array $arguments An array of Reference or Definition objects relating to service definitions
  94       */
  95      private function processArguments(array $arguments)
  96      {
  97          foreach ($arguments as $argument) {
  98              if (\is_array($argument)) {
  99                  $this->processArguments($argument);
 100              } elseif ($argument instanceof Reference) {
 101                  $this->graph->connect(
 102                      $this->currentId,
 103                      $this->currentDefinition,
 104                      $this->getDefinitionId((string) $argument),
 105                      $this->getDefinition((string) $argument),
 106                      $argument
 107                  );
 108              } elseif ($argument instanceof Definition) {
 109                  $this->processArguments($argument->getArguments());
 110                  $this->processArguments($argument->getMethodCalls());
 111                  $this->processArguments($argument->getProperties());
 112  
 113                  if (\is_array($argument->getFactory())) {
 114                      $this->processArguments($argument->getFactory());
 115                  }
 116                  if ($argument->getFactoryService(false)) {
 117                      $this->processArguments(array(new Reference($argument->getFactoryService(false))));
 118                  }
 119              }
 120          }
 121      }
 122  
 123      /**
 124       * Returns a service definition given the full name or an alias.
 125       *
 126       * @param string $id A full id or alias for a service definition
 127       *
 128       * @return Definition|null The definition related to the supplied id
 129       */
 130      private function getDefinition($id)
 131      {
 132          $id = $this->getDefinitionId($id);
 133  
 134          return null === $id ? null : $this->container->getDefinition($id);
 135      }
 136  
 137      private function getDefinitionId($id)
 138      {
 139          while ($this->container->hasAlias($id)) {
 140              $id = (string) $this->container->getAlias($id);
 141          }
 142  
 143          if (!$this->container->hasDefinition($id)) {
 144              return;
 145          }
 146  
 147          return $id;
 148      }
 149  }


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