Skip to content

[2.8]Fix optional cache warmers are always instantiated whereas they should be lazy-loaded #23014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Translation\TranslatorInterface;
Expand All @@ -22,18 +23,35 @@
*/
class TranslationsCacheWarmer implements CacheWarmerInterface
{
private $container;
private $translator;

public function __construct(TranslatorInterface $translator)
/**
* TranslationsCacheWarmer constructor.
*
* @param ContainerInterface|TranslatorInterface $container
*/
public function __construct($container)
{
$this->translator = $translator;
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
if ($container instanceof ContainerInterface) {
$this->container = $container;
} elseif ($container instanceof TranslatorInterface) {
$this->translator = $container;
} else {
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Symfony\Component\Translation\TranslatorInterface as first argument.', __CLASS__));
}
}

/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
if (null === $this->translator) {
$this->translator = $this->container->get('translator');
}

if ($this->translator instanceof WarmableInterface) {
$this->translator->warmUp($cacheDir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<service id="translation.writer" class="%translation.writer.class%"/>

<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer" public="false">
<argument type="service" id="translator" />
<argument type="service" id="service_container" />
<tag name="kernel.cache_warmer" />
</service>
</services>
Expand Down
24 changes: 22 additions & 2 deletions src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\TwigBundle\CacheWarmer;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Twig\Environment;
use Twig\Error\Error;
Expand All @@ -22,12 +23,27 @@
*/
class TemplateCacheWarmer implements CacheWarmerInterface
{
private $container;
private $twig;
private $iterator;

public function __construct(Environment $twig, \Traversable $iterator)
/**
* TemplateCacheWarmer constructor.
*
* @param ContainerInterface|Environment $container
* @param \Traversable $iterator
*/
public function __construct($container, \Traversable $iterator)
{
$this->twig = $twig;
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
if ($container instanceof ContainerInterface) {
$this->container = $container;
} elseif ($container instanceof Environment) {
$this->twig = $container;
} else {
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Environment as first argument.', __CLASS__));
}

$this->iterator = $iterator;
}

Expand All @@ -36,6 +52,10 @@ public function __construct(Environment $twig, \Traversable $iterator)
*/
public function warmUp($cacheDir)
{
if (null === $this->twig) {
$this->twig = $this->container->get('twig');
}

foreach ($this->iterator as $template) {
try {
$this->twig->loadTemplate($template);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer" public="false">
<tag name="kernel.cache_warmer" />
<argument type="service" id="twig" />
<argument type="service" id="service_container" />
<argument type="service" id="twig.template_iterator" />
</service>

Expand Down