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
|
7 |
|
public function __construct( |
31
|
|
|
$path, |
32
|
|
|
array $fileExtensions = ['html.twig', 'twig'] |
33
|
|
|
) { |
34
|
7 |
|
$this->path = $path; |
35
|
7 |
|
$this->fileExtensions = $fileExtensions; |
36
|
7 |
|
} |
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) |
123
|
|
|
{ |
124
|
5 |
|
yield $name = $this->normalizeName($name); |
125
|
|
|
|
126
|
2 |
|
foreach ($this->fileExtensions as $extension) { |
127
|
2 |
|
yield "{$name}.{$extension}"; |
128
|
|
|
} |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
5 |
|
private function normalizeName($name) |
137
|
|
|
{ |
138
|
5 |
|
return preg_replace('#/{2,}#', |
139
|
5 |
|
DIRECTORY_SEPARATOR, |
140
|
5 |
|
str_replace('\\', DIRECTORY_SEPARATOR, $name) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|