composer package updates
[openemr.git] / vendor / symfony / dependency-injection / Compiler / ResolveReferencesToAliasesPass.php
blob24c38cafb01370eb601abfe29975d0236ca3cc23
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 /**
20 * Replaces all references to aliases with references to the actual service.
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 class ResolveReferencesToAliasesPass implements CompilerPassInterface
26 private $container;
28 /**
29 * Processes the ContainerBuilder to replace references to aliases with actual service references.
31 public function process(ContainerBuilder $container)
33 $this->container = $container;
35 foreach ($container->getDefinitions() as $definition) {
36 if ($definition->isSynthetic() || $definition->isAbstract()) {
37 continue;
40 $definition->setArguments($this->processArguments($definition->getArguments()));
41 $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
42 $definition->setProperties($this->processArguments($definition->getProperties()));
43 $definition->setFactory($this->processFactory($definition->getFactory()));
44 $definition->setFactoryService($this->processFactoryService($definition->getFactoryService(false)), false);
47 foreach ($container->getAliases() as $id => $alias) {
48 $aliasId = (string) $alias;
49 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
50 $container->setAlias($id, new Alias($defId, $alias->isPublic()));
55 /**
56 * Processes the arguments to replace aliases.
58 * @param array $arguments An array of References
60 * @return array An array of References
62 private function processArguments(array $arguments)
64 foreach ($arguments as $k => $argument) {
65 if (is_array($argument)) {
66 $arguments[$k] = $this->processArguments($argument);
67 } elseif ($argument instanceof Reference) {
68 $defId = $this->getDefinitionId($id = (string) $argument);
70 if ($defId !== $id) {
71 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
76 return $arguments;
79 private function processFactoryService($factoryService)
81 if (null === $factoryService) {
82 return;
85 return $this->getDefinitionId($factoryService);
88 private function processFactory($factory)
90 if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
91 return $factory;
94 $defId = $this->getDefinitionId($id = (string) $factory[0]);
96 if ($defId !== $id) {
97 $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
100 return $factory;
104 * Resolves an alias into a definition id.
106 * @param string $id The definition or alias id to resolve
108 * @return string The definition id with aliases resolved
110 private function getDefinitionId($id)
112 $seen = array();
113 while ($this->container->hasAlias($id)) {
114 if (isset($seen[$id])) {
115 throw new ServiceCircularReferenceException($id, array_keys($seen));
117 $seen[$id] = true;
118 $id = (string) $this->container->getAlias($id);
121 return $id;