[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Compiler/ -> ResolveDefinitionTemplatesPass.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\Definition;
  15  use Symfony\Component\DependencyInjection\DefinitionDecorator;
  16  use Symfony\Component\DependencyInjection\ContainerBuilder;
  17  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  18  
  19  /**
  20   * This replaces all DefinitionDecorator instances with their equivalent fully
  21   * merged Definition instance.
  22   *
  23   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  24   */
  25  class ResolveDefinitionTemplatesPass implements CompilerPassInterface
  26  {
  27      private $container;
  28      private $compiler;
  29      private $formatter;
  30  
  31      /**
  32       * Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
  33       *
  34       * @param ContainerBuilder $container
  35       */
  36      public function process(ContainerBuilder $container)
  37      {
  38          $this->container = $container;
  39          $this->compiler = $container->getCompiler();
  40          $this->formatter = $this->compiler->getLoggingFormatter();
  41  
  42          foreach ($container->getDefinitions() as $id => $definition) {
  43              // yes, we are specifically fetching the definition from the
  44              // container to ensure we are not operating on stale data
  45              $definition = $container->getDefinition($id);
  46              if (!$definition instanceof DefinitionDecorator || $definition->isAbstract()) {
  47                  continue;
  48              }
  49  
  50              $this->resolveDefinition($id, $definition);
  51          }
  52      }
  53  
  54      /**
  55       * Resolves the definition.
  56       *
  57       * @param string              $id         The definition identifier
  58       * @param DefinitionDecorator $definition
  59       *
  60       * @return Definition
  61       *
  62       * @throws \RuntimeException When the definition is invalid
  63       */
  64      private function resolveDefinition($id, DefinitionDecorator $definition)
  65      {
  66          if (!$this->container->hasDefinition($parent = $definition->getParent())) {
  67              throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
  68          }
  69  
  70          $parentDef = $this->container->getDefinition($parent);
  71          if ($parentDef instanceof DefinitionDecorator) {
  72              $parentDef = $this->resolveDefinition($parent, $parentDef);
  73          }
  74  
  75          $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $id, $parent));
  76          $def = new Definition();
  77  
  78          // merge in parent definition
  79          // purposely ignored attributes: scope, abstract, tags
  80          $def->setClass($parentDef->getClass());
  81          $def->setArguments($parentDef->getArguments());
  82          $def->setMethodCalls($parentDef->getMethodCalls());
  83          $def->setProperties($parentDef->getProperties());
  84          $def->setFactoryClass($parentDef->getFactoryClass());
  85          $def->setFactoryMethod($parentDef->getFactoryMethod());
  86          $def->setFactoryService($parentDef->getFactoryService());
  87          $def->setConfigurator($parentDef->getConfigurator());
  88          $def->setFile($parentDef->getFile());
  89          $def->setPublic($parentDef->isPublic());
  90          $def->setLazy($parentDef->isLazy());
  91  
  92          // overwrite with values specified in the decorator
  93          $changes = $definition->getChanges();
  94          if (isset($changes['class'])) {
  95              $def->setClass($definition->getClass());
  96          }
  97          if (isset($changes['factory_class'])) {
  98              $def->setFactoryClass($definition->getFactoryClass());
  99          }
 100          if (isset($changes['factory_method'])) {
 101              $def->setFactoryMethod($definition->getFactoryMethod());
 102          }
 103          if (isset($changes['factory_service'])) {
 104              $def->setFactoryService($definition->getFactoryService());
 105          }
 106          if (isset($changes['configurator'])) {
 107              $def->setConfigurator($definition->getConfigurator());
 108          }
 109          if (isset($changes['file'])) {
 110              $def->setFile($definition->getFile());
 111          }
 112          if (isset($changes['public'])) {
 113              $def->setPublic($definition->isPublic());
 114          }
 115          if (isset($changes['lazy'])) {
 116              $def->setLazy($definition->isLazy());
 117          }
 118  
 119          // merge arguments
 120          foreach ($definition->getArguments() as $k => $v) {
 121              if (is_numeric($k)) {
 122                  $def->addArgument($v);
 123                  continue;
 124              }
 125  
 126              if (0 !== strpos($k, 'index_')) {
 127                  throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
 128              }
 129  
 130              $index = (int) substr($k, strlen('index_'));
 131              $def->replaceArgument($index, $v);
 132          }
 133  
 134          // merge properties
 135          foreach ($definition->getProperties() as $k => $v) {
 136              $def->setProperty($k, $v);
 137          }
 138  
 139          // append method calls
 140          if (count($calls = $definition->getMethodCalls()) > 0) {
 141              $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
 142          }
 143  
 144          // these attributes are always taken from the child
 145          $def->setAbstract($definition->isAbstract());
 146          $def->setScope($definition->getScope());
 147          $def->setTags($definition->getTags());
 148  
 149          // set new definition on container
 150          $this->container->setDefinition($id, $def);
 151  
 152          return $def;
 153      }
 154  }


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