|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\TwigBundle\CacheWarmer; |
|
13 |
|
14 use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
|
15 use Symfony\Component\DependencyInjection\ContainerInterface; |
|
16 use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface; |
|
17 |
|
18 /** |
|
19 * Generates the Twig cache for all templates. |
|
20 * |
|
21 * This warmer must be registered after TemplatePathsCacheWarmer, |
|
22 * as the Twig loader will need the cache generated by it. |
|
23 * |
|
24 * @author Fabien Potencier <fabien@symfony.com> |
|
25 */ |
|
26 class TemplateCacheCacheWarmer implements CacheWarmerInterface |
|
27 { |
|
28 protected $container; |
|
29 protected $warmer; |
|
30 |
|
31 /** |
|
32 * Constructor. |
|
33 * |
|
34 * @param ContainerInterface $container The dependency injection container |
|
35 * @param TemplateFinderInterface $finder The template paths cache warmer |
|
36 */ |
|
37 public function __construct(ContainerInterface $container, TemplateFinderInterface $finder) |
|
38 { |
|
39 // We don't inject the Twig environment directly as it depends on the |
|
40 // template locator (via the loader) which might be a cached one. |
|
41 // The cached template locator is available once the TemplatePathsCacheWarmer |
|
42 // has been warmed up |
|
43 $this->container = $container; |
|
44 $this->finder = $finder; |
|
45 } |
|
46 |
|
47 /** |
|
48 * Warms up the cache. |
|
49 * |
|
50 * @param string $cacheDir The cache directory |
|
51 */ |
|
52 public function warmUp($cacheDir) |
|
53 { |
|
54 $twig = $this->container->get('twig'); |
|
55 |
|
56 foreach ($this->finder->findAllTemplates() as $template) { |
|
57 if ('twig' !== $template->get('engine')) { |
|
58 continue; |
|
59 } |
|
60 |
|
61 try { |
|
62 $twig->loadTemplate($template); |
|
63 } catch (\Twig_Error $e) { |
|
64 // problem during compilation, give up |
|
65 } |
|
66 } |
|
67 } |
|
68 |
|
69 /** |
|
70 * Checks whether this warmer is optional or not. |
|
71 * |
|
72 * @return Boolean always true |
|
73 */ |
|
74 public function isOptional() |
|
75 { |
|
76 return true; |
|
77 } |
|
78 } |