Skip to content

Commit e00192d

Browse files
committed
Fix optional cache warmers are always instantiated whereas they should be lazy-loaded
1 parent 700a51c commit e00192d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

CacheWarmer/TranslationsCacheWarmer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1415
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1516
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1617
use Symfony\Component\Translation\TranslatorInterface;
@@ -22,18 +23,35 @@
2223
*/
2324
class TranslationsCacheWarmer implements CacheWarmerInterface
2425
{
26+
private $container;
2527
private $translator;
2628

27-
public function __construct(TranslatorInterface $translator)
29+
/**
30+
* TranslationsCacheWarmer constructor.
31+
*
32+
* @param ContainerInterface|TranslatorInterface $container
33+
*/
34+
public function __construct($container)
2835
{
29-
$this->translator = $translator;
36+
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
37+
if ($container instanceof ContainerInterface) {
38+
$this->container = $container;
39+
} elseif ($container instanceof TranslatorInterface) {
40+
$this->translator = $container;
41+
} else {
42+
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Symfony\Component\Translation\TranslatorInterface as first argument.', __CLASS__));
43+
}
3044
}
3145

3246
/**
3347
* {@inheritdoc}
3448
*/
3549
public function warmUp($cacheDir)
3650
{
51+
if (null === $this->translator) {
52+
$this->translator = $this->container->get('translator');
53+
}
54+
3755
if ($this->translator instanceof WarmableInterface) {
3856
$this->translator->warmUp($cacheDir);
3957
}

Resources/config/translation.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
<service id="translation.writer" class="%translation.writer.class%"/>
160160

161161
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer" public="false">
162-
<argument type="service" id="translator" />
162+
<argument type="service" id="service_container" />
163163
<tag name="kernel.cache_warmer" />
164164
</service>
165165
</services>

0 commit comments

Comments
 (0)