|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Template; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A template name. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Name |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Instance of the template engine. |
|
15
|
|
|
* @var Engine |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $engine; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The original name. |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The parsed template folder. |
|
27
|
|
|
* @var Folder |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $folder; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The parsed template filename. |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $file; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Create a new Name instance. |
|
39
|
|
|
* @param Engine $engine |
|
40
|
|
|
* @param string $name |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Engine $engine, $name) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->setEngine($engine); |
|
45
|
|
|
$this->setName($name); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set the engine. |
|
50
|
|
|
* @param Engine $engine |
|
51
|
|
|
* @return Name |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setEngine(Engine $engine) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->engine = $engine; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the engine. |
|
62
|
|
|
* @return Engine |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getEngine() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->engine; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the original name and parse it. |
|
71
|
|
|
* @param string $name |
|
72
|
|
|
* @return Name |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setName($name) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->name = $name; |
|
77
|
|
|
|
|
78
|
|
|
$parts = explode('::', $this->name); |
|
79
|
|
|
|
|
80
|
|
|
if (count($parts) === 1) { |
|
81
|
|
|
$this->setFile($parts[0]); |
|
82
|
|
|
} elseif (count($parts) === 2) { |
|
83
|
|
|
$this->setFolder($parts[0]); |
|
84
|
|
|
$this->setFile($parts[1]); |
|
85
|
|
|
} else { |
|
86
|
|
|
throw new LogicException( |
|
87
|
|
|
'The template name "' . $this->name . '" is not valid. ' . |
|
88
|
|
|
'Do not use the folder namespace separator "::" more than once.' |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the original name. |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getName() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->name; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Set the parsed template folder. |
|
106
|
|
|
* @param string $folder |
|
107
|
|
|
* @return Name |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setFolder($folder) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->folder = $this->engine->getFolders()->get($folder); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the parsed template folder. |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getFolder() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->folder; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set the parsed template file. |
|
127
|
|
|
* @param string $file |
|
128
|
|
|
* @return Name |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setFile($file) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($file === '') { |
|
133
|
|
|
throw new LogicException( |
|
134
|
|
|
'The template name "' . $this->name . '" is not valid. ' . |
|
135
|
|
|
'The template name cannot be empty.' |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$this->file = $file; |
|
140
|
|
|
|
|
141
|
|
|
if (!is_null($this->engine->getFileExtension())) { |
|
142
|
|
|
$this->file .= '.' . $this->engine->getFileExtension(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the parsed template file. |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getFile() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->file; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Resolve template path. |
|
159
|
|
|
* @return string |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getPath() |
|
162
|
|
|
{ |
|
163
|
|
|
if (is_null($this->folder)) { |
|
164
|
|
|
return "{$this->getDefaultDirectory()}/{$this->file}"; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$path = "{$this->folder->getPath()}/{$this->file}"; |
|
168
|
|
|
|
|
169
|
|
|
if ( |
|
170
|
|
|
!is_file($path) |
|
171
|
|
|
&& $this->folder->getFallback() |
|
172
|
|
|
&& is_file("{$this->getDefaultDirectory()}/{$this->file}") |
|
173
|
|
|
) { |
|
174
|
|
|
$path = "{$this->getDefaultDirectory()}/{$this->file}"; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $path; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Check if template path exists. |
|
182
|
|
|
* @return boolean |
|
183
|
|
|
*/ |
|
184
|
|
|
public function doesPathExist() |
|
185
|
|
|
{ |
|
186
|
|
|
return is_file($this->getPath()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the default templates directory. |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function getDefaultDirectory() |
|
194
|
|
|
{ |
|
195
|
|
|
$directory = $this->engine->getDirectory(); |
|
196
|
|
|
|
|
197
|
|
|
if (is_null($directory)) { |
|
198
|
|
|
throw new LogicException( |
|
199
|
|
|
'The template name "' . $this->name . '" is not valid. '. |
|
200
|
|
|
'The default directory has not been defined.' |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $directory; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|