1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Site; |
4
|
|
|
|
5
|
|
|
use Brendt\Html\Meta\Meta; |
6
|
|
|
use Brendt\Stitcher\Exception\TemplateNotFoundException; |
7
|
|
|
use Brendt\Stitcher\Site\Http\Header; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A Page object represents a page entry configured in a YAML file located in the `src/sites/` directory. |
11
|
|
|
* Constructing a new Page requires a unique ID and an array of data. This array can hold several different arguments: |
12
|
|
|
* |
13
|
|
|
* - `template`: the only required argument. This variable is a path to a template file. |
14
|
|
|
* This path is relative to the `directories.src` or `directories.template` configuration entry |
15
|
|
|
* @see \Brendt\Stitcher\Stitcher::loadTemplates |
16
|
|
|
* |
17
|
|
|
* |
18
|
|
|
* - `data`: an optional array of variables which will be mapped onto the template. |
19
|
|
|
* Each of these variables is parsed during compile time. |
20
|
|
|
* @see \Brendt\Stitcher\Parser\AbstractParser |
21
|
|
|
* |
22
|
|
|
* - `adapters`: an optional array of Adapters for this page. Adapters are used to adapt a page's configuration |
23
|
|
|
* to another one. |
24
|
|
|
* @see \Brendt\Stitcher\Adapter\Adapter |
25
|
|
|
* |
26
|
|
|
* @package Brendt\Stitcher\Site |
27
|
|
|
*/ |
28
|
|
|
class Page |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Meta |
32
|
|
|
*/ |
33
|
|
|
public $meta; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The page's ID. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $id; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The template path of this page. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $templatePath; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The variables of this page, which will be available in the rendered template. |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $variables = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The adapters of this page. |
58
|
|
|
* Adapters will transform a page's variables and/or the page itself into one or more pages. |
59
|
|
|
* |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $adapters; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* An array containing a list of parsed variables. |
66
|
|
|
* |
67
|
|
|
* @see setVariableIsParsed |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $parsedVariables = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var Header[] |
75
|
|
|
*/ |
76
|
|
|
private $headers = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Construct a new page |
80
|
|
|
* |
81
|
|
|
* @param string $id |
82
|
|
|
* @param array $data |
83
|
|
|
* |
84
|
|
|
* @param Meta|null $meta |
85
|
|
|
* |
86
|
|
|
* @throws TemplateNotFoundException |
87
|
|
|
*/ |
88
|
|
|
public function __construct($id, array $data = [], Meta $meta = null) { |
89
|
|
|
if (!isset($data['template'])) { |
90
|
|
|
throw new TemplateNotFoundException("No template was set for page {$id}"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->id = $id; |
94
|
|
|
$this->templatePath = $data['template']; |
95
|
|
|
$this->meta = $meta ?? new Meta(); |
96
|
|
|
|
97
|
|
|
if (isset($data['variables'])) { |
98
|
|
|
$this->variables += $data['variables']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (isset($data['adapters'])) { |
102
|
|
|
foreach ($data['adapters'] as $type => $adapterConfig) { |
103
|
|
|
$this->adapters[$type] = $adapterConfig; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Defines a variable as parsed. |
110
|
|
|
* Parsed variables will be ignored by Stitcher when compiling the website. |
111
|
|
|
* Adapters can define parsed variables to indicate Stitcher it should skip parsing that variable during compile |
112
|
|
|
* time. |
113
|
|
|
* |
114
|
|
|
* @param $name |
115
|
|
|
* |
116
|
|
|
* @return Page |
117
|
|
|
* |
118
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
119
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
120
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
121
|
|
|
*/ |
122
|
|
|
public function setVariableIsParsed($name) { |
123
|
|
|
$this->parsedVariables[$name] = true; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Check whether a variable is parsed or not. |
130
|
|
|
* Parsed variables will be ignored by Stitcher during compile time. |
131
|
|
|
* |
132
|
|
|
* @param $name |
133
|
|
|
* |
134
|
|
|
* @return bool |
135
|
|
|
* |
136
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
137
|
|
|
*/ |
138
|
|
|
public function isParsedVariable($name) { |
139
|
|
|
return isset($this->parsedVariables[$name]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the ID of this page |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
* |
147
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
148
|
|
|
*/ |
149
|
|
|
public function getId() { |
150
|
|
|
return $this->id; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the template path of this page. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
* |
158
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
159
|
|
|
*/ |
160
|
|
|
public function getTemplatePath() { |
161
|
|
|
return $this->templatePath; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get the variables of this page. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
* |
169
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
170
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
171
|
|
|
*/ |
172
|
|
|
public function getVariables() { |
173
|
|
|
return $this->variables; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the adapters of this page. |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
* |
181
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseAdapters |
182
|
|
|
*/ |
183
|
|
|
public function getAdapters() { |
184
|
|
|
return $this->adapters; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get an adapter configuration by name. |
189
|
|
|
* |
190
|
|
|
* @param $name |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
* |
194
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
195
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
196
|
|
|
* @see \Brendt\Stitcher\controller\DevController::run |
197
|
|
|
*/ |
198
|
|
|
public function getAdapterConfig($name) { |
199
|
|
|
if (!isset($this->adapters[$name])) { |
200
|
|
|
return []; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->adapters[$name]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get a variable by name. |
208
|
|
|
* |
209
|
|
|
* @param $name |
210
|
|
|
* |
211
|
|
|
* @return mixed|null |
212
|
|
|
* |
213
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
214
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
215
|
|
|
*/ |
216
|
|
|
public function getVariable($name) { |
217
|
|
|
if (!isset($this->variables[$name])) { |
218
|
|
|
return null; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->variables[$name]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the value of a variable. |
226
|
|
|
* |
227
|
|
|
* @param $name |
228
|
|
|
* @param $value |
229
|
|
|
* |
230
|
|
|
* @return Page |
231
|
|
|
* |
232
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
233
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
234
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
235
|
|
|
*/ |
236
|
|
|
public function setVariableValue($name, $value) { |
237
|
|
|
$this->variables[$name] = $value; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Remove an adapter. |
244
|
|
|
* |
245
|
|
|
* @param $name |
246
|
|
|
* |
247
|
|
|
* @return Page |
248
|
|
|
* |
249
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
250
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
251
|
|
|
*/ |
252
|
|
|
public function removeAdapter($name) { |
253
|
|
|
if (isset($this->adapters[$name])) { |
254
|
|
|
unset($this->adapters[$name]); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Set the ID of this page. |
262
|
|
|
* An page's ID can be re-set after constructing when an adapter is creating other pages based on an existing page. |
263
|
|
|
* |
264
|
|
|
* @param string $id |
265
|
|
|
* |
266
|
|
|
* @return Page |
267
|
|
|
* |
268
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
269
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
270
|
|
|
*/ |
271
|
|
|
public function setId($id) { |
272
|
|
|
$this->id = $id; |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param Header $header |
279
|
|
|
*/ |
280
|
|
|
public function addHeader(Header $header) { |
281
|
|
|
$this->headers[] = $header; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @return Header[] |
286
|
|
|
*/ |
287
|
|
|
public function getHeaders() : array { |
288
|
|
|
return $this->headers; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
} |
292
|
|
|
|