8000 [DependencyInjection] resolve aliases in factories by xabbuh · Pull Request #17554 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
resolve aliases in factories
  • Loading branch information
xabbuh committed Jan 27, 2016
commit fde10e799a599332dcc702b8b71ff8c4a63d847f
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function process(ContainerBuilder $container)
$definition->setArguments($this->processArguments($definition->getArguments()));
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
$definition->setProperties($this->processArguments($definition->getProperties()));
$definition->setFactory($this->processFactory($definition->getFactory()));
}

foreach ($container->getAliases() as $id => $alias) {
Expand Down Expand Up @@ -76,6 +78,21 @@ private function processArguments(array $arguments)
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(), $factory[0]->isStrict());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that this line will need changes when merging into 2.8 (and then again in 3.0) due to changes to isStrict

}

return $factory;
}

/**
* Resolves an alias into a definition id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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\Compiler\ResolveReferencesToAliasesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -59,6 +61,27 @@ public function testAliasCircularReference()
$this->process($container);
}

public function testResolveFactory()
{
$container = new ContainerBuilder();
$container->register('factory', 'Factory');
$container->setAlias('factory_alias', new Alias('factory'));
$foo = new Definition();
$foo->setFactory(array(new Reference('factory_alias'), 'createFoo'));
$container->setDefinition('foo', $foo);
$bar = new Definition();
$bar->setFactory(array('Factory', 'createFoo'));
$container->setDefinition('bar', $bar);

$this->process($container);

$resolvedFooFactory = $container->getDefinition('foo')->getFactory();
$resolvedBarFactory = $container->getDefinition('bar')->getFactory();

$this->assertSame('factory', (string) $resolvedFooFactory[0]);
$this->assertSame('Factory', (string) $resolvedBarFactory[0]);
}

protected function process(ContainerBuilder $container)
{
$pass = new ResolveReferencesToAliasesPass();
Expand Down
0