TemplateCacheCacheWarmer::warmUp()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Goetas\TwitalBundle\CacheWarmer;
4
5
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
8
use Twig\Error\Error;
9
10
class TemplateCacheCacheWarmer implements CacheWarmerInterface
11
{
12
    protected $container;
13
    protected $finder;
14
15
    /**
16
     * Constructor.
17
     *
18
     * @param ContainerInterface $container The dependency injection container
19
     * @param TemplateFinderInterface $finder The template paths cache warmer
20
     */
21
    public function __construct(ContainerInterface $container, TemplateFinderInterface $finder)
22
    {
23
        // We don't inject the Twig environment directly as it depends on the
24
        // template locator (via the loader) which might be a cached one.
25
        // The cached template locator is available once the TemplatePathsCacheWarmer
26
        // has been warmed up
27
        $this->container = $container;
28
        $this->finder = $finder;
29
    }
30
31
    /**
32
     * Warms up the cache.
33
     *
34
     * @param string $cacheDir The cache directory
35
     */
36
    public function warmUp($cacheDir)
37
    {
38
        $twig = $this->container->get('twig');
39
40
        foreach ($this->finder->findAllTemplates() as $template) {
41
42
            if ('twital' !== $template->get('engine')) {
43
                continue;
44
            }
45
46
            try {
47
                $twig->loadTemplate($template);
48
            } catch (Error $e) {
49
                // problem during compilation, give up
50
            }
51
        }
52
    }
53
54
    /**
55
     * Checks whether this warmer is optional or not.
56
     *
57
     * @return bool    always true
58
     */
59
    public function isOptional()
60
    {
61
        return true;
62
    }
63
}
64