File tree Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Expand file tree Collapse file tree 2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \CacheWarmer ;
13
13
14
+ use Symfony \Component \DependencyInjection \ContainerInterface ;
14
15
use Symfony \Component \HttpKernel \CacheWarmer \CacheWarmerInterface ;
15
16
use Symfony \Component \HttpKernel \CacheWarmer \WarmableInterface ;
16
17
use Symfony \Component \Translation \TranslatorInterface ;
22
23
*/
23
24
class TranslationsCacheWarmer implements CacheWarmerInterface
24
25
{
26
+ private $ container ;
25
27
private $ translator ;
26
28
27
- public function __construct (TranslatorInterface $ translator )
29
+ /**
30
+ * TranslationsCacheWarmer constructor.
31
+ *
32
+ * @param ContainerInterface|TranslatorInterface $container
33
+ */
34
+ public function __construct ($ container )
28
35
{
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
+ }
30
44
}
31
45
32
46
/**
33
47
* {@inheritdoc}
34
48
*/
35
49
public function warmUp ($ cacheDir )
36
50
{
51
+ if (null === $ this ->translator ) {
52
+ $ this ->translator = $ this ->container ->get ('translator ' );
53
+ }
54
+
37
55
if ($ this ->translator instanceof WarmableInterface) {
38
56
$ this ->translator ->warmUp ($ cacheDir );
39
57
}
Original file line number Diff line number Diff line change 159
159
<service id =" translation.writer" class =" %translation.writer.class%" />
160
160
161
161
<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 " />
163
163
<tag name =" kernel.cache_warmer" />
164
164
</service >
165
165
</services >
You can’t perform that action at this time.
0 commit comments