From 5b68aa346b67290414bcbe0eabe5405730502fba Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 01:19:21 +0200 Subject: [PATCH 1/7] Make the translator null when its invalid. --- src/Translator/config/services.php | 2 +- src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Translator/config/services.php b/src/Translator/config/services.php index e5eb6bab50b..fe042e3aa1f 100644 --- a/src/Translator/config/services.php +++ b/src/Translator/config/services.php @@ -24,7 +24,7 @@ $container->services() ->set('ux.translator.cache_warmer.translations_cache_warmer', TranslationsCacheWarmer::class) ->args([ - service('translator'), + service('translator')->nullOnInvalid(), service('ux.translator.translations_dumper'), ]) ->tag('kernel.cache_warmer') diff --git a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php index 2411772375c..6e04a4cffcf 100644 --- a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php @@ -23,7 +23,7 @@ class TranslationsCacheWarmer implements CacheWarmerInterface { public function __construct( - private TranslatorBagInterface $translatorBag, + private ?TranslatorBagInterface $translatorBag, private TranslationsDumper $translationsDumper, ) { } @@ -35,6 +35,9 @@ public function isOptional(): bool public function warmUp(string $cacheDir, ?string $buildDir = null): array { + if ($this->translatorBag === null) { + return []; + } $this->translationsDumper->dump( ...$this->translatorBag->getCatalogues() ); From d06db23da8cc3f338f7c2af592ab6d3931da51c8 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 01:22:43 +0200 Subject: [PATCH 2/7] Updated documentation --- src/Translator/doc/index.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Translator/doc/index.rst b/src/Translator/doc/index.rst index 614f47991f7..0ac060960ce 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` package, the warmup command will not generate any translations. + Configuring the dumped translations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From 60ca5b67bb9365a4af9cf7a761a578fe444c6b26 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 01:32:09 +0200 Subject: [PATCH 3/7] Added logger and added a test scenario --- src/Translator/config/services.php | 1 + .../CacheWarmer/TranslationsCacheWarmer.php | 5 +++- .../TranslationsCacheWarmerTest.php | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Translator/config/services.php b/src/Translator/config/services.php index fe042e3aa1f..363081459dd 100644 --- a/src/Translator/config/services.php +++ b/src/Translator/config/services.php @@ -26,6 +26,7 @@ ->args([ service('translator')->nullOnInvalid(), service('ux.translator.translations_dumper'), + service('logger')->ignoreOnInvalid(), ]) ->tag('kernel.cache_warmer') diff --git a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php index 6e04a4cffcf..a6d5253d1d6 100644 --- a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php @@ -11,6 +11,7 @@ namespace Symfony\UX\Translator\CacheWarmer; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\Translation\TranslatorBagInterface; use Symfony\UX\Translator\TranslationsDumper; @@ -25,6 +26,7 @@ class TranslationsCacheWarmer implements CacheWarmerInterface public function __construct( private ?TranslatorBagInterface $translatorBag, private TranslationsDumper $translationsDumper, + private readonly ?LoggerInterface $logger = null, ) { } @@ -35,7 +37,8 @@ public function isOptional(): bool public function warmUp(string $cacheDir, ?string $buildDir = null): array { - if ($this->translatorBag === null) { + if (null === $this->translatorBag) { + $this->logger?->warning('Translator bag not available'); return []; } $this->translationsDumper->dump( diff --git a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php index d97e49e0967..fe52700062d 100644 --- a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php +++ b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php @@ -12,6 +12,7 @@ namespace Symfony\UX\Translator\Tests\CacheWarmer; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\TranslatorBag; use Symfony\UX\Translator\CacheWarmer\TranslationsCacheWarmer; @@ -55,4 +56,26 @@ public function test() $translationsCacheWarmer->warmUp(self::$cacheDir); } + + public function testWithoutTranslator() + { + $translationsDumperMock = $this->createMock(TranslationsDumper::class); + $translationsDumperMock + ->expects($this->never()) + ->method('dump'); + + $loggerMock = $this->createMock(LoggerInterface::class); + $loggerMock + ->expects($this->once()) + ->method('warning') + ->with('Translator bag not available'); + + $translationsCacheWarmer = new TranslationsCacheWarmer( + null, + $translationsDumperMock, + $loggerMock, + ); + + $translationsCacheWarmer->warmUp(self::$cacheDir); + } } From 45ade0552772d43a2853c8bf3f94480b1f6644ad Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 01:35:11 +0200 Subject: [PATCH 4/7] chore: php-cs-fixer --- src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php index a6d5253d1d6..4e019df6bf8 100644 --- a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php @@ -39,6 +39,7 @@ public function warmUp(string $cacheDir, ?string $buildDir = null): array { if (null === $this->translatorBag) { $this->logger?->warning('Translator bag not available'); + return []; } $this->translationsDumper->dump( From 8219a2d3f5a20940e3704e232ffde0481817d766 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 14:54:17 +0200 Subject: [PATCH 5/7] Update src/Translator/doc/index.rst Co-authored-by: Alexis Lefebvre --- src/Translator/doc/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Translator/doc/index.rst b/src/Translator/doc/index.rst index 0ac060960ce..a61eeaaa693 100644 --- a/src/Translator/doc/index.rst +++ b/src/Translator/doc/index.rst @@ -73,7 +73,7 @@ Don't worry about your final bundle size, only the translations you use will be .. note:: - This package requires the ``translator`` to be enabled in your Symfony application. If you don't use the `translator` package, the warmup command will not generate any translations. + This package requires the ``translator`` package to be enabled in your Symfony application. If you don't use the ``translator`` package, the warmup command will not generate any translations. Configuring the dumped translations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From a5486812608397e0f861c29ca543211098a3c5f4 Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 15:31:32 +0200 Subject: [PATCH 6/7] The `TranslatorBag` is no `TranslatorInterface` Leverage union types to properly set up the configuration --- src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php | 5 +++-- .../tests/CacheWarmer/TranslationsCacheWarmerTest.php | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php index 4e019df6bf8..f58630bf667 100644 --- a/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php +++ b/src/Translator/src/CacheWarmer/TranslationsCacheWarmer.php @@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; use Symfony\Component\Translation\TranslatorBagInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\UX\Translator\TranslationsDumper; /** @@ -24,7 +25,7 @@ class TranslationsCacheWarmer implements CacheWarmerInterface { public function __construct( - private ?TranslatorBagInterface $translatorBag, + private TranslatorInterface|TranslatorBagInterface|null $translatorBag, private TranslationsDumper $translationsDumper, private readonly ?LoggerInterface $logger = null, ) { @@ -37,7 +38,7 @@ public function isOptional(): bool public function warmUp(string $cacheDir, ?string $buildDir = null): array { - if (null === $this->translatorBag) { + if (!$this->translatorBag instanceof TranslatorBagInterface) { $this->logger?->warning('Translator bag not available'); return []; diff --git a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php index fe52700062d..a573e6930ad 100644 --- a/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php +++ b/src/Translator/tests/CacheWarmer/TranslationsCacheWarmerTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\TranslatorBag; use Symfony\UX\Translator\CacheWarmer\TranslationsCacheWarmer; use Symfony\UX\Translator\TranslationsDumper; From 6143bbebbdafe610e9d55d00dc8ac84b975423fb Mon Sep 17 00:00:00 2001 From: Sander Verkuil Date: Wed, 14 Aug 2024 17:41:45 +0200 Subject: [PATCH 7/7] Re-added the test for the container with disabled translator --- src/Translator/tests/Kernel/FrameworkAppKernel.php | 4 ++++ src/Translator/tests/UxTranslatorBundleTest.php | 1 + 2 files changed, 5 insertions(+) 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)]; } /**