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