1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asmaster\EquipTwig\Loader; |
4
|
|
|
|
5
|
|
|
use Asmaster\EquipTwig\Exception\LoaderException; |
6
|
|
|
use Twig_ExistsLoaderInterface as TwigExistsLoader; |
7
|
|
|
use Twig_LoaderInterface as TwigLoader; |
8
|
|
|
|
9
|
|
|
final class FilesystemLoader implements TwigLoader, TwigExistsLoader |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $path; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $fileExtensions = ['html.twig', 'twig']; |
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
|
|
|
public function __construct($path, array $fileExtensions = null) |
31
|
|
|
{ |
32
|
|
|
$this->path = $this->realFilePath($path); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if ($fileExtensions) { |
35
|
|
|
$this->fileExtensions = $fileExtensions; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @inheritDoc |
41
|
|
|
*/ |
42
|
|
|
public function getSource($name) |
43
|
|
|
{ |
44
|
|
|
return file_get_contents($this->template($name)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
|
|
public function getCacheKey($name) |
51
|
|
|
{ |
52
|
|
|
return $this->template($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritDoc |
57
|
|
|
*/ |
58
|
|
|
public function isFresh($name, $time) |
59
|
|
|
{ |
60
|
|
|
return filemtime($this->template($name)) <= $time; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
|
|
public function exists($name) |
67
|
|
|
{ |
68
|
|
|
if (isset($this->cache[$name])) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return (bool) $this->findTemplate($name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @throws LoaderException When $name is not found |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function template($name) |
83
|
|
|
{ |
84
|
|
|
if (isset($this->cache[$name])) { |
85
|
|
|
return $this->cache[$name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$found = $this->findTemplate($name); |
89
|
|
|
if (!$found) { |
|
|
|
|
90
|
|
|
throw LoaderException::notFound($name, $this->path); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->cache[$name] = $found; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $name |
98
|
|
|
* |
99
|
|
|
* @return string|null |
100
|
|
|
*/ |
101
|
|
|
private function findTemplate($name) |
102
|
|
|
{ |
103
|
|
|
$files = $this->possibleTemplateFiles($name); |
104
|
|
|
|
105
|
|
|
foreach($files as $file) { |
106
|
|
|
$filepath = $this->path . DIRECTORY_SEPARATOR . $file; |
107
|
|
|
if (is_file($filepath)) { |
108
|
|
|
return $filepath; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $name |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
private function possibleTemplateFiles($name) |
121
|
|
|
{ |
122
|
|
|
$name = $this->normalizeName($name); |
123
|
|
|
|
124
|
|
|
$templates = [$name]; |
125
|
|
|
foreach($this->fileExtensions as $extension) { |
126
|
|
|
$templates[] = "$name.$extension"; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $templates; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $name |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
private function normalizeName($name) |
138
|
|
|
{ |
139
|
|
|
return preg_replace('#/{2,}#', DIRECTORY_SEPARATOR, |
140
|
|
|
str_replace('\\', DIRECTORY_SEPARATOR, $name) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $file |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function realFilePath($file) |
150
|
|
|
{ |
151
|
|
|
$realpath = realpath($file); |
152
|
|
|
if (false !== $realpath) { |
153
|
|
|
return $realpath; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $file; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.