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