1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The page model |
4
|
|
|
*/ |
5
|
|
|
namespace Phile\Model; |
6
|
|
|
|
7
|
|
|
use Phile\Core\Container; |
8
|
|
|
use Phile\Core\Router; |
9
|
|
|
use Phile\Core\ServiceLocator; |
10
|
|
|
use Phile\Repository\Page as Repository; |
11
|
|
|
use Phile\ServiceLocator\MetaInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* the Model class for a page |
15
|
|
|
* |
16
|
|
|
* @author Frank Nägler |
17
|
|
|
* @link https://philecms.github.io |
18
|
|
|
* @license http://opensource.org/licenses/MIT |
19
|
|
|
* @package Phile\Model |
20
|
|
|
*/ |
21
|
|
|
class Page |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \Phile\Model\Meta the meta model |
25
|
|
|
*/ |
26
|
|
|
protected $meta; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string the content |
30
|
|
|
*/ |
31
|
|
|
protected $content; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string the path to the original file |
35
|
|
|
*/ |
36
|
|
|
protected $filePath; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var null|string the raw file |
40
|
|
|
*/ |
41
|
|
|
protected $rawData; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \Phile\ServiceLocator\ParserInterface the parser |
45
|
|
|
*/ |
46
|
|
|
protected $parser; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string the pageId of the page |
50
|
|
|
*/ |
51
|
|
|
protected $pageId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string The content folder, as passed to the class constructor when initiating the object. |
55
|
|
|
*/ |
56
|
|
|
protected $contentFolder; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string content extension |
60
|
|
|
*/ |
61
|
|
|
protected $contentExtension; |
62
|
|
|
|
63
|
|
|
/** @var Repository */ |
64
|
|
|
protected $repository; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* the constructor |
68
|
|
|
* |
69
|
|
|
* @param string $filePath |
70
|
|
|
* @param string $folder |
71
|
|
|
*/ |
72
|
22 |
|
public function __construct($filePath, $folder = null) |
73
|
|
|
{ |
74
|
22 |
|
$settings = Container::getInstance()->get('Phile_Config')->toArray(); |
75
|
22 |
|
$this->contentFolder = $folder ?: $settings['content_dir']; |
76
|
22 |
|
$this->contentExtension = $settings['content_ext']; |
77
|
22 |
|
$this->setFilePath($filePath); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @triggerEvent before_load_content this event is triggered before the content is loaded |
81
|
|
|
* |
82
|
|
|
* @param string filePath the path to the file |
83
|
|
|
* @param \Phile\Model\Page page the page model |
84
|
|
|
*/ |
85
|
22 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
86
|
|
|
'before_load_content', |
87
|
22 |
|
['filePath' => &$this->filePath, 'page' => &$this] |
88
|
|
|
); |
89
|
22 |
|
if (file_exists($this->filePath)) { |
90
|
22 |
|
$this->rawData = file_get_contents($this->filePath) ?: null; |
91
|
22 |
|
$this->parseRawData(); |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* @triggerEvent after_load_content this event is triggered after the content is loaded |
95
|
|
|
* |
96
|
|
|
* @param string filePath the path to the file |
97
|
|
|
* @param string rawData the raw data |
98
|
|
|
* @param \Phile\Model\Page page the page model |
99
|
|
|
*/ |
100
|
22 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
101
|
|
|
'after_load_content', |
102
|
|
|
[ |
103
|
22 |
|
'filePath' => &$this->filePath, |
104
|
22 |
|
'rawData' => $this->rawData, |
105
|
|
|
'page' => &$this |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
22 |
|
$this->parser = ServiceLocator::getService('Phile_Parser'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* method to get content of page, this method returned the parsed content |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
7 |
|
public function getContent() |
118
|
|
|
{ |
119
|
|
|
/** |
120
|
|
|
* @triggerEvent before_parse_content this event is triggered before the content is parsed |
121
|
|
|
* |
122
|
|
|
* @param string content the raw data |
123
|
|
|
* @param \Phile\Model\Page page the page model |
124
|
|
|
*/ |
125
|
7 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
126
|
|
|
'before_parse_content', |
127
|
7 |
|
['content' => $this->content, 'page' => &$this] |
128
|
|
|
); |
129
|
7 |
|
$content = $this->parser->parse($this->content); |
130
|
|
|
/** |
131
|
|
|
* @triggerEvent after_parse_content this event is triggered after the content is parsed |
132
|
|
|
* |
133
|
|
|
* @param string content the parsed content |
134
|
|
|
* @param \Phile\Model\Page page the page model |
135
|
|
|
*/ |
136
|
7 |
|
Container::getInstance()->get('Phile_EventBus')->trigger( |
137
|
|
|
'after_parse_content', |
138
|
7 |
|
['content' => &$content, 'page' => &$this] |
139
|
|
|
); |
140
|
|
|
|
141
|
7 |
|
return $content; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* set content of page |
146
|
|
|
* |
147
|
|
|
* @param string $content |
148
|
|
|
*/ |
149
|
3 |
|
public function setContent($content) |
150
|
|
|
{ |
151
|
3 |
|
$this->content = $content; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* get raw (un-parsed) page content |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
2 |
|
public function getRawContent() |
160
|
|
|
{ |
161
|
2 |
|
return $this->content; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* get the meta model |
166
|
|
|
* |
167
|
|
|
* @return Meta |
168
|
|
|
*/ |
169
|
12 |
|
public function getMeta() |
170
|
|
|
{ |
171
|
12 |
|
return $this->meta; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* parse the raw content |
176
|
|
|
*/ |
177
|
22 |
|
protected function parseRawData(): void |
178
|
|
|
{ |
179
|
22 |
|
$this->meta = new Meta($this->rawData); |
180
|
|
|
|
181
|
22 |
|
$content = ''; |
182
|
22 |
|
if ($this->rawData !== null) { |
183
|
|
|
/** @var MetaInterface */ |
184
|
21 |
|
$metaParser = ServiceLocator::getService('Phile_Parser_Meta'); |
185
|
21 |
|
$content = $metaParser->extractContent($this->rawData); |
186
|
|
|
} |
187
|
|
|
|
188
|
22 |
|
$this->content = $content; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Sets repository this page was retrieved by/belongs to |
193
|
|
|
* |
194
|
|
|
* @param Repository $repository |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
22 |
|
public function setRepository(Repository $repository) |
198
|
|
|
{ |
199
|
22 |
|
$this->repository = $repository; |
200
|
22 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets repository this page belongs to |
205
|
|
|
* |
206
|
|
|
* @return Repository |
207
|
|
|
*/ |
208
|
9 |
|
public function getRepository() |
209
|
|
|
{ |
210
|
9 |
|
if ($this->repository === null) { |
211
|
1 |
|
$this->repository = new Repository(); |
212
|
|
|
} |
213
|
|
|
|
214
|
9 |
|
return $this->repository; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* get the title of page from meta information |
219
|
|
|
* |
220
|
|
|
* @return string|null |
221
|
|
|
*/ |
222
|
9 |
|
public function getTitle() |
223
|
|
|
{ |
224
|
9 |
|
return $this->getMeta()->get('title'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* get Phile $pageId |
229
|
|
|
* |
230
|
|
|
* @param string $filePath |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
22 |
|
protected function buildPageId($filePath) |
234
|
|
|
{ |
235
|
22 |
|
$pageId = str_replace($this->contentFolder, '', $filePath); |
236
|
22 |
|
$pageId = str_replace($this->contentExtension, '', $pageId); |
237
|
22 |
|
$pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId); |
238
|
22 |
|
$pageId = ltrim($pageId, '/'); |
239
|
22 |
|
$pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId); |
240
|
22 |
|
return $pageId; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* get the url of page |
245
|
|
|
* |
246
|
|
|
* @return string |
247
|
|
|
*/ |
248
|
6 |
|
public function getUrl() |
249
|
|
|
{ |
250
|
6 |
|
$container = Container::getInstance(); |
251
|
6 |
|
if ($container->has('Phile_Router')) { |
252
|
5 |
|
$router = $container->get('Phile_Router'); |
253
|
|
|
} else { |
254
|
|
|
// BC: some old 1.x plugins may use Pages before the core is initialized |
255
|
1 |
|
$router = new Router; |
256
|
|
|
} |
257
|
6 |
|
return $router->urlForPage($this->pageId, false); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* set the filepath of the page |
262
|
|
|
* |
263
|
|
|
* @param string $filePath |
264
|
|
|
*/ |
265
|
22 |
|
public function setFilePath($filePath) |
266
|
|
|
{ |
267
|
22 |
|
$this->filePath = $filePath; |
268
|
22 |
|
$this->pageId = $this->buildPageId($this->filePath); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* get the filepath of the page |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
3 |
|
public function getFilePath() |
277
|
|
|
{ |
278
|
3 |
|
return $this->filePath; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* get the folder name |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function getFolder() |
287
|
|
|
{ |
288
|
|
|
return basename(dirname($this->getFilePath())); |
289
|
|
|
} |
290
|
|
|
|
291
|
7 |
|
public function getPageId() |
292
|
|
|
{ |
293
|
7 |
|
return $this->pageId; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* get the previous page if one exist |
298
|
|
|
* |
299
|
|
|
* @return null|\Phile\Model\Page |
300
|
|
|
*/ |
301
|
1 |
|
public function getPreviousPage() |
302
|
|
|
{ |
303
|
1 |
|
return $this->getRepository()->getPageOffset($this, -1); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* get the next page if one exist |
308
|
|
|
* |
309
|
|
|
* @return null|\Phile\Model\Page |
310
|
|
|
*/ |
311
|
1 |
|
public function getNextPage() |
312
|
|
|
{ |
313
|
1 |
|
return $this->getRepository()->getPageOffset($this, 1); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|