[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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\Exception\InvalidArgumentException; 16 use Symfony\Component\DependencyInjection\Reference; 17 18 /** 19 * Replaces aliases with actual service definitions, effectively removing these 20 * aliases. 21 * 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 23 */ 24 class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface 25 { 26 private $compiler; 27 private $formatter; 28 29 /** 30 * Process the Container to replace aliases with service definitions. 31 * 32 * @throws InvalidArgumentException if the service definition does not exist 33 */ 34 public function process(ContainerBuilder $container) 35 { 36 // Setup 37 $this->compiler = $container->getCompiler(); 38 $this->formatter = $this->compiler->getLoggingFormatter(); 39 // First collect all alias targets that need to be replaced 40 $seenAliasTargets = array(); 41 $replacements = array(); 42 foreach ($container->getAliases() as $definitionId => $target) { 43 $targetId = (string) $target; 44 // Special case: leave this target alone 45 if ('service_container' === $targetId) { 46 continue; 47 } 48 // Check if target needs to be replaces 49 if (isset($replacements[$targetId])) { 50 $container->setAlias($definitionId, $replacements[$targetId]); 51 } 52 // No need to process the same target twice 53 if (isset($seenAliasTargets[$targetId])) { 54 continue; 55 } 56 // Process new target 57 $seenAliasTargets[$targetId] = true; 58 try { 59 $definition = $container->getDefinition($targetId); 60 } catch (InvalidArgumentException $e) { 61 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e); 62 } 63 if ($definition->isPublic()) { 64 continue; 65 } 66 // Remove private definition and schedule for replacement 67 $definition->setPublic(true); 68 $container->setDefinition($definitionId, $definition); 69 $container->removeDefinition($targetId); 70 $replacements[$targetId] = $definitionId; 71 } 72 73 // Now replace target instances in all definitions 74 foreach ($container->getDefinitions() as $definitionId => $definition) { 75 $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments())); 76 $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls())); 77 $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties())); 78 $definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService(false)), false); 79 $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory())); 80 } 81 } 82 83 /** 84 * Recursively updates references in an array. 85 * 86 * @param array $replacements Table of aliases to replace 87 * @param string $definitionId Identifier of this definition 88 * @param array $arguments Where to replace the aliases 89 * 90 * @return array 91 */ 92 private function updateArgumentReferences(array $replacements, $definitionId, array $arguments) 93 { 94 foreach ($arguments as $k => $argument) { 95 // Handle recursion step 96 if (\is_array($argument)) { 97 $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument); 98 continue; 99 } 100 // Skip arguments that don't need replacement 101 if (!$argument instanceof Reference) { 102 continue; 103 } 104 $referenceId = (string) $argument; 105 if (!isset($replacements[$referenceId])) { 106 continue; 107 } 108 // Perform the replacement 109 $newId = $replacements[$referenceId]; 110 $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior()); 111 $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId)); 112 } 113 114 return $arguments; 115 } 116 117 /** 118 * Returns the updated reference for the factory service. 119 * 120 * @param array $replacements Table of aliases to replace 121 * @param string|null $referenceId Factory service reference identifier 122 * 123 * @return string|null 124 */ 125 private function updateFactoryReferenceId(array $replacements, $referenceId) 126 { 127 if (null === $referenceId) { 128 return; 129 } 130 131 return isset($replacements[$referenceId]) ? $replacements[$referenceId] : $referenceId; 132 } 133 134 private function updateFactoryReference(array $replacements, $factory) 135 { 136 if (\is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) { 137 $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior()); 138 } 139 140 return $factory; 141 } 142 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |