LoggingTranslatorPass.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  14. use Symfony\Component\Translation\TranslatorBagInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17. * @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
  18. */
  19. class LoggingTranslatorPass implements CompilerPassInterface
  20. {
  21. public function process(ContainerBuilder $container)
  22. {
  23. if (!$container->hasAlias('logger') || !$container->hasAlias('translator')) {
  24. return;
  25. }
  26. if ($container->hasParameter('translator.logging') && $container->getParameter('translator.logging')) {
  27. $translatorAlias = $container->getAlias('translator');
  28. $definition = $container->getDefinition((string) $translatorAlias);
  29. $class = $container->getParameterBag()->resolveValue($definition->getClass());
  30. if (!$r = $container->getReflectionClass($class)) {
  31. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $translatorAlias));
  32. }
  33. if ($r->isSubclassOf(TranslatorInterface::class) && $r->isSubclassOf(TranslatorBagInterface::class)) {
  34. $container->getDefinition('translator.logging')->setDecoratedService('translator');
  35. $warmer = $container->getDefinition('translation.warmer');
  36. $subscriberAttributes = $warmer->getTag('container.service_subscriber');
  37. $warmer->clearTag('container.service_subscriber');
  38. foreach ($subscriberAttributes as $k => $v) {
  39. if ((!isset($v['id']) || 'translator' !== $v['id']) && (!isset($v['key']) || 'translator' !== $v['key'])) {
  40. $warmer->addTag('container.service_subscriber', $v);
  41. }
  42. }
  43. $warmer->addTag('container.service_subscriber', ['key' => 'translator', 'id' => 'translator.logging.inner']);
  44. }
  45. }
  46. }
  47. }