Fix optional cache warmers are always instantiated whereas they should be lazy-loaded

This commit is contained in:
Romain Neutron 2017-06-01 14:40:39 +02:00
parent 434c8334ed
commit 3371db9f8c
No known key found for this signature in database
GPG Key ID: 201FC7CF9F0CA3ED
4 changed files with 44 additions and 6 deletions

View File

@ -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;
@ -22,11 +23,24 @@ use Symfony\Component\Translation\TranslatorInterface;
*/
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__));
}
}
/**
@ -34,6 +48,10 @@ class TranslationsCacheWarmer implements CacheWarmerInterface
*/
public function warmUp($cacheDir)
{
if (null === $this->translator) {
$this->translator = $this->container->get('translator');
}
if ($this->translator instanceof WarmableInterface) {
$this->translator->warmUp($cacheDir);
}

View File

@ -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>

View File

@ -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;
@ -22,12 +23,27 @@ use Twig\Error\Error;
*/
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;
}
@ -36,6 +52,10 @@ class TemplateCacheWarmer implements CacheWarmerInterface
*/
public function warmUp($cacheDir)
{
if (null === $this->twig) {
$this->twig = $this->container->get('twig');
}
foreach ($this->iterator as $template) {
try {
$this->twig->loadTemplate($template);

View File

@ -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>