TranslatorPass.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Reference;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  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 = array();
  36. $loaderRefs = array();
  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', array($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. if ($container->hasDefinition($this->debugCommandServiceId)) {
  61. $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
  62. }
  63. if ($container->hasDefinition($this->updateCommandServiceId)) {
  64. $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
  65. }
  66. }
  67. }