4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Psr\Container\ContainerInterface;
15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\Reference;
19 * Compiler pass to inject their service locator to service subscribers.
21 * @author Nicolas Grekas <p@tchwork.com>
23 class ResolveServiceSubscribersPass extends AbstractRecursivePass
25 private $serviceLocator;
27 protected function processValue($value, $isRoot = false)
29 if ($value instanceof Reference && $this->serviceLocator && ContainerInterface::class === $this->container->normalizeId($value)) {
30 return new Reference($this->serviceLocator);
33 if (!$value instanceof Definition) {
34 return parent::processValue($value, $isRoot);
37 $serviceLocator = $this->serviceLocator;
38 $this->serviceLocator = null;
40 if ($value->hasTag('container.service_subscriber.locator')) {
41 $this->serviceLocator = $value->getTag('container.service_subscriber.locator')[0]['id'];
42 $value->clearTag('container.service_subscriber.locator');
46 return parent::processValue($value);
48 $this->serviceLocator = $serviceLocator;