TranslatorPass.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public function process(ContainerBuilder $container)
  18. {
  19. if (!$container->hasDefinition('translator.default')) {
  20. return;
  21. }
  22. $loaders = [];
  23. $loaderRefs = [];
  24. foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) {
  25. $loaderRefs[$id] = new Reference($id);
  26. $loaders[$id][] = $attributes[0]['alias'];
  27. if (isset($attributes[0]['legacy-alias'])) {
  28. $loaders[$id][] = $attributes[0]['legacy-alias'];
  29. }
  30. }
  31. if ($container->hasDefinition('translation.reader')) {
  32. $definition = $container->getDefinition('translation.reader');
  33. foreach ($loaders as $id => $formats) {
  34. foreach ($formats as $format) {
  35. $definition->addMethodCall('addLoader', [$format, $loaderRefs[$id]]);
  36. }
  37. }
  38. }
  39. $container
  40. ->findDefinition('translator.default')
  41. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
  42. ->replaceArgument(3, $loaders)
  43. ;
  44. if ($container->hasDefinition('validator') && $container->hasDefinition('translation.extractor.visitor.constraint')) {
  45. $constraintVisitorDefinition = $container->getDefinition('translation.extractor.visitor.constraint');
  46. $constraintClassNames = [];
  47. foreach ($container->findTaggedServiceIds('validator.constraint_validator', true) as $id => $attributes) {
  48. $serviceDefinition = $container->getDefinition($id);
  49. // Resolve constraint validator FQCN even if defined as %foo.validator.class% parameter
  50. $className = $container->getParameterBag()->resolveValue($serviceDefinition->getClass());
  51. // Extraction of the constraint class name from the Constraint Validator FQCN
  52. $constraintClassNames[] = str_replace('Validator', '', substr(strrchr($className, '\\'), 1));
  53. }
  54. $constraintVisitorDefinition->setArgument(0, $constraintClassNames);
  55. }
  56. if (!$container->hasParameter('twig.default_path')) {
  57. return;
  58. }
  59. $paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1));
  60. if ($container->hasDefinition('console.command.translation_debug')) {
  61. $definition = $container->getDefinition('console.command.translation_debug');
  62. $definition->replaceArgument(4, $container->getParameter('twig.default_path'));
  63. if (\count($definition->getArguments()) > 6) {
  64. $definition->replaceArgument(6, $paths);
  65. }
  66. }
  67. if ($container->hasDefinition('console.command.translation_extract')) {
  68. $definition = $container->getDefinition('console.command.translation_extract');
  69. $definition->replaceArgument(5, $container->getParameter('twig.default_path'));
  70. if (\count($definition->getArguments()) > 7) {
  71. $definition->replaceArgument(7, $paths);
  72. }
  73. }
  74. }
  75. }