Skip to content

Commit e75a8bb

Browse files
committed
Implement ServiceSubscriberInterface in optional cache warmers
1 parent 068f8d1 commit e75a8bb

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14-
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1616
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1717
use Symfony\Component\Translation\TranslatorInterface;
@@ -21,7 +21,7 @@
2121
*
2222
* @author Xavier Leune <xavier.leune@gmail.com>
2323
*/
24-
class TranslationsCacheWarmer implements CacheWarmerInterface
24+
class TranslationsCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
2525
{
2626
private $container;
2727
private $translator;
@@ -39,7 +39,7 @@ public function __construct($container)
3939
} elseif ($container instanceof TranslatorInterface) {
4040
$this->translator = $container;
4141
} else {
42-
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Symfony\Component\Translation\TranslatorInterface as first argument.', __CLASS__));
42+
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
4343
}
4444
}
4545

@@ -64,4 +64,14 @@ public function isOptional()
6464
{
6565
return true;
6666
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public static function getSubscribedServices()
72+
{
73+
return array(
74+
'translator' => TranslatorInterface::class,
75+
);
76+
}
6777
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@
123123
<service id="translation.writer" class="Symfony\Component\Translation\Writer\TranslationWriter" public="true" />
124124

125125
<service id="translation.warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TranslationsCacheWarmer">
126-
<argument type="service" id="service_container" />
126+
<tag name="container.service_subscriber" id="translator" />
127127
<tag name="kernel.cache_warmer" />
128+
<argument type="service" id="Psr\Container\ContainerInterface" />
128129
</service>
129130

130131
<service id="translator_listener" class="Symfony\Component\HttpKernel\EventListener\TranslatorListener" public="true">

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
1313

14-
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1516
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1617
use Twig\Environment;
1718
use Twig\Error\Error;
@@ -21,7 +22,7 @@
2122
*
2223
* @author Fabien Potencier <fabien@symfony.com>
2324
*/
24-
class TemplateCacheWarmer implements CacheWarmerInterface
25+
class TemplateCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
2526
{
2627
private $container;
2728
private $twig;
@@ -41,7 +42,7 @@ public function __construct($container, \Traversable $iterator)
4142
} elseif ($container instanceof Environment) {
4243
$this->twig = $container;
4344
} else {
44-
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Environment as first argument.', __CLASS__));
45+
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
4546
}
4647

4748
$this->iterator = $iterator;
@@ -73,4 +74,14 @@ public function isOptional()
7374
{
7475
return true;
7576
}
77+
78+
/**
79+
* {@inheritdoc}
80+
*/
81+
public static function getSubscribedServices()
82+
{
83+
return array(
84+
'twig' => Environment::class,
85+
);
86+
}
7687
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545

4646
<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer">
4747
<tag name="kernel.cache_warmer" />
48-
<argument type="service" id="service_container" />
48+
<tag name="container.service_subscriber" id="twig" />
49+
<argument type="service" id="Psr\Container\ContainerInterface" />
4950
<argument type="service" id="twig.template_iterator" />
5051
</service>
5152

0 commit comments

Comments
 (0)