1 | <?php |
||
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 | 7 | public function __construct( |
|
37 | |||
38 | /** |
||
39 | * @inheritDoc |
||
40 | */ |
||
41 | 4 | public function getSourceContext($name) |
|
42 | { |
||
43 | 4 | $code = file_get_contents($this->template($name)); |
|
44 | |||
45 | 3 | return new Twig_Source($code, $name); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @inheritDoc |
||
50 | */ |
||
51 | 1 | public function getCacheKey($name) |
|
52 | { |
||
53 | 1 | return $this->template($name); |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * @inheritDoc |
||
58 | */ |
||
59 | 1 | public function isFresh($name, $time) |
|
60 | { |
||
61 | 1 | return filemtime($this->template($name)) <= $time; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @inheritDoc |
||
66 | */ |
||
67 | 1 | public function exists($name) |
|
68 | { |
||
69 | 1 | if (isset($this->cache[$name])) { |
|
70 | 1 | return true; |
|
71 | } |
||
72 | |||
73 | 1 | return (bool) $this->findTemplate($name); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $name |
||
78 | * |
||
79 | * @return string |
||
80 | * |
||
81 | * @throws LoaderException |
||
82 | * When $name is not found. |
||
83 | */ |
||
84 | 5 | private function template($name) |
|
85 | { |
||
86 | 5 | if (isset($this->cache[$name])) { |
|
87 | 1 | return $this->cache[$name]; |
|
88 | } |
||
89 | |||
90 | 5 | $found = $this->findTemplate($name); |
|
91 | 5 | if (null === $found) { |
|
92 | 1 | throw LoaderException::notFound($name, $this->path); |
|
93 | } |
||
94 | |||
95 | 4 | return $this->cache[$name] = $found; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string $name |
||
100 | * |
||
101 | * @return string|null |
||
102 | */ |
||
103 | 5 | private function findTemplate($name) |
|
104 | { |
||
105 | 5 | $files = $this->possibleTemplateFiles($name); |
|
106 | |||
107 | 5 | foreach ($files as $file) { |
|
108 | 5 | $filepath = $this->path . DIRECTORY_SEPARATOR . $file; |
|
109 | 5 | if (is_file($filepath) && is_readable($filepath)) { |
|
110 | 5 | return realpath($filepath); |
|
111 | } |
||
112 | } |
||
113 | |||
114 | 1 | return null; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $name |
||
119 | * |
||
120 | * @return Generator |
||
121 | */ |
||
122 | 5 | private function possibleTemplateFiles($name) |
|
130 | |||
131 | /** |
||
132 | * @param string $name |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 5 | private function normalizeName($name) |
|
143 | } |
||
144 |