[ Index ] |
PHP Cross Reference of phpBB-3.1.12-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 * @param ContainerBuilder $container 33 * 34 * @throws InvalidArgumentException if the service definition does not exist 35 */ 36 public function process(ContainerBuilder $container) 37 { 38 // Setup 39 $this->compiler = $container->getCompiler(); 40 $this->formatter = $this->compiler->getLoggingFormatter(); 41 // First collect all alias targets that need to be replaced 42 $seenAliasTargets = array(); 43 $replacements = array(); 44 foreach ($container->getAliases() as $definitionId => $target) { 45 $targetId = (string) $target; 46 // Special case: leave this target alone 47 if ('service_container' === $targetId) { 48 continue; 49 } 50 // Check if target needs to be replaces 51 if (isset($replacements[$targetId])) { 52 $container->setAlias($definitionId, $replacements[$targetId]); 53 } 54 // No neeed to process the same target twice 55 if (isset($seenAliasTargets[$targetId])) { 56 continue; 57 } 58 // Process new target 59 $seenAliasTargets[$targetId] = true; 60 try { 61 $definition = $container->getDefinition($targetId); 62 } catch (InvalidArgumentException $e) { 63 throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e); 64 } 65 if ($definition->isPublic()) { 66 continue; 67 } 68 // Remove private definition and schedule for replacement 69 $definition->setPublic(true); 70 $container->setDefinition($definitionId, $definition); 71 $container->removeDefinition($targetId); 72 $replacements[$targetId] = $definitionId; 73 } 74 // Now replace target instances in all definitions 75 foreach ($container->getDefinitions() as $definitionId => $definition) { 76 $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments())); 77 $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls())); 78 $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties())); 79 $definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService())); 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |