Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ReplaceAliasByActualDefinitionPass.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 5.58 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\DependencyInjection\Compiler;
 013   
 014  use Symfony\Component\DependencyInjection\ContainerBuilder;
 015  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
 016  use Symfony\Component\DependencyInjection\Reference;
 017   
 018  /**
 019   * Replaces aliases with actual service definitions, effectively removing these
 020   * aliases.
 021   *
 022   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 023   */
 024  class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
 025  {
 026      private $compiler;
 027      private $formatter;
 028   
 029      /**
 030       * Process the Container to replace aliases with service definitions.
 031       *
 032       * @param ContainerBuilder $container
 033       *
 034       * @throws InvalidArgumentException if the service definition does not exist
 035       */
 036      public function process(ContainerBuilder $container)
 037      {
 038          // Setup
 039          $this->compiler = $container->getCompiler();
 040          $this->formatter = $this->compiler->getLoggingFormatter();
 041          // First collect all alias targets that need to be replaced
 042          $seenAliasTargets = array();
 043          $replacements = array();
 044          foreach ($container->getAliases() as $definitionId => $target) {
 045              $targetId = (string) $target;
 046              // Special case: leave this target alone
 047              if ('service_container' === $targetId) {
 048                  continue;
 049              }
 050              // Check if target needs to be replaces
 051              if (isset($replacements[$targetId])) {
 052                  $container->setAlias($definitionId, $replacements[$targetId]);
 053              }
 054              // No neeed to process the same target twice
 055              if (isset($seenAliasTargets[$targetId])) {
 056                  continue;
 057              }
 058              // Process new target
 059              $seenAliasTargets[$targetId] = true;
 060              try {
 061                  $definition = $container->getDefinition($targetId);
 062              } catch (InvalidArgumentException $e) {
 063                  throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
 064              }
 065              if ($definition->isPublic()) {
 066                  continue;
 067              }
 068              // Remove private definition and schedule for replacement
 069              $definition->setPublic(true);
 070              $container->setDefinition($definitionId, $definition);
 071              $container->removeDefinition($targetId);
 072              $replacements[$targetId] = $definitionId;
 073          }
 074   
 075          // Now replace target instances in all definitions
 076          foreach ($container->getDefinitions() as $definitionId => $definition) {
 077              $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments()));
 078              $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls()));
 079              $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties()));
 080              $definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService(false)), false);
 081              $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory()));
 082          }
 083      }
 084   
 085      /**
 086       * Recursively updates references in an array.
 087       *
 088       * @param array  $replacements Table of aliases to replace
 089       * @param string $definitionId Identifier of this definition
 090       * @param array  $arguments    Where to replace the aliases
 091       *
 092       * @return array
 093       */
 094      private function updateArgumentReferences(array $replacements, $definitionId, array $arguments)
 095      {
 096          foreach ($arguments as $k => $argument) {
 097              // Handle recursion step
 098              if (is_array($argument)) {
 099                  $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument);
 100                  continue;
 101              }
 102              // Skip arguments that don't need replacement
 103              if (!$argument instanceof Reference) {
 104                  continue;
 105              }
 106              $referenceId = (string) $argument;
 107              if (!isset($replacements[$referenceId])) {
 108                  continue;
 109              }
 110              // Perform the replacement
 111              $newId = $replacements[$referenceId];
 112              $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
 113              $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId));
 114          }
 115   
 116          return $arguments;
 117      }
 118   
 119      /**
 120       * Returns the updated reference for the factory service.
 121       *
 122       * @param array       $replacements Table of aliases to replace
 123       * @param string|null $referenceId  Factory service reference identifier
 124       *
 125       * @return string|null
 126       */
 127      private function updateFactoryReferenceId(array $replacements, $referenceId)
 128      {
 129          if (null === $referenceId) {
 130              return;
 131          }
 132   
 133          return isset($replacements[$referenceId]) ? $replacements[$referenceId] : $referenceId;
 134      }
 135   
 136      private function updateFactoryReference(array $replacements, $factory)
 137      {
 138          if (is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) {
 139              $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior());
 140          }
 141   
 142          return $factory;
 143      }
 144  }
 145