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\Adapter\Adapter; |
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
|
|
|
* Construct a new page |
75
|
|
|
* |
76
|
|
|
* @param string $id |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @param Meta|null $meta |
80
|
|
|
* |
81
|
|
|
* @throws TemplateNotFoundException |
82
|
|
|
*/ |
83
|
|
|
public function __construct($id, array $data = [], Meta $meta = null) { |
84
|
|
|
if (!isset($data['template'])) { |
85
|
|
|
throw new TemplateNotFoundException("No template was set for page {$id}"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->id = $id; |
89
|
|
|
$this->templatePath = $data['template']; |
90
|
|
|
$this->meta = $meta ?? new Meta(); |
91
|
|
|
|
92
|
|
|
if (isset($data['variables'])) { |
93
|
|
|
$this->variables += $data['variables']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (isset($data['adapters'])) { |
97
|
|
|
foreach ($data['adapters'] as $type => $adapterConfig) { |
98
|
|
|
$this->adapters[$type] = $adapterConfig; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Defines a variable as parsed. |
105
|
|
|
* Parsed variables will be ignored by Stitcher when compiling the website. |
106
|
|
|
* Adapters can define parsed variables to indicate Stitcher it should skip parsing that variable during compile |
107
|
|
|
* time. |
108
|
|
|
* |
109
|
|
|
* @param $name |
110
|
|
|
* |
111
|
|
|
* @return Page |
112
|
|
|
* |
113
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
114
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
115
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
116
|
|
|
*/ |
117
|
|
|
public function setVariableIsParsed($name) { |
118
|
|
|
$this->parsedVariables[$name] = true; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check whether a variable is parsed or not. |
125
|
|
|
* Parsed variables will be ignored by Stitcher during compile time. |
126
|
|
|
* |
127
|
|
|
* @param $name |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
* |
131
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
132
|
|
|
*/ |
133
|
|
|
public function isParsedVariable($name) { |
134
|
|
|
return isset($this->parsedVariables[$name]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the ID of this page |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
* |
142
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
143
|
|
|
*/ |
144
|
|
|
public function getId() { |
145
|
|
|
return $this->id; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the template path of this page. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
* |
153
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
154
|
|
|
*/ |
155
|
|
|
public function getTemplatePath() { |
156
|
|
|
return $this->templatePath; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the variables of this page. |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
* |
164
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch |
165
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
166
|
|
|
*/ |
167
|
|
|
public function getVariables() { |
168
|
|
|
return $this->variables; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the adapters of this page. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
* |
176
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseAdapters |
177
|
|
|
*/ |
178
|
|
|
public function getAdapters() { |
179
|
|
|
return $this->adapters; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get an adapter configuration by name. |
184
|
|
|
* |
185
|
|
|
* @param $name |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
* |
189
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
190
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
191
|
|
|
* @see \Brendt\Stitcher\controller\DevController::run |
192
|
|
|
*/ |
193
|
|
|
public function getAdapterConfig($name) { |
194
|
|
|
if (!isset($this->adapters[$name])) { |
195
|
|
|
return []; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->adapters[$name]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get a variable by name. |
203
|
|
|
* |
204
|
|
|
* @param $name |
205
|
|
|
* |
206
|
|
|
* @return mixed|null |
207
|
|
|
* |
208
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
209
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
210
|
|
|
*/ |
211
|
|
|
public function getVariable($name) { |
212
|
|
|
if (!isset($this->variables[$name])) { |
213
|
|
|
return null; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->variables[$name]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set the value of a variable. |
221
|
|
|
* |
222
|
|
|
* @param $name |
223
|
|
|
* @param $value |
224
|
|
|
* |
225
|
|
|
* @return Page |
226
|
|
|
* |
227
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
228
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
229
|
|
|
* @see \Brendt\Stitcher\Stitcher::parseVariables |
230
|
|
|
*/ |
231
|
|
|
public function setVariableValue($name, $value) { |
232
|
|
|
$this->variables[$name] = $value; |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Remove an adapter. |
239
|
|
|
* |
240
|
|
|
* @param $name |
241
|
|
|
* |
242
|
|
|
* @return Page |
243
|
|
|
* |
244
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
245
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
246
|
|
|
*/ |
247
|
|
|
public function removeAdapter($name) { |
248
|
|
|
if (isset($this->adapters[$name])) { |
249
|
|
|
unset($this->adapters[$name]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set the ID of this page. |
257
|
|
|
* An page's ID can be re-set after constructing when an adapter is creating other pages based on an existing page. |
258
|
|
|
* |
259
|
|
|
* @param string $id |
260
|
|
|
* |
261
|
|
|
* @return Page |
262
|
|
|
* |
263
|
|
|
* @see \Brendt\Stitcher\adapter\CollectionAdapter::transform |
264
|
|
|
* @see \Brendt\Stitcher\adapter\PagincationAdapter::transform |
265
|
|
|
*/ |
266
|
|
|
public function setId($id) { |
267
|
|
|
$this->id = $id; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array $data |
274
|
|
|
*/ |
275
|
|
|
public function parseMeta(array $data) { |
276
|
|
|
if (isset($data['meta'])) { |
277
|
|
|
foreach ($data['meta'] as $name => $value) { |
278
|
|
|
$value = $data[$value] ?? $value; |
279
|
|
|
|
280
|
|
|
$this->meta->name($name, $value); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if (isset($data['title'])) { |
285
|
|
|
$this->meta->title($data['title']); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
if (isset($data['image'])) { |
289
|
|
|
$this->meta->image($data['image']['src']); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if (isset($data['description'])) { |
293
|
|
|
$this->meta->description($data['description']); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
View Code Duplication |
if (isset($data['pagination']['next'])) { |
|
|
|
|
297
|
|
|
$this->meta->link('next', $data['pagination']['next']['url']); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
View Code Duplication |
if (isset($data['pagination']['prev'])) { |
|
|
|
|
301
|
|
|
$this->meta->link('prev', $data['pagination']['prev']['url']); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
} |
306
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.