1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Template\ResolveTemplatePath; |
4
|
|
|
|
5
|
|
|
use League\Plates\Exception\TemplateNotFound; |
6
|
|
|
use League\Plates\Template\Name; |
7
|
|
|
use League\Plates\Template\ResolveTemplatePath; |
8
|
|
|
use LogicException; |
9
|
|
|
|
10
|
|
|
/** Resolves the path from the logic in the Name class which resolves via folder lookup, and then the default directory */ |
11
|
|
|
final class NameAndFolderResolveTemplatePath implements ResolveTemplatePath |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Identifier of the main namespace. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const MAIN_NAMESPACE = '__main__'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Collection of template folders. |
22
|
|
|
*/ |
23
|
|
|
protected $paths = []; |
24
|
|
|
|
25
|
|
|
public function __invoke(Name $name): string |
26
|
|
|
{ |
27
|
|
|
$path = $this->resolvePath($name); |
28
|
|
|
if (is_file($path)) { |
29
|
|
|
return $path; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
throw new TemplateNotFound( |
33
|
|
|
$name->getName(), |
34
|
|
|
[$path], |
35
|
|
|
'The template "'.$name->getName().'" could not be found at "'.$path.'".' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function exists(Name $name): bool |
40
|
|
|
{ |
41
|
|
|
$path = $this->resolvePath($name); |
42
|
|
|
if (is_file($path)) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function resolvePath(Name $name) |
50
|
|
|
{ |
51
|
|
|
$namespace = $this->normalizeNamespace($name->getNamespace()); |
52
|
|
|
$shortPath = $name->getPath(false); |
53
|
|
|
$shortPath = $this->normalizePath($shortPath, $name); |
54
|
|
|
|
55
|
|
|
$fullPath = null; |
|
|
|
|
56
|
|
|
$paths = $this->getPaths($namespace); |
57
|
|
|
foreach ($paths as $path) { |
58
|
|
|
$fullPath = $path.'/'.$shortPath; |
59
|
|
|
if (is_file($fullPath)) { |
60
|
|
|
return $fullPath; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$defaultDirectory = $this->getDefaultDirectory($name); |
65
|
|
|
|
66
|
|
|
return "{$defaultDirectory}/{$shortPath}"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the paths to the templates. |
71
|
|
|
*/ |
72
|
|
|
public function getPaths(string $namespace = self::MAIN_NAMESPACE): array |
73
|
|
|
{ |
74
|
|
|
$namespace = $this->normalizeNamespace($namespace); |
75
|
|
|
|
76
|
|
|
return $this->paths[$namespace] ?? []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the path namespaces. |
81
|
|
|
* |
82
|
|
|
* The main namespace is always defined. |
83
|
|
|
*/ |
84
|
|
|
public function getNamespaces(): array |
85
|
|
|
{ |
86
|
|
|
return array_keys($this->paths); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string|array $paths A path or an array of paths where to look for templates |
91
|
|
|
*/ |
92
|
|
|
public function setPaths($paths, string $namespace = self::MAIN_NAMESPACE): void |
93
|
|
|
{ |
94
|
|
|
if (!\is_array($paths)) { |
95
|
|
|
$paths = [$paths]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->paths[$namespace] = []; |
99
|
|
|
foreach ($paths as $path) { |
100
|
|
|
$this->addPath($path, $namespace); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws \Exception |
106
|
|
|
*/ |
107
|
|
|
public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE) |
108
|
|
|
{ |
109
|
|
|
if (!is_dir($path)) { |
110
|
|
|
throw new \Exception(sprintf('The "%s" directory does not exist.', $path)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->paths[$namespace][] = rtrim($path, '/\\'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function prependPath(string $path, string $namespace = self::MAIN_NAMESPACE): void |
117
|
|
|
{ |
118
|
|
|
if (!is_dir($path)) { |
119
|
|
|
throw new \Exception(sprintf('The "%s" directory does not exist.', $path)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$path = rtrim($path, '/\\'); |
123
|
|
|
|
124
|
|
|
if (!isset($this->paths[$namespace])) { |
125
|
|
|
$this->paths[$namespace][] = $path; |
126
|
|
|
} else { |
127
|
|
|
array_unshift($this->paths[$namespace], $path); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function normalizePath($path, Name $name): string |
132
|
|
|
{ |
133
|
|
|
if (!is_null($name->getEngine()->getFileExtension())) { |
134
|
|
|
$path .= '.'.$name->getEngine()->getFileExtension(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $path; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the default templates directory. |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function getDefaultDirectory(Name $name) |
145
|
|
|
{ |
146
|
|
|
$directory = $name->getEngine()->getDirectory(); |
147
|
|
|
|
148
|
|
|
if (is_null($directory)) { |
149
|
|
|
throw new LogicException( |
150
|
|
|
'The template name "'.$name->getName().'" is not valid. '. |
151
|
|
|
'The default directory has not been defined.' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $directory; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function normalizeNamespace($namespace = self::MAIN_NAMESPACE): string |
159
|
|
|
{ |
160
|
|
|
if (empty($namespace)) { |
161
|
|
|
return self::MAIN_NAMESPACE; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $namespace; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.