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.

93 lines
3.6 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\Translation\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. class TranslatorPass implements CompilerPassInterface
  16. {
  17. private $translatorServiceId;
  18. private $readerServiceId;
  19. private $loaderTag;
  20. private $debugCommandServiceId;
  21. private $updateCommandServiceId;
  22. public function __construct(string $translatorServiceId = 'translator.default', string $readerServiceId = 'translation.reader', string $loaderTag = 'translation.loader', string $debugCommandServiceId = 'console.command.translation_debug', string $updateCommandServiceId = 'console.command.translation_update')
  23. {
  24. if (0 < \func_num_args()) {
  25. trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  26. }
  27. $this->translatorServiceId = $translatorServiceId;
  28. $this->readerServiceId = $readerServiceId;
  29. $this->loaderTag = $loaderTag;
  30. $this->debugCommandServiceId = $debugCommandServiceId;
  31. $this->updateCommandServiceId = $updateCommandServiceId;
  32. }
  33. public function process(ContainerBuilder $container)
  34. {
  35. if (!$container->hasDefinition($this->translatorServiceId)) {
  36. return;
  37. }
  38. $loaders = [];
  39. $loaderRefs = [];
  40. foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
  41. $loaderRefs[$id] = new Reference($id);
  42. $loaders[$id][] = $attributes[0]['alias'];
  43. if (isset($attributes[0]['legacy-alias'])) {
  44. $loaders[$id][] = $attributes[0]['legacy-alias'];
  45. }
  46. }
  47. if ($container->hasDefinition($this->readerServiceId)) {
  48. $definition = $container->getDefinition($this->readerServiceId);
  49. foreach ($loaders as $id => $formats) {
  50. foreach ($formats as $format) {
  51. $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
  52. }
  53. }
  54. }
  55. $container
  56. ->findDefinition($this->translatorServiceId)
  57. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  58. ->replaceArgument(3, $loaders)
  59. ;
  60. if (!$container->hasParameter('twig.default_path')) {
  61. return;
  62. }
  63. $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1));
  64. if ($container->hasDefinition($this->debugCommandServiceId)) {
  65. $definition = $container->getDefinition($this->debugCommandServiceId);
  66. $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
  67. if (\count($definition->getArguments()) > 6) {
  68. $definition->replaceArgument(6, $paths);
  69. }
  70. }
  71. if ($container->hasDefinition($this->updateCommandServiceId)) {
  72. $definition = $container->getDefinition($this->updateCommandServiceId);
  73. $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
  74. if (\count($definition->getArguments()) > 7) {
  75. $definition->replaceArgument(7, $paths);
  76. }
  77. }
  78. }
  79. }