Aucune description

TranslatorPass.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $this->translatorServiceId = $translatorServiceId;
  25. $this->readerServiceId = $readerServiceId;
  26. $this->loaderTag = $loaderTag;
  27. $this->debugCommandServiceId = $debugCommandServiceId;
  28. $this->updateCommandServiceId = $updateCommandServiceId;
  29. }
  30. public function process(ContainerBuilder $container)
  31. {
  32. if (!$container->hasDefinition($this->translatorServiceId)) {
  33. return;
  34. }
  35. $loaders = [];
  36. $loaderRefs = [];
  37. foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) {
  38. $loaderRefs[$id] = new Reference($id);
  39. $loaders[$id][] = $attributes[0]['alias'];
  40. if (isset($attributes[0]['legacy-alias'])) {
  41. $loaders[$id][] = $attributes[0]['legacy-alias'];
  42. }
  43. }
  44. if ($container->hasDefinition($this->readerServiceId)) {
  45. $definition = $container->getDefinition($this->readerServiceId);
  46. foreach ($loaders as $id => $formats) {
  47. foreach ($formats as $format) {
  48. $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
  49. }
  50. }
  51. }
  52. $container
  53. ->findDefinition($this->translatorServiceId)
  54. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  55. ->replaceArgument(3, $loaders)
  56. ;
  57. if (!$container->hasParameter('twig.default_path')) {
  58. return;
  59. }
  60. $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(2));
  61. if ($container->hasDefinition($this->debugCommandServiceId)) {
  62. $definition = $container->getDefinition($this->debugCommandServiceId);
  63. $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
  64. if (\count($definition->getArguments()) > 6) {
  65. $definition->replaceArgument(6, $paths);
  66. }
  67. }
  68. if ($container->hasDefinition($this->updateCommandServiceId)) {
  69. $definition = $container->getDefinition($this->updateCommandServiceId);
  70. $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
  71. if (\count($definition->getArguments()) > 7) {
  72. $definition->replaceArgument(7, $paths);
  73. }
  74. }
  75. }
  76. }