Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3371db9

Browse filesBrowse files
committed
Fix optional cache warmers are always instantiated whereas they should be lazy-loaded
1 parent 434c833 commit 3371db9
Copy full SHA for 3371db9

File tree

Expand file treeCollapse file tree

4 files changed

+44
-6
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+44
-6
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php
+20-2Lines 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
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml
+1-1Lines 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>

‎src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
+22-2Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
1313

14+
use Symfony\Component\DependencyInjection\ContainerInterface;
1415
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1516
use Twig\Environment;
1617
use Twig\Error\Error;
@@ -22,12 +23,27 @@
2223
*/
2324
class TemplateCacheWarmer implements CacheWarmerInterface
2425
{
26+
private $container;
2527
private $twig;
2628
private $iterator;
2729

28-
public function __construct(Environment $twig, \Traversable $iterator)
30+
/**
31+
* TemplateCacheWarmer constructor.
32+
*
33+
* @param ContainerInterface|Environment $container
34+
* @param \Traversable $iterator
35+
*/
36+
public function __construct($container, \Traversable $iterator)
2937
{
30-
$this->twig = $twig;
38+
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
39+
if ($container instanceof ContainerInterface) {
40+
$this->container = $container;
41+
} elseif ($container instanceof Environment) {
42+
$this->twig = $container;
43+
} else {
44+
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Environment as first argument.', __CLASS__));
45+
}
46+
3147
$this->iterator = $iterator;
3248
}
3349

@@ -36,6 +52,10 @@ public function __construct(Environment $twig, \Traversable $iterator)
3652
*/
3753
public function warmUp($cacheDir)
3854
{
55+
if (null === $this->twig) {
56+
$this->twig = $this->container->get('twig');
57+
}
58+
3959
foreach ($this->iterator as $template) {
4060
try {
4161
$this->twig->loadTemplate($template);

‎src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer" public="false">
6262
<tag name="kernel.cache_warmer" />
63-
<argument type="service" id="twig" />
63+
<argument type="service" id="service_container" />
6464
<argument type="service" id="twig.template_iterator" />
6565
</service>
6666

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.