[ Index ]

PHP Cross Reference of phpBB-3.3.14-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\Argument\ArgumentInterface;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\ContainerInterface;
  17  use Symfony\Component\DependencyInjection\Definition;
  18  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  19  use Symfony\Component\DependencyInjection\ExpressionLanguage;
  20  use Symfony\Component\DependencyInjection\Reference;
  21  use Symfony\Component\ExpressionLanguage\Expression;
  22  
  23  /**
  24   * Run this pass before passes that need to know more about the relation of
  25   * your services.
  26   *
  27   * This class will populate the ServiceReferenceGraph with information. You can
  28   * retrieve the graph in other passes from the compiler.
  29   *
  30   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31   */
  32  class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
  33  {
  34      private $graph;
  35      private $currentDefinition;
  36      private $onlyConstructorArguments;
  37      private $hasProxyDumper;
  38      private $lazy;
  39      private $expressionLanguage;
  40      private $byConstructor;
  41  
  42      /**
  43       * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  44       */
  45      public function __construct($onlyConstructorArguments = false, $hasProxyDumper = true)
  46      {
  47          $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
  48          $this->hasProxyDumper = (bool) $hasProxyDumper;
  49      }
  50  
  51      /**
  52       * {@inheritdoc}
  53       */
  54      public function setRepeatedPass(RepeatedPass $repeatedPass)
  55      {
  56          // no-op for BC
  57      }
  58  
  59      /**
  60       * Processes a ContainerBuilder object to populate the service reference graph.
  61       */
  62      public function process(ContainerBuilder $container)
  63      {
  64          $this->container = $container;
  65          $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  66          $this->graph->clear();
  67          $this->lazy = false;
  68          $this->byConstructor = false;
  69  
  70          foreach ($container->getAliases() as $id => $alias) {
  71              $targetId = $this->getDefinitionId((string) $alias);
  72              $this->graph->connect($id, $alias, $targetId, $this->getDefinition($targetId), null);
  73          }
  74  
  75          parent::process($container);
  76      }
  77  
  78      protected function processValue($value, $isRoot = false)
  79      {
  80          $lazy = $this->lazy;
  81  
  82          if ($value instanceof ArgumentInterface) {
  83              $this->lazy = true;
  84              parent::processValue($value->getValues());
  85              $this->lazy = $lazy;
  86  
  87              return $value;
  88          }
  89          if ($value instanceof Expression) {
  90              $this->getExpressionLanguage()->compile((string) $value, ['this' => 'container']);
  91  
  92              return $value;
  93          }
  94          if ($value instanceof Reference) {
  95              $targetId = $this->getDefinitionId((string) $value);
  96              $targetDefinition = $this->getDefinition($targetId);
  97  
  98              $this->graph->connect(
  99                  $this->currentId,
 100                  $this->currentDefinition,
 101                  $targetId,
 102                  $targetDefinition,
 103                  $value,
 104                  $this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
 105                  ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior(),
 106                  $this->byConstructor
 107              );
 108  
 109              return $value;
 110          }
 111          if (!$value instanceof Definition) {
 112              return parent::processValue($value, $isRoot);
 113          }
 114          if ($isRoot) {
 115              if ($value->isSynthetic() || $value->isAbstract()) {
 116                  return $value;
 117              }
 118              $this->currentDefinition = $value;
 119          } elseif ($this->currentDefinition === $value) {
 120              return $value;
 121          }
 122          $this->lazy = false;
 123  
 124          $byConstructor = $this->byConstructor;
 125          $this->byConstructor = $isRoot || $byConstructor;
 126          $this->processValue($value->getFactory());
 127          $this->processValue($value->getArguments());
 128          $this->byConstructor = $byConstructor;
 129  
 130          if (!$this->onlyConstructorArguments) {
 131              $this->processValue($value->getProperties());
 132              $this->processValue($value->getMethodCalls());
 133              $this->processValue($value->getConfigurator());
 134          }
 135          $this->lazy = $lazy;
 136  
 137          return $value;
 138      }
 139  
 140      /**
 141       * Returns a service definition given the full name or an alias.
 142       *
 143       * @param string $id A full id or alias for a service definition
 144       *
 145       * @return Definition|null The definition related to the supplied id
 146       */
 147      private function getDefinition($id)
 148      {
 149          return null === $id ? null : $this->container->getDefinition($id);
 150      }
 151  
 152      private function getDefinitionId($id)
 153      {
 154          while ($this->container->hasAlias($id)) {
 155              $id = (string) $this->container->getAlias($id);
 156          }
 157  
 158          if (!$this->container->hasDefinition($id)) {
 159              return null;
 160          }
 161  
 162          return $this->container->normalizeId($id);
 163      }
 164  
 165      private function getExpressionLanguage()
 166      {
 167          if (null === $this->expressionLanguage) {
 168              if (!class_exists(ExpressionLanguage::class)) {
 169                  throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
 170              }
 171  
 172              $providers = $this->container->getExpressionLanguageProviders();
 173              $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
 174                  if ('""' === substr_replace($arg, '', 1, -1)) {
 175                      $id = stripcslashes(substr($arg, 1, -1));
 176                      $id = $this->getDefinitionId($id);
 177  
 178                      $this->graph->connect(
 179                          $this->currentId,
 180                          $this->currentDefinition,
 181                          $id,
 182                          $this->getDefinition($id)
 183                      );
 184                  }
 185  
 186                  return sprintf('$this->get(%s)', $arg);
 187              });
 188          }
 189  
 190          return $this->expressionLanguage;
 191      }
 192  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1