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