TranslationExtractorPass.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Translation\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Adds tagged translation.extractor services to translation extractor.
  17. */
  18. class TranslationExtractorPass implements CompilerPassInterface
  19. {
  20. private $extractorServiceId;
  21. private $extractorTag;
  22. public function __construct(string $extractorServiceId = 'translation.extractor', string $extractorTag = 'translation.extractor')
  23. {
  24. if (0 < \func_num_args()) {
  25. trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  26. }
  27. $this->extractorServiceId = $extractorServiceId;
  28. $this->extractorTag = $extractorTag;
  29. }
  30. public function process(ContainerBuilder $container)
  31. {
  32. if (!$container->hasDefinition($this->extractorServiceId)) {
  33. return;
  34. }
  35. $definition = $container->getDefinition($this->extractorServiceId);
  36. foreach ($container->findTaggedServiceIds($this->extractorTag, true) as $id => $attributes) {
  37. if (!isset($attributes[0]['alias'])) {
  38. throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
  39. }
  40. $definition->addMethodCall('addExtractor', [$attributes[0]['alias'], new Reference($id)]);
  41. }
  42. }
  43. }