TemplateNameParser::parse()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.1243

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 40
ccs 19
cts 22
cp 0.8636
rs 6.7272
cc 7
eloc 22
nc 7
nop 1
crap 7.1243
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Templating;
3
4
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as BaseTemplateNameParser;
5
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference as FrameworkTemplateReference;
6
use Symfony\Component\Templating\TemplateReferenceInterface;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class TemplateNameParser extends BaseTemplateNameParser
12
{
13 15
    public function parse($name)
14
    {
15 15
        if ($name instanceof TemplateReferenceInterface) {
16 1
            return $name;
17 14
        } elseif (isset($this->cache[$name])) {
18 2
            return $this->cache[$name];
19
        }
20
21
        // Not a override
22 14
        if ($name[0] !== '!') {
23 12
            return parent::parse($name);
24
        }
25
26
        // normalize name (see TemplateNameParser line 52)
27 2
        $name = substr($name, 1);
28 2
        $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
29 2
        if (isset($this->cache['!' . $name])) {
30
            return $this->cache['!' . $name];
31
        }
32
33
        // Get the parent reference
34 2
        $reference = parent::parse($name);
35 2
        if (!$reference instanceof FrameworkTemplateReference) {
36
            return $reference;
37
        }
38
39 2
        $bundle = $reference->get('bundle');
40 2
        if (empty($bundle)) {
41
            return $reference;
42
        }
43
44
        // Resolve the base template path
45 2
        $path = $reference->getPath();
46 2
        $relativePath = substr($path, strlen($bundle) + 1);
47
48 2
        $bundles = $this->kernel->getBundle($bundle, false);
49 2
        $relativePath = end($bundles)->getPath() . $relativePath;
50
51 2
        return $this->cache['!' . $name] = new TemplateReference($relativePath, $reference);
52
    }
53
}
54