bug #24184 [DI] Resolve nested refs to aliases (nicolas-grekas)
This PR was merged into the 3.4 branch.
Discussion
----------
[DI] Resolve nested refs to aliases
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | yes
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Required now that we have most services private. Should make tests green again.
Commits
-------
8729f9f
[DI] Resolve nested refs to aliases
This commit is contained in:
commit
2df1cbc675
@ -1463,7 +1463,7 @@ class FrameworkExtension extends Extension
|
||||
->replaceArgument(2, $config['debug'])
|
||||
->addTag('annotations.cached_reader', array('provider' => $cacheService))
|
||||
;
|
||||
$container->setAlias('annotation_reader', 'annotations.cached_reader');
|
||||
$container->setAlias('annotation_reader', 'annotations.cached_reader')->setPrivate(true);
|
||||
$container->setAlias(Reader::class, new Alias('annotations.cached_reader', false));
|
||||
} else {
|
||||
$container->removeDefinition('annotations.cached_reader');
|
||||
|
@ -35,8 +35,8 @@
|
||||
"symfony/http-foundation": "~2.8|~3.0|~4.0",
|
||||
"symfony/security-acl": "~2.8|~3.0",
|
||||
"symfony/translation": "~2.8|~3.0|~4.0",
|
||||
"symfony/twig-bundle": "~2.8|~3.0|~4.0",
|
||||
"symfony/twig-bridge": "~2.8|~3.0|~4.0",
|
||||
"symfony/twig-bundle": "~3.4|~4.0",
|
||||
"symfony/twig-bridge": "~3.4|~4.0",
|
||||
"symfony/process": "~2.8|~3.0|~4.0",
|
||||
"symfony/validator": "^3.2.5|~4.0",
|
||||
"symfony/var-dumper": "~3.3|~4.0",
|
||||
|
@ -11,8 +11,6 @@
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
@ -22,97 +20,56 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class ResolveReferencesToAliasesPass implements CompilerPassInterface
|
||||
class ResolveReferencesToAliasesPass extends AbstractRecursivePass
|
||||
{
|
||||
private $container;
|
||||
|
||||
/**
|
||||
* Processes the ContainerBuilder to replace references to aliases with actual service references.
|
||||
*
|
||||
* @param ContainerBuilder $container
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
foreach ($container->getDefinitions() as $definition) {
|
||||
if ($definition->isSynthetic() || $definition->isAbstract()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$definition->setArguments($this->processArguments($definition->getArguments()));
|
||||
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
|
||||
$definition->setProperties($this->processArguments($definition->getProperties()));
|
||||
if (isset($definition->getChanges()['factory'])) {
|
||||
$definition->setFactory($this->processFactory($definition->getFactory()));
|
||||
}
|
||||
}
|
||||
parent::process($container);
|
||||
|
||||
foreach ($container->getAliases() as $id => $alias) {
|
||||
$aliasId = (string) $alias;
|
||||
if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
|
||||
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
|
||||
$container->setAlias($id, $defId)->setPublic($alias->isPublic() && !$alias->isPrivate())->setPrivate($alias->isPrivate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the arguments to replace aliases.
|
||||
*
|
||||
* @param array $arguments An array of References
|
||||
*
|
||||
* @return array An array of References
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
private function processArguments(array $arguments)
|
||||
protected function processValue($value, $isRoot = false)
|
||||
{
|
||||
foreach ($arguments as $k => $argument) {
|
||||
if (is_array($argument)) {
|
||||
$arguments[$k] = $this->processArguments($argument);
|
||||
} elseif ($argument instanceof ArgumentInterface) {
|
||||
$argument->setValues($this->processArguments($argument->getValues()));
|
||||
} elseif ($argument instanceof Reference) {
|
||||
$defId = $this->getDefinitionId($id = (string) $argument);
|
||||
if ($value instanceof Reference) {
|
||||
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
|
||||
|
||||
if ($defId !== $id) {
|
||||
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
|
||||
}
|
||||
if ($defId !== $id) {
|
||||
return new Reference($defId, $value->getInvalidBehavior());
|
||||
}
|
||||
}
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
private function processFactory($factory)
|
||||
{
|
||||
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
|
||||
return $factory;
|
||||
}
|
||||
|
||||
$defId = $this->getDefinitionId($id = (string) $factory[0]);
|
||||
|
||||
if ($defId !== $id) {
|
||||
$factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior());
|
||||
}
|
||||
|
||||
return $factory;
|
||||
return parent::processValue($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves an alias into a definition id.
|
||||
*
|
||||
* @param string $id The definition or alias id to resolve
|
||||
* @param string $id The definition or alias id to resolve
|
||||
* @param ContainerBuilder $container
|
||||
*
|
||||
* @return string The definition id with aliases resolved
|
||||
*/
|
||||
private function getDefinitionId($id)
|
||||
private function getDefinitionId($id, $container)
|
||||
{
|
||||
$seen = array();
|
||||
while ($this->container->hasAlias($id)) {
|
||||
while ($container->hasAlias($id)) {
|
||||
if (isset($seen[$id])) {
|
||||
throw new ServiceCircularReferenceException($id, array_keys($seen));
|
||||
}
|
||||
$seen[$id] = true;
|
||||
$id = (string) $this->container->getAlias($id);
|
||||
$id = (string) $container->getAlias($id);
|
||||
}
|
||||
|
||||
return $id;
|
||||
|
Reference in New Issue
Block a user