[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> InlineServiceDefinitionsPass.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\ContainerInterface;
  16  use Symfony\Component\DependencyInjection\Definition;
  17  use Symfony\Component\DependencyInjection\Reference;
  18  
  19  /**
  20   * Inline service definitions where this is possible.
  21   *
  22   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  23   */
  24  class InlineServiceDefinitionsPass implements RepeatablePassInterface
  25  {
  26      private $graph;
  27      private $compiler;
  28      private $formatter;
  29      private $currentId;
  30  
  31      /**
  32       * {@inheritdoc}
  33       */
  34      public function setRepeatedPass(RepeatedPass $repeatedPass)
  35      {
  36          // no-op for BC
  37      }
  38  
  39      /**
  40       * Processes the ContainerBuilder for inline service definitions.
  41       */
  42      public function process(ContainerBuilder $container)
  43      {
  44          $this->compiler = $container->getCompiler();
  45          $this->formatter = $this->compiler->getLoggingFormatter();
  46          $this->graph = $this->compiler->getServiceReferenceGraph();
  47  
  48          $container->setDefinitions($this->inlineArguments($container, $container->getDefinitions(), true));
  49      }
  50  
  51      /**
  52       * Processes inline arguments.
  53       *
  54       * @param ContainerBuilder $container The ContainerBuilder
  55       * @param array            $arguments An array of arguments
  56       * @param bool             $isRoot    If we are processing the root definitions or not
  57       *
  58       * @return array
  59       */
  60      private function inlineArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
  61      {
  62          foreach ($arguments as $k => $argument) {
  63              if ($isRoot) {
  64                  $this->currentId = $k;
  65              }
  66              if (\is_array($argument)) {
  67                  $arguments[$k] = $this->inlineArguments($container, $argument);
  68              } elseif ($argument instanceof Reference) {
  69                  if (!$container->hasDefinition($id = (string) $argument)) {
  70                      continue;
  71                  }
  72  
  73                  if ($this->isInlineableDefinition($container, $id, $definition = $container->getDefinition($id))) {
  74                      $this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
  75  
  76                      if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope(false)) {
  77                          $arguments[$k] = $definition;
  78                      } else {
  79                          $arguments[$k] = clone $definition;
  80                      }
  81                  }
  82              } elseif ($argument instanceof Definition) {
  83                  $argument->setArguments($this->inlineArguments($container, $argument->getArguments()));
  84                  $argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls()));
  85                  $argument->setProperties($this->inlineArguments($container, $argument->getProperties()));
  86  
  87                  $configurator = $this->inlineArguments($container, array($argument->getConfigurator()));
  88                  $argument->setConfigurator($configurator[0]);
  89  
  90                  $factory = $this->inlineArguments($container, array($argument->getFactory()));
  91                  $argument->setFactory($factory[0]);
  92              }
  93          }
  94  
  95          return $arguments;
  96      }
  97  
  98      /**
  99       * Checks if the definition is inlineable.
 100       *
 101       * @param ContainerBuilder $container
 102       * @param string           $id
 103       * @param Definition       $definition
 104       *
 105       * @return bool If the definition is inlineable
 106       */
 107      private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
 108      {
 109          if ($definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
 110              return false;
 111          }
 112  
 113          if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
 114              return true;
 115          }
 116  
 117          if ($definition->isPublic()) {
 118              return false;
 119          }
 120  
 121          if (!$this->graph->hasNode($id)) {
 122              return true;
 123          }
 124  
 125          if ($this->currentId == $id) {
 126              return false;
 127          }
 128  
 129          $ids = array();
 130          foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
 131              $ids[] = $edge->getSourceNode()->getId();
 132          }
 133  
 134          if (\count(array_unique($ids)) > 1) {
 135              return false;
 136          }
 137  
 138          if (\count($ids) > 1 && \is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
 139              return false;
 140          }
 141  
 142          if (\count($ids) > 1 && $definition->getFactoryService(false)) {
 143              return false;
 144          }
 145  
 146          return $container->getDefinition(reset($ids))->getScope(false) === $definition->getScope(false);
 147      }
 148  }


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