Completed
Push — master ( f23011...908f75 )
by Asmir
26:03 queued 16:05
created

TemplateCacheCacheWarmer::warmUp()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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