ResolveServiceSubscribersPass.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. /**
  15. * Compiler pass to inject their service locator to service subscribers.
  16. *
  17. * @autor ThurData <info@thurdata.ch>
  18. */
  19. class ResolveServiceSubscribersPass extends AbstractRecursivePass
  20. {
  21. private $serviceLocator;
  22. protected function processValue($value, $isRoot = false)
  23. {
  24. if ($value instanceof Reference && $this->serviceLocator && ContainerInterface::class === (string) $value) {
  25. return new Reference($this->serviceLocator);
  26. }
  27. if (!$value instanceof Definition) {
  28. return parent::processValue($value, $isRoot);
  29. }
  30. $serviceLocator = $this->serviceLocator;
  31. $this->serviceLocator = $value->hasTag('container.service_subscriber.locator') ? $value->getTag('container.service_subscriber.locator')[0]['id'] : null;
  32. try {
  33. return parent::processValue($value);
  34. } finally {
  35. $this->serviceLocator = $serviceLocator;
  36. }
  37. }
  38. }