Skip to content

Commit b75e5d5

Browse files
SanderVerkuilkbond
authored andcommitted
Disable cache warmup when translator is identity translator
1 parent d0bb978 commit b75e5d5

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

src/Translator/doc/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ For a better developer experience, TypeScript types definitions are also generat
7171
Then, you will be able to import those JavaScript translations in your assets.
7272
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 <https://webpack.js.org/guides/tree-shaking/>`_.
7373

74+
.. note::
75+
76+
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.
77+
7478
Configuring the default locale
7579
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7680

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Symfony\UX\Translator\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\Translation\TranslatorBagInterface;
8+
9+
class TranslatorCompilerPass implements CompilerPassInterface
10+
{
11+
public function process(ContainerBuilder $container): void
12+
{
13+
if (!$this->hasValidTranslator($container)) {
14+
$container->removeDefinition('ux.translator.cache_warmer.translations_cache_warmer');
15+
}
16+
}
17+
18+
private function hasValidTranslator(ContainerBuilder $containerBuilder): bool
19+
{
20+
if (!$containerBuilder->hasDefinition('translator')) {
21+
return false;
22+
}
23+
24+
$translator = $containerBuilder->getDefinition('translator');
25+
if (!is_subclass_of($translator->getClass(), TranslatorBagInterface::class)) {
26+
return false;
27+
}
28+
return true;
29+
}
30+
}

src/Translator/src/UxTranslatorBundle.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\UX\Translator;
1313

14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
1415
use Symfony\Component\HttpKernel\Bundle\Bundle;
16+
use Symfony\UX\Translator\DependencyInjection\TranslatorCompilerPass;
1517

1618
/**
1719
* @author Hugo Alliaume <hugo@alliau.me>
@@ -26,4 +28,9 @@ public function getPath(): string
2628
{
2729
return \dirname(__DIR__);
2830
}
31+
32+
public function build(ContainerBuilder $container): void
33+
{
34+
$container->addCompilerPass(new TranslatorCompilerPass());
35+
}
2936
}

src/Translator/tests/Kernel/FrameworkAppKernel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public function registerContainerConfiguration(LoaderInterface $loader)
3838
'secret' => '$ecret',
3939
'test' => true,
4040
'translator' => [
41+
'enabled' => match ($this->environment) {
42+
'test_without_translator' => false,
43+
default => true,
44+
},
4145
'fallbacks' => ['en'],
4246
],
4347
'http_method_override' => false,

src/Translator/tests/UxTranslatorBundleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function provideKernels()
2222
{
2323
yield 'empty' => [new EmptyAppKernel('test', true)];
2424
yield 'framework' => [new FrameworkAppKernel('test', true)];
25+
yield 'framework without translator' => [new FrameworkAppKernel('test_without_translator', true)];
2526
}
2627

2728
/**

0 commit comments

Comments
 (0)