annotate vendor/symfony/dependency-injection/Compiler/ResolveReferencesToAliasesPass.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@0 13
Chris@0 14 use Symfony\Component\DependencyInjection\Alias;
Chris@0 15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
Chris@0 16 use Symfony\Component\DependencyInjection\Reference;
Chris@0 17 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Replaces all references to aliases with references to the actual service.
Chris@0 21 *
Chris@0 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 23 */
Chris@0 24 class ResolveReferencesToAliasesPass implements CompilerPassInterface
Chris@0 25 {
Chris@0 26 private $container;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Processes the ContainerBuilder to replace references to aliases with actual service references.
Chris@0 30 *
Chris@0 31 * @param ContainerBuilder $container
Chris@0 32 */
Chris@0 33 public function process(ContainerBuilder $container)
Chris@0 34 {
Chris@0 35 $this->container = $container;
Chris@0 36
Chris@0 37 foreach ($container->getDefinitions() as $definition) {
Chris@0 38 if ($definition->isSynthetic() || $definition->isAbstract()) {
Chris@0 39 continue;
Chris@0 40 }
Chris@0 41
Chris@0 42 $definition->setArguments($this->processArguments($definition->getArguments()));
Chris@0 43 $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
Chris@0 44 $definition->setProperties($this->processArguments($definition->getProperties()));
Chris@0 45 $definition->setFactory($this->processFactory($definition->getFactory()));
Chris@0 46 }
Chris@0 47
Chris@0 48 foreach ($container->getAliases() as $id => $alias) {
Chris@0 49 $aliasId = (string) $alias;
Chris@0 50 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
Chris@0 51 $container->setAlias($id, new Alias($defId, $alias->isPublic()));
Chris@0 52 }
Chris@0 53 }
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Processes the arguments to replace aliases.
Chris@0 58 *
Chris@0 59 * @param array $arguments An array of References
Chris@0 60 *
Chris@0 61 * @return array An array of References
Chris@0 62 */
Chris@0 63 private function processArguments(array $arguments)
Chris@0 64 {
Chris@0 65 foreach ($arguments as $k => $argument) {
Chris@0 66 if (is_array($argument)) {
Chris@0 67 $arguments[$k] = $this->processArguments($argument);
Chris@0 68 } elseif ($argument instanceof Reference) {
Chris@0 69 $defId = $this->getDefinitionId($id = (string) $argument);
Chris@0 70
Chris@0 71 if ($defId !== $id) {
Chris@0 72 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
Chris@0 73 }
Chris@0 74 }
Chris@0 75 }
Chris@0 76
Chris@0 77 return $arguments;
Chris@0 78 }
Chris@0 79
Chris@0 80 private function processFactory($factory)
Chris@0 81 {
Chris@0 82 if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
Chris@0 83 return $factory;
Chris@0 84 }
Chris@0 85
Chris@0 86 $defId = $this->getDefinitionId($id = (string) $factory[0]);
Chris@0 87
Chris@0 88 if ($defId !== $id) {
Chris@0 89 $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior());
Chris@0 90 }
Chris@0 91
Chris@0 92 return $factory;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Resolves an alias into a definition id.
Chris@0 97 *
Chris@0 98 * @param string $id The definition or alias id to resolve
Chris@0 99 *
Chris@0 100 * @return string The definition id with aliases resolved
Chris@0 101 */
Chris@0 102 private function getDefinitionId($id)
Chris@0 103 {
Chris@0 104 $seen = array();
Chris@0 105 while ($this->container->hasAlias($id)) {
Chris@0 106 if (isset($seen[$id])) {
Chris@0 107 throw new ServiceCircularReferenceException($id, array_keys($seen));
Chris@0 108 }
Chris@0 109 $seen[$id] = true;
Chris@0 110 $id = (string) $this->container->getAlias($id);
Chris@0 111 }
Chris@0 112
Chris@0 113 return $id;
Chris@0 114 }
Chris@0 115 }