TranslatorPass.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
  23. {
  24. if ('translation.loader' === $readerServiceId && 2 > func_num_args()) {
  25. @trigger_error('The default value for $readerServiceId will change in 4.0 to "translation.reader".', E_USER_DEPRECATED);
  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 = array();
  39. $loaderRefs = array();
  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', array($format, $loaderRefs[$id]));
  52. }
  53. }
  54. }
  55. // Duplicated code to support "translation.reader", to be removed in 4.0
  56. if ('translation.reader' !== $this->readerServiceId) {
  57. if ($container->hasDefinition('translation.reader')) {
  58. $definition = $container->getDefinition('translation.reader');
  59. foreach ($loaders as $id => $formats) {
  60. foreach ($formats as $format) {
  61. $definition->addMethodCall('addLoader', array($format, $loaderRefs[$id]));
  62. }
  63. }
  64. }
  65. }
  66. $container
  67. ->findDefinition($this->translatorServiceId)
  68. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  69. ->replaceArgument(3, $loaders)
  70. ;
  71. if (!$container->hasParameter('twig.default_path')) {
  72. return;
  73. }
  74. if ($container->hasDefinition($this->debugCommandServiceId)) {
  75. $container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
  76. }
  77. if ($container->hasDefinition($this->updateCommandServiceId)) {
  78. $container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
  79. }
  80. }
  81. }