[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1