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