ResolveServiceSubscribersPass.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Contracts\Service\ServiceProviderInterface;
  15. /**
  16. * Compiler pass to inject their service locator to service subscribers.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ResolveServiceSubscribersPass extends AbstractRecursivePass
  21. {
  22. private $serviceLocator;
  23. protected function processValue($value, bool $isRoot = false)
  24. {
  25. if ($value instanceof Reference && $this->serviceLocator && \in_array((string) $value, [ContainerInterface::class, ServiceProviderInterface::class], true)) {
  26. return new Reference($this->serviceLocator);
  27. }
  28. if (!$value instanceof Definition) {
  29. return parent::processValue($value, $isRoot);
  30. }
  31. $serviceLocator = $this->serviceLocator;
  32. $this->serviceLocator = null;
  33. if ($value->hasTag('container.service_subscriber.locator')) {
  34. $this->serviceLocator = $value->getTag('container.service_subscriber.locator')[0]['id'];
  35. $value->clearTag('container.service_subscriber.locator');
  36. }
  37. try {
  38. return parent::processValue($value);
  39. } finally {
  40. $this->serviceLocator = $serviceLocator;
  41. }
  42. }
  43. }