bug #17555 [DependencyInjection] resolve aliases in factory services (xabbuh)
This PR was merged into the 2.3 branch.
Discussion
----------
[DependencyInjection] resolve aliases in factory services
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR |
Commits
-------
7702189
resolve aliases in factory services
This commit is contained in:
commit
7743eefe18
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\DependencyInjection\Compiler;
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\Alias;
|
use Symfony\Component\DependencyInjection\Alias;
|
||||||
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
@ -42,6 +43,7 @@ class ResolveReferencesToAliasesPass implements CompilerPassInterface
|
|||||||
$definition->setArguments($this->processArguments($definition->getArguments()));
|
$definition->setArguments($this->processArguments($definition->getArguments()));
|
||||||
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
|
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
|
||||||
$definition->setProperties($this->processArguments($definition->getProperties()));
|
$definition->setProperties($this->processArguments($definition->getProperties()));
|
||||||
|
$definition->setFactoryService($this->processFactoryService($definition->getFactoryService()));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($container->getAliases() as $id => $alias) {
|
foreach ($container->getAliases() as $id => $alias) {
|
||||||
@ -76,6 +78,15 @@ class ResolveReferencesToAliasesPass implements CompilerPassInterface
|
|||||||
return $arguments;
|
return $arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function processFactoryService($factoryService)
|
||||||
|
{
|
||||||
|
if (null === $factoryService) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getDefinitionId($factoryService);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves an alias into a definition id.
|
* Resolves an alias into a definition id.
|
||||||
*
|
*
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Alias;
|
||||||
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
@ -59,6 +61,21 @@ class ResolveReferencesToAliasesPassTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->process($container);
|
$this->process($container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testResolveFactory()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->register('factory', 'Factory');
|
||||||
|
$container->setAlias('factory_alias', new Alias('factory'));
|
||||||
|
$foo = new Definition();
|
||||||
|
$foo->setFactoryService('factory_alias');
|
||||||
|
$foo->setFactoryMethod('createFoo');
|
||||||
|
$container->setDefinition('foo', $foo);
|
||||||
|
|
||||||
|
$this->process($container);
|
||||||
|
|
||||||
|
$this->assertSame('factory', $foo->getFactoryService());
|
||||||
|
}
|
||||||
|
|
||||||
protected function process(ContainerBuilder $container)
|
protected function process(ContainerBuilder $container)
|
||||||
{
|
{
|
||||||
$pass = new ResolveReferencesToAliasesPass();
|
$pass = new ResolveReferencesToAliasesPass();
|
||||||
|
Reference in New Issue
Block a user