Completed
Push — master ( 9ef2ca...5d5d50 )
by Alex
06:12 queued 03:03
created

FilesystemLoader::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AlexMasterov\EquipTwig\Loader;
4
5
use AlexMasterov\EquipTwig\Exception\LoaderException;
6
use Twig_LoaderInterface;
7
use Twig_Source;
8
9
final class FilesystemLoader implements Twig_LoaderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var array
18
     */
19
    private $fileExtensions = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $cache = [];
25
26
    /**
27
     * @param string $path           The template directory path
28
     * @param array  $fileExtensions The template file extensions
29
     */
30 6
    public function __construct(
31
        $path,
32
        array $fileExtensions = ['html.twig', 'twig']
33
    ) {
34 6
        $this->path = $path;
35 6
        $this->fileExtensions = $fileExtensions;
36 6
    }
37
38
    public function getSourceContext($name)
39
    {
40
        $code = file_get_contents($this->template($name));
41 4
42
        return new Twig_Source($code, $name);
43 4
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function getCacheKey($name)
49 1
    {
50
        return $this->template($name);
51 1
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function isFresh($name, $time)
57 1
    {
58
        return filemtime($this->template($name)) <= $time;
59 1
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function exists($name)
65 1
    {
66
        if (isset($this->cache[$name])) {
67 1
            return true;
68 1
        }
69
70
        return (bool) $this->findTemplate($name);
71 1
    }
72
73
    /**
74
     * @param string $name
75
     *
76
     * @return string
77
     *
78
     * @throws LoaderException
79
     *  When $name is not found.
80
     */
81
    private function template($name)
82 5
    {
83
        if (isset($this->cache[$name])) {
84 5
            return $this->cache[$name];
85 1
        }
86
87
        $found = $this->findTemplate($name);
88 5
        if (null === $found) {
89 5
            throw LoaderException::notFound($name, $this->path);
90 1
        }
91
92
        return $this->cache[$name] = $found;
93 4
    }
94
95
    /**
96
     * @param string $name
97
     *
98
     * @return string|null
99
     */
100
    private function findTemplate($name)
101 5
    {
102
        $files = $this->possibleTemplateFiles($name);
103 5
104
        foreach($files as $file) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
105 5
            $filepath = $this->path . DIRECTORY_SEPARATOR . $file;
106 5
            if (is_file($filepath) && is_readable($filepath)) {
107 5
                return realpath($filepath);
108 5
            }
109
        }
110
111
        return null;
112 1
    }
113
114
    /**
115
     * @param string $name
116
     *
117
     * @return array
118
     */
119
    private function possibleTemplateFiles($name)
120 5
    {
121
        $name = $this->normalizeName($name);
122 5
123
        $templates = [$name];
124 5
        foreach($this->fileExtensions as $extension) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
125 5
            $templates[] = "{$name}.{$extension}";
126 5
        }
127
128
        return $templates;
129 5
    }
130
131
    /**
132
     * @param string $name
133
     *
134
     * @return string
135
     */
136
    private function normalizeName($name)
137 5
    {
138
        return preg_replace('#/{2,}#',
139 5
            DIRECTORY_SEPARATOR,
140 5
            str_replace('\\', DIRECTORY_SEPARATOR, $name)
141
        );
142
    }
143
}
144