TemplatePathsCacheWarmer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 51
ccs 0
cts 27
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B warmUp() 0 32 5
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\CacheWarmer;
3
4
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
5
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer as BaseTemplatePathsCacheWarmer;
6
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
7
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
8
use Symfony\Component\HttpKernel\KernelInterface;
9
10
/**
11
 * @author Warnar Boekkooi <[email protected]>
12
 */
13
class TemplatePathsCacheWarmer extends BaseTemplatePathsCacheWarmer
14
{
15
    /**
16
     * @var KernelInterface
17
     */
18
    protected $kernel;
19
20
    public function __construct(TemplateFinderInterface $finder, TemplateLocator $locator, KernelInterface $kernel)
21
    {
22
        parent::__construct($finder, $locator);
23
        $this->kernel = $kernel;
24
    }
25
26
    /**
27
     * Warms up the cache.
28
     *
29
     * @param string $cacheDir The cache directory
30
     */
31
    public function warmUp($cacheDir)
32
    {
33
        $templates = array();
34
35
        /** @var \Symfony\Component\Templating\TemplateReferenceInterface $template */
36
        foreach ($this->finder->findAllTemplates() as $template) {
37
            $templates[$template->getLogicalName()] = $this->locator->locate($template);
38
39
            if (!$template instanceof TemplateReference) {
40
                continue;
41
            }
42
43
            $bundle = $template->get('bundle');
44
            if (empty($bundle)) {
45
                continue;
46
            }
47
            // Resolve the base bundle
48
            $bundles = $this->kernel->getBundle($bundle, false);
49
            $baseBundle = end($bundles);
50
51
            // Resolve the base path
52
            $relativePath = substr($template->getPath(), strlen($bundle) + 1);
53
            $baseBundlePath = $baseBundle->getPath() . $relativePath;
54
55
            // Check if the base is the same if not add the override to the template cache
56
            if ($templates[$template->getLogicalName()] !== $baseBundlePath) {
57
                $templates['!'.$template->getLogicalName()] = $baseBundlePath;
58
            }
59
        }
60
61
        $this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
62
    }
63
}
64