You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

3 years ago
  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\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Register all services that have the "kernel.locale_aware" tag into the listener.
  17. *
  18. * @author Pierre Bobiet <pierrebobiet@gmail.com>
  19. */
  20. class RegisterLocaleAwareServicesPass implements CompilerPassInterface
  21. {
  22. private $listenerServiceId;
  23. private $localeAwareTag;
  24. public function __construct(string $listenerServiceId = 'locale_aware_listener', string $localeAwareTag = 'kernel.locale_aware')
  25. {
  26. if (0 < \func_num_args()) {
  27. trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  28. }
  29. $this->listenerServiceId = $listenerServiceId;
  30. $this->localeAwareTag = $localeAwareTag;
  31. }
  32. public function process(ContainerBuilder $container)
  33. {
  34. if (!$container->hasDefinition($this->listenerServiceId)) {
  35. return;
  36. }
  37. $services = [];
  38. foreach ($container->findTaggedServiceIds($this->localeAwareTag) as $id => $tags) {
  39. $services[] = new Reference($id);
  40. }
  41. if (!$services) {
  42. $container->removeDefinition($this->listenerServiceId);
  43. return;
  44. }
  45. $container
  46. ->getDefinition($this->listenerServiceId)
  47. ->setArgument(0, new IteratorArgument($services))
  48. ;
  49. }
  50. }