From b75e5d5f21fedcc28a23ba70c385bc137d9f8bf6 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Fri, 5 Jul 2024 22:06:42 +0200 Subject: [PATCH] Disable cache warmup when translator is identity translator --- src/Translator/doc/index.rst | 4 +++ .../TranslatorCompilerPass.php | 30 +++++++++++++++++++ src/Translator/src/UxTranslatorBundle.php | 7 +++++ .../tests/Kernel/FrameworkAppKernel.php | 4 +++ .../tests/UxTranslatorBundleTest.php | 1 + 5 files changed, 46 insertions(+) create mode 100644 src/Translator/src/DependencyInjection/TranslatorCompilerPass.php diff --git a/src/Translator/doc/index.rst b/src/Translator/doc/index.rst index 4872d569f6d..011ce6ae068 100644 --- a/src/Translator/doc/index.rst +++ b/src/Translator/doc/index.rst @@ -71,6 +71,10 @@ For a better developer experience, TypeScript types definitions are also generat Then, you will be able to import those JavaScript translations in your assets. Don't worry about your final bundle size, only the translations you use will be included in your final bundle, thanks to the `tree shaking `_. +.. note:: + + This package requires the `translator` to be enabled in your Symfony application. If you don't use the `translator` service, the warmup command will not generate any translations. + Configuring the default locale ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/Translator/src/DependencyInjection/TranslatorCompilerPass.php b/src/Translator/src/DependencyInjection/TranslatorCompilerPass.php new file mode 100644 index 00000000000..9262c137437 --- /dev/null +++ b/src/Translator/src/DependencyInjection/TranslatorCompilerPass.php @@ -0,0 +1,30 @@ +hasValidTranslator($container)) { + $container->removeDefinition('ux.translator.cache_warmer.translations_cache_warmer'); + } + } + + private function hasValidTranslator(ContainerBuilder $containerBuilder): bool + { + if (!$containerBuilder->hasDefinition('translator')) { + return false; + } + + $translator = $containerBuilder->getDefinition('translator'); + if (!is_subclass_of($translator->getClass(), TranslatorBagInterface::class)) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/src/Translator/src/UxTranslatorBundle.php b/src/Translator/src/UxTranslatorBundle.php index 91fa829a11b..ff6c74928aa 100644 --- a/src/Translator/src/UxTranslatorBundle.php +++ b/src/Translator/src/UxTranslatorBundle.php @@ -11,7 +11,9 @@ namespace Symfony\UX\Translator; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\UX\Translator\DependencyInjection\TranslatorCompilerPass; /** * @author Hugo Alliaume @@ -26,4 +28,9 @@ public function getPath(): string { return \dirname(__DIR__); } + + public function build(ContainerBuilder $container): void + { + $container->addCompilerPass(new TranslatorCompilerPass()); + } } diff --git a/src/Translator/tests/Kernel/FrameworkAppKernel.php b/src/Translator/tests/Kernel/FrameworkAppKernel.php index 98c0ca6a703..2686ab7e54d 100644 --- a/src/Translator/tests/Kernel/FrameworkAppKernel.php +++ b/src/Translator/tests/Kernel/FrameworkAppKernel.php @@ -38,6 +38,10 @@ public function registerContainerConfiguration(LoaderInterface $loader) 'secret' => '$ecret', 'test' => true, 'translator' => [ + 'enabled' => match ($this->environment) { + 'test_without_translator' => false, + default => true, + }, 'fallbacks' => ['en'], ], 'http_method_override' => false, diff --git a/src/Translator/tests/UxTranslatorBundleTest.php b/src/Translator/tests/UxTranslatorBundleTest.php index 8f244289558..fe17709d8b0 100644 --- a/src/Translator/tests/UxTranslatorBundleTest.php +++ b/src/Translator/tests/UxTranslatorBundleTest.php @@ -22,6 +22,7 @@ public static function provideKernels() { yield 'empty' => [new EmptyAppKernel('test', true)]; yield 'framework' => [new FrameworkAppKernel('test', true)]; + yield 'framework without translator' => [new FrameworkAppKernel('test_without_translator', true)]; } /**