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