Skip to content

[3.4] Implement ServiceSubscriberInterface in optional cache warmers #23036

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
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,7 +11,8 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Translation\TranslatorInterface;
Expand All @@ -21,7 +22,7 @@
*
* @author Xavier Leune <xavier.leune@gmail.com>
*/
class TranslationsCacheWarmer implements CacheWarmerInterface
class TranslationsCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
{
private $container;
private $translator;
Expand All @@ -39,7 +40,7 @@ public function __construct($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__));
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
}
}

Expand All @@ -64,4 +65,14 @@ public function isOptional()
{
return true;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'translator' => TranslatorInterface::class,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@
<service id="translation.writer" class="Symfony\Component\Translation\Writer\TranslationWriter" public="true" />

<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer">
<argument type="service" id="service_container" />
<tag name="container.service_subscriber" id="translator" />
<tag name="kernel.cache_warmer" />
<argument type="service" id="Psr\Container\ContainerInterface" />
</service>

<service id="translator_listener" class="Symfony\Component\HttpKernel\EventListener\TranslatorListener" public="true">
Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Symfony\Bundle\TwigBundle\CacheWarmer;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Twig\Environment;
use Twig\Error\Error;
Expand All @@ -21,7 +22,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateCacheWarmer implements CacheWarmerInterface
class TemplateCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
{
private $container;
private $twig;
Expand All @@ -41,7 +42,7 @@ public function __construct($container, \Traversable $iterator)
} 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__));
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
}

$this->iterator = $iterator;
Expand Down Expand Up @@ -73,4 +74,14 @@ public function isOptional()
{
return true;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'twig' => Environment::class,
);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@

<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer">
<tag name="kernel.cache_warmer" />
<argument type="service" id="service_container" />
<tag name="container.service_subscriber" id="twig" />
<argument type="service" id="Psr\Container\ContainerInterface" />
<argument type="service" id="twig.template_iterator" />
</service>

Expand Down