Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Allow service subscribers to leverage autowiring to know where their locator should be injected #22457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2017
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
[DI] Allow service subscribers to leverage autowiring to know where t…
…he locator should be injected
  • Loading branch information
nicolas-grekas committed Apr 19, 2017
commit e407b3d42e9adff4e82df05b7f62b0363e9f685c
2 changes: 1 addition & 1 deletion 2 src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<service id="twig.cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheCacheWarmer" public="false">
<tag name="kernel.cache_warmer" />
<tag name="container.service_subscriber" id="twig" />
<argument type="service" id="container" />
<argument type="service" id="Psr\Container\ContainerInterface" />
<argument type="service" id="templating.finder" on-invalid="ignore" />
<argument type="collection" /> <!-- Twig paths -->
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct()
new RegisterServiceSubscribersPass(),
new ResolveNamedArgumentsPass(),
new AutowirePass(),
new ResolveServiceSubscribersPass(),
new ResolveReferencesToAliasesPass(),
new ResolveInvalidReferencesPass(),
new AnalyzeServiceReferencesPass(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,8 @@
*/
class RegisterServiceSubscribersPass extends AbstractRecursivePass
{
private $serviceLocator;

protected function processValue($value, $isRoot = false)
{
if ($value instanceof Reference && $this->serviceLocator && 'container' === (string) $value) {
return new Reference($this->serviceLocator);
}

if (!$value instanceof Definition || $value->isAbstract() || $value->isSynthetic() || !$value->hasTag('container.service_subscriber')) {
return parent::processValue($value, $isRoot);
}
Expand Down Expand Up @@ -100,13 +94,8 @@ protected function processValue($value, $isRoot = false)
throw new InvalidArgumentException(sprintf('Service %s not exist in the map returned by "%s::getSubscribedServices()" for service "%s".', $message, $class, $this->currentId));
}

$serviceLocator = $this->serviceLocator;
$this->serviceLocator = (string) ServiceLocatorTagPass::register($this->container, $subscriberMap);
$value->addTag('container.service_subscriber.locator', array('id' => (string) ServiceLocatorTagPass::register($this->container, $subscriberMap)));

try {
return parent::processValue($value);
} finally {
$this->serviceLocator = $serviceLocator;
}
return parent::processValue($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Compiler;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* Compiler pass to inject their service locator to service subscribers.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class ResolveServiceSubscribersPass extends AbstractRecursivePass
{
private $serviceLocator;

protected function processValue($value, $isRoot = false)
{
if ($value instanceof Reference && $this->serviceLocator && ContainerInterface::class === (string) $value) {
return new Reference($this->serviceLocator);
}

if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
}

$serviceLocator = $this->serviceLocator;
$this->serviceLocator = $value->hasTag('container.service_subscriber.locator') ? $value->getTag('container.service_subscriber.locator')[0]['id'] : null;

try {
return parent::processValue($value);
} finally {
$this->serviceLocator = $serviceLocator;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Compiler\RegisterServiceSubscribersPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveServiceSubscribersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -38,8 +40,8 @@ public function testInvalidClass()
->addTag('container.service_subscriber')
;

$pass = new RegisterServiceSubscribersPass();
$pass->process($container);
(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);
}

/**
Expand All @@ -54,21 +56,21 @@ public function testInvalidAttributes()
->addTag('container.service_subscriber', array('bar' => '123'))
;

$pass = new RegisterServiceSubscribersPass();
$pass->process($container);
(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);
}

public function testNoAttributes()
{
$container = new ContainerBuilder();

$container->register('foo', TestServiceSubscriber::class)
->addArgument(new Reference('container'))
->addArgument(new Reference(PsrContainerInterface::class))
->addTag('container.service_subscriber')
;

$pass = new RegisterServiceSubscribersPass();
$pass->process($container);
(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);

$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getArgument(0));
Expand All @@ -92,13 +94,13 @@ public function testWithAttributes()

$container->register('foo', TestServiceSubscriber::class)
->setAutowired(true)
->addArgument(new Reference('container'))
->addArgument(new Reference(PsrContainerInterface::class))
->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'bar'))
->addTag('container.service_subscriber', array('key' => 'bar', 'id' => 'baz')) // should be ignored: the first wins
;

$pass = new RegisterServiceSubscribersPass();
$pass->process($container);
(new RegisterServiceSubscribersPass())->process($container);
(new ResolveServiceSubscribersPass())->process($container);

$foo = $container->getDefinition('foo');
$locator = $container->getDefinition((string) $foo->getArgument(0));
Expand All @@ -125,7 +127,7 @@ public function testExtraServiceSubscriber()
$container = new ContainerBuilder();
$container->register('foo_service', TestServiceSubscriber::class)
->setAutowired(true)
->addArgument(new Reference('container'))
->addArgument(new Reference(PsrContainerInterface::class))
->addTag('container.service_subscriber', array(
'key' => 'test',
'id' => TestServiceSubscriber::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use DummyProxyDumper;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
Expand Down Expand Up @@ -560,7 +561,7 @@ public function testServiceSubscriber()
$container = new ContainerBuilder();
$container->register('foo_service', TestServiceSubscriber::class)
->setAutowired(true)
->addArgument(new Reference('container'))
->addArgument(new Reference(ContainerInterface::class))
->addTag('container.service_subscriber', array(
'key' => 'bar',
'id' => TestServiceSubscriber::class,
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.