Skip to content

Commit

Permalink
[DependencyInjection][ServiceSubscriber] Support late aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Apr 15, 2020
1 parent b2f210f commit 2651a60
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
Expand Up @@ -1715,7 +1715,11 @@ private function dumpValue($value, bool $interpolate = true): string
if (!$v) {
continue;
}
$definition = $this->container->findDefinition($id = (string) $v);
$id = (string) $v;
while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}
$definition = $this->container->getDefinition($id);
$load = !($definition->hasErrors() && $e = $definition->getErrors()) ? $this->asFiles && !$this->inlineFactories && !$this->isHotPath($definition) : reset($e);
$serviceMap .= sprintf("\n %s => [%s, %s, %s, %s],",
$this->export($k),
Expand Down
Expand Up @@ -88,6 +88,7 @@ public function testNoAttributes()
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')),
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')),
'late_alias' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'late_alias')),
];

$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
Expand Down Expand Up @@ -118,6 +119,7 @@ public function testWithAttributes()
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
'bar' => new ServiceClosureArgument(new TypedReference('bar', CustomDefinition::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')),
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')),
'late_alias' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'late_alias')),
];

$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
Expand Down Expand Up @@ -260,6 +262,7 @@ public function testBinding()
CustomDefinition::class => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE)),
'bar' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'bar')),
'baz' => new ServiceClosureArgument(new TypedReference(CustomDefinition::class, CustomDefinition::class, ContainerInterface::IGNORE_ON_INVALID_REFERENCE, 'baz')),
'late_alias' => new ServiceClosureArgument(new TypedReference(TestDefinition1::class, TestDefinition1::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, 'late_alias')),
];

$this->assertEquals($expected, $container->getDefinition((string) $locator->getFactory()[0])->getArgument(0));
Expand Down
Expand Up @@ -16,11 +16,14 @@
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocator as ArgumentServiceLocator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -38,6 +41,7 @@
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooWithAbstractArgument;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\DependencyInjection\Variable;
Expand Down Expand Up @@ -911,6 +915,21 @@ public function testServiceSubscriber()

$container->register(CustomDefinition::class, CustomDefinition::class)
->setPublic(false);

$container->register(TestDefinition1::class, TestDefinition1::class)->setPublic(true);

$container->addCompilerPass(new class() implements CompilerPassInterface {
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$container->setDefinition('late_alias', new Definition(TestDefinition1::class));
$container->setAlias(TestDefinition1::class, 'late_alias');
}

}, PassConfig::TYPE_AFTER_REMOVING);

$container->compile();

$dumper = new PhpDumper($container);
Expand Down
Expand Up @@ -22,6 +22,7 @@ public static function getSubscribedServices(): array
'?'.CustomDefinition::class,
'bar' => CustomDefinition::class,
'baz' => '?'.CustomDefinition::class,
'late_alias' => TestDefinition1::class,
];
}
}
Expand Up @@ -24,9 +24,11 @@ public function __construct()
$this->methodMap = [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'getTestServiceSubscriberService',
'foo_service' => 'getFooServiceService',
'late_alias' => 'getLateAliasService',
];
$this->aliases = [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1' => 'late_alias',
];

$this->aliases = [];
}

public function compile(): void
Expand All @@ -42,11 +44,13 @@ public function isCompiled(): bool
public function getRemovedIds(): array
{
return [
'.service_locator.Csd_kfL' => true,
'.service_locator.Csd_kfL.foo_service' => true,
'.service_locator.dZze14t' => true,
'.service_locator.dZze14t.foo_service' => true,
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => true,
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1' => true,
'late_alias' => true,
];
}

Expand All @@ -72,14 +76,26 @@ protected function getFooServiceService()
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
'bar' => ['services', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber', 'getTestServiceSubscriberService', false],
'baz' => ['privates', 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition', 'getCustomDefinitionService', false],
'late_alias' => ['services', 'late_alias', 'getLateAliasService', false],
], [
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber',
'bar' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'baz' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition',
'late_alias' => 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestDefinition1',
]))->withContext('foo_service', $this));
}

/**
* Gets the public 'late_alias' shared service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1
*/
protected function getLateAliasService()
{
return $this->services['late_alias'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestDefinition1();
}

/**
* Gets the private 'Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared service.
*
Expand Down

0 comments on commit 2651a60

Please sign in to comment.