[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Compiler/ -> RemoveUnusedDefinitionsPass.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  
  16  /**
  17   * Removes unused service definitions from the container.
  18   *
  19   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20   */
  21  class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
  22  {
  23      private $repeatedPass;
  24  
  25      /**
  26       * {@inheritdoc}
  27       */
  28      public function setRepeatedPass(RepeatedPass $repeatedPass)
  29      {
  30          $this->repeatedPass = $repeatedPass;
  31      }
  32  
  33      /**
  34       * Processes the ContainerBuilder to remove unused definitions.
  35       *
  36       * @param ContainerBuilder $container
  37       */
  38      public function process(ContainerBuilder $container)
  39      {
  40          $compiler = $container->getCompiler();
  41          $formatter = $compiler->getLoggingFormatter();
  42          $graph = $compiler->getServiceReferenceGraph();
  43  
  44          $hasChanged = false;
  45          foreach ($container->getDefinitions() as $id => $definition) {
  46              if ($definition->isPublic()) {
  47                  continue;
  48              }
  49  
  50              if ($graph->hasNode($id)) {
  51                  $edges = $graph->getNode($id)->getInEdges();
  52                  $referencingAliases = array();
  53                  $sourceIds = array();
  54                  foreach ($edges as $edge) {
  55                      $node = $edge->getSourceNode();
  56                      $sourceIds[] = $node->getId();
  57  
  58                      if ($node->isAlias()) {
  59                          $referencingAliases[] = $node->getValue();
  60                      }
  61                  }
  62                  $isReferenced = (count(array_unique($sourceIds)) - count($referencingAliases)) > 0;
  63              } else {
  64                  $referencingAliases = array();
  65                  $isReferenced = false;
  66              }
  67  
  68              if (1 === count($referencingAliases) && false === $isReferenced) {
  69                  $container->setDefinition((string) reset($referencingAliases), $definition);
  70                  $definition->setPublic(true);
  71                  $container->removeDefinition($id);
  72                  $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias '.reset($referencingAliases)));
  73              } elseif (0 === count($referencingAliases) && false === $isReferenced) {
  74                  $container->removeDefinition($id);
  75                  $compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused'));
  76                  $hasChanged = true;
  77              }
  78          }
  79  
  80          if ($hasChanged) {
  81              $this->repeatedPass->setRepeat();
  82          }
  83      }
  84  }


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