| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\DependencyInjection; |
| 13 |
|
| 14 |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 15 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 |
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 17 |
use Symfony\Component\Translation\TranslatorBagInterface; |
| 18 |
use Symfony\Contracts\Translation\TranslatorInterface; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class LoggingTranslatorPass implements CompilerPassInterface |
| 24 |
{ |
| 25 |
public function process(ContainerBuilder $container): void |
| 26 |
{ |
| 27 |
if (!$container->hasAlias('logger') || !$container->hasAlias('translator')) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
if (!$container->hasParameter('translator.logging') || !$container->getParameter('translator.logging')) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$translatorAlias = $container->getAlias('translator'); |
| 36 |
$definition = $container->getDefinition((string) $translatorAlias); |
| 37 |
$class = $container->getParameterBag()->resolveValue($definition->getClass()); |
| 38 |
|
| 39 |
if (!$r = $container->getReflectionClass($class)) { |
| 40 |
throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $translatorAlias)); |
| 41 |
} |
| 42 |
|
| 43 |
if (!$r->isSubclassOf(TranslatorInterface::class) || !$r->isSubclassOf(TranslatorBagInterface::class)) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
$container->getDefinition('translator.logging')->setDecoratedService('translator'); |
| 48 |
$warmer = $container->getDefinition('translation.warmer'); |
| 49 |
$subscriberAttributes = $warmer->getTag('container.service_subscriber'); |
| 50 |
$warmer->clearTag('container.service_subscriber'); |
| 51 |
|
| 52 |
foreach ($subscriberAttributes as $k => $v) { |
| 53 |
if ((!isset($v['id']) || 'translator' !== $v['id']) && (!isset($v['key']) || 'translator' !== $v['key'])) { |
| 54 |
$warmer->addTag('container.service_subscriber', $v); |
| 55 |
} |
| 56 |
} |
| 57 |
$warmer->addTag('container.service_subscriber', ['key' => 'translator', 'id' => 'translator.logging.inner']); |
| 58 |
} |
| 59 |
} |