[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/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\ContainerBuilder;
  15  use Symfony\Component\DependencyInjection\Definition;
  16  use Symfony\Component\DependencyInjection\DefinitionDecorator;
  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   * @author Nicolas Grekas <p@tchwork.com>
  25   */
  26  class ResolveDefinitionTemplatesPass implements CompilerPassInterface
  27  {
  28      private $compiler;
  29      private $formatter;
  30      private $currentId;
  31  
  32      /**
  33       * Process the ContainerBuilder to replace DefinitionDecorator instances with their real Definition instances.
  34       */
  35      public function process(ContainerBuilder $container)
  36      {
  37          $this->compiler = $container->getCompiler();
  38          $this->formatter = $this->compiler->getLoggingFormatter();
  39  
  40          $container->setDefinitions($this->resolveArguments($container, $container->getDefinitions(), true));
  41      }
  42  
  43      /**
  44       * Resolves definition decorator arguments.
  45       *
  46       * @param ContainerBuilder $container The ContainerBuilder
  47       * @param array            $arguments An array of arguments
  48       * @param bool             $isRoot    If we are processing the root definitions or not
  49       *
  50       * @return array
  51       */
  52      private function resolveArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
  53      {
  54          foreach ($arguments as $k => $argument) {
  55              if ($isRoot) {
  56                  // yes, we are specifically fetching the definition from the
  57                  // container to ensure we are not operating on stale data
  58                  $arguments[$k] = $argument = $container->getDefinition($k);
  59                  $this->currentId = $k;
  60              }
  61              if (\is_array($argument)) {
  62                  $arguments[$k] = $this->resolveArguments($container, $argument);
  63              } elseif ($argument instanceof Definition) {
  64                  if ($argument instanceof DefinitionDecorator) {
  65                      $arguments[$k] = $argument = $this->resolveDefinition($container, $argument);
  66                      if ($isRoot) {
  67                          $container->setDefinition($k, $argument);
  68                      }
  69                  }
  70                  $argument->setArguments($this->resolveArguments($container, $argument->getArguments()));
  71                  $argument->setMethodCalls($this->resolveArguments($container, $argument->getMethodCalls()));
  72                  $argument->setProperties($this->resolveArguments($container, $argument->getProperties()));
  73  
  74                  $configurator = $this->resolveArguments($container, array($argument->getConfigurator()));
  75                  $argument->setConfigurator($configurator[0]);
  76  
  77                  $factory = $this->resolveArguments($container, array($argument->getFactory()));
  78                  $argument->setFactory($factory[0]);
  79              }
  80          }
  81  
  82          return $arguments;
  83      }
  84  
  85      /**
  86       * Resolves the definition.
  87       *
  88       * @param ContainerBuilder    $container  The ContainerBuilder
  89       * @param DefinitionDecorator $definition
  90       *
  91       * @return Definition
  92       *
  93       * @throws \RuntimeException When the definition is invalid
  94       */
  95      private function resolveDefinition(ContainerBuilder $container, DefinitionDecorator $definition)
  96      {
  97          if (!$container->hasDefinition($parent = $definition->getParent())) {
  98              throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $this->currentId));
  99          }
 100  
 101          $parentDef = $container->getDefinition($parent);
 102          if ($parentDef instanceof DefinitionDecorator) {
 103              $id = $this->currentId;
 104              $this->currentId = $parent;
 105              $parentDef = $this->resolveDefinition($container, $parentDef);
 106              $container->setDefinition($parent, $parentDef);
 107              $this->currentId = $id;
 108          }
 109  
 110          $this->compiler->addLogMessage($this->formatter->formatResolveInheritance($this, $this->currentId, $parent));
 111          $def = new Definition();
 112  
 113          // merge in parent definition
 114          // purposely ignored attributes: scope, abstract, tags
 115          $def->setClass($parentDef->getClass());
 116          $def->setArguments($parentDef->getArguments());
 117          $def->setMethodCalls($parentDef->getMethodCalls());
 118          $def->setProperties($parentDef->getProperties());
 119          $def->setAutowiringTypes($parentDef->getAutowiringTypes());
 120          if ($parentDef->getFactoryClass(false)) {
 121              $def->setFactoryClass($parentDef->getFactoryClass(false));
 122          }
 123          if ($parentDef->getFactoryMethod(false)) {
 124              $def->setFactoryMethod($parentDef->getFactoryMethod(false));
 125          }
 126          if ($parentDef->getFactoryService(false)) {
 127              $def->setFactoryService($parentDef->getFactoryService(false));
 128          }
 129          if ($parentDef->isDeprecated()) {
 130              $def->setDeprecated(true, $parentDef->getDeprecationMessage('%service_id%'));
 131          }
 132          $def->setFactory($parentDef->getFactory());
 133          $def->setConfigurator($parentDef->getConfigurator());
 134          $def->setFile($parentDef->getFile());
 135          $def->setPublic($parentDef->isPublic());
 136          $def->setLazy($parentDef->isLazy());
 137          $def->setAutowired($parentDef->isAutowired());
 138  
 139          // overwrite with values specified in the decorator
 140          $changes = $definition->getChanges();
 141          if (isset($changes['class'])) {
 142              $def->setClass($definition->getClass());
 143          }
 144          if (isset($changes['factory_class'])) {
 145              $def->setFactoryClass($definition->getFactoryClass(false));
 146          }
 147          if (isset($changes['factory_method'])) {
 148              $def->setFactoryMethod($definition->getFactoryMethod(false));
 149          }
 150          if (isset($changes['factory_service'])) {
 151              $def->setFactoryService($definition->getFactoryService(false));
 152          }
 153          if (isset($changes['factory'])) {
 154              $def->setFactory($definition->getFactory());
 155          }
 156          if (isset($changes['configurator'])) {
 157              $def->setConfigurator($definition->getConfigurator());
 158          }
 159          if (isset($changes['file'])) {
 160              $def->setFile($definition->getFile());
 161          }
 162          if (isset($changes['public'])) {
 163              $def->setPublic($definition->isPublic());
 164          }
 165          if (isset($changes['lazy'])) {
 166              $def->setLazy($definition->isLazy());
 167          }
 168          if (isset($changes['deprecated'])) {
 169              $def->setDeprecated($definition->isDeprecated(), $definition->getDeprecationMessage('%service_id%'));
 170          }
 171          if (isset($changes['autowire'])) {
 172              $def->setAutowired($definition->isAutowired());
 173          }
 174          if (isset($changes['decorated_service'])) {
 175              $decoratedService = $definition->getDecoratedService();
 176              if (null === $decoratedService) {
 177                  $def->setDecoratedService($decoratedService);
 178              } else {
 179                  $def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
 180              }
 181          }
 182  
 183          // merge arguments
 184          foreach ($definition->getArguments() as $k => $v) {
 185              if (is_numeric($k)) {
 186                  $def->addArgument($v);
 187                  continue;
 188              }
 189  
 190              if (0 !== strpos($k, 'index_')) {
 191                  throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
 192              }
 193  
 194              $index = (int) substr($k, \strlen('index_'));
 195              $def->replaceArgument($index, $v);
 196          }
 197  
 198          // merge properties
 199          foreach ($definition->getProperties() as $k => $v) {
 200              $def->setProperty($k, $v);
 201          }
 202  
 203          // append method calls
 204          if (\count($calls = $definition->getMethodCalls()) > 0) {
 205              $def->setMethodCalls(array_merge($def->getMethodCalls(), $calls));
 206          }
 207  
 208          // merge autowiring types
 209          foreach ($definition->getAutowiringTypes() as $autowiringType) {
 210              $def->addAutowiringType($autowiringType);
 211          }
 212  
 213          // these attributes are always taken from the child
 214          $def->setAbstract($definition->isAbstract());
 215          $def->setScope($definition->getScope(false), false);
 216          $def->setShared($definition->isShared());
 217          $def->setTags($definition->getTags());
 218  
 219          return $def;
 220      }
 221  }


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