|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Cache\Item; |
|
19
|
|
|
use O2System\Framework\Http\Presenter\Meta; |
|
20
|
|
|
use O2System\Framework\Http\Router\Datastructures\Page; |
|
21
|
|
|
use O2System\Gear\Toolbar; |
|
22
|
|
|
use O2System\Html; |
|
23
|
|
|
use O2System\Psr\Cache\CacheItemPoolInterface; |
|
24
|
|
|
use O2System\Psr\Patterns\Structural\Composite\RenderableInterface; |
|
25
|
|
|
use O2System\Spl\Exceptions\ErrorException; |
|
26
|
|
|
use O2System\Spl\Traits\Collectors\FileExtensionCollectorTrait; |
|
27
|
|
|
use O2System\Spl\Traits\Collectors\FilePathCollectorTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class View |
|
31
|
|
|
* |
|
32
|
|
|
* @package O2System |
|
33
|
|
|
*/ |
|
34
|
|
|
class View implements RenderableInterface |
|
35
|
|
|
{ |
|
36
|
|
|
use FilePathCollectorTrait; |
|
37
|
|
|
use FileExtensionCollectorTrait; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* View Config |
|
41
|
|
|
* |
|
42
|
|
|
* @var \O2System\Kernel\Datastructures\Config |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $config; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* View HTML Document |
|
48
|
|
|
* |
|
49
|
|
|
* @var Html\Document |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $document; |
|
52
|
|
|
|
|
53
|
|
|
// ------------------------------------------------------------------------ |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* View::__construct |
|
57
|
|
|
* |
|
58
|
|
|
* @return View |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->setFileDirName('Views'); |
|
63
|
|
|
$this->addFilePath(PATH_APP); |
|
64
|
|
|
|
|
65
|
|
|
output()->addFilePath(PATH_APP); |
|
66
|
|
|
|
|
67
|
|
|
$this->config = config()->loadFile('view', true); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$this->setFileExtensions( |
|
70
|
|
|
[ |
|
71
|
|
|
'.php', |
|
72
|
|
|
'.phtml', |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
if ($this->config->offsetExists('extensions')) { |
|
77
|
|
|
$this->setFileExtensions($this->config[ 'extensions' ]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->document = new Html\Document(); |
|
81
|
|
|
$this->document->formatOutput = (bool)$this->config->beautify; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* __get |
|
86
|
|
|
* |
|
87
|
|
|
* @param $property |
|
88
|
|
|
* |
|
89
|
|
|
* @return Parser|bool Returns FALSE when property is not set. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function &__get($property) |
|
92
|
|
|
{ |
|
93
|
|
|
$get[ $property ] = false; |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
if (property_exists($this, $property)) { |
|
96
|
|
|
return $this->{$property}; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $get[ $property ]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function parse($string, array $vars = []) |
|
103
|
|
|
{ |
|
104
|
|
|
parser()->loadString($string); |
|
105
|
|
|
|
|
106
|
|
|
return parser()->parse($vars); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function with($vars, $value = null) |
|
110
|
|
|
{ |
|
111
|
|
|
if (is_string($vars)) { |
|
112
|
|
|
$vars = [$vars => $value]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
presenter()->merge($vars); |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function load($filename, array $vars = [], $return = false) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($filename instanceof Page) { |
|
123
|
|
|
return $this->page($filename->getRealPath(), array_merge($vars, $filename->getVars())); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (strpos($filename, 'Pages') !== false) { |
|
127
|
|
|
return $this->page($filename, $vars, $return); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
presenter()->merge($vars); |
|
131
|
|
|
|
|
132
|
|
|
if (false !== ($filePath = $this->getFilePath($filename))) { |
|
133
|
|
|
if ($return === false) { |
|
134
|
|
|
|
|
135
|
|
|
$partials = presenter()->get('partials'); |
|
136
|
|
|
|
|
137
|
|
|
if ($partials->hasPartial('content') === false) { |
|
138
|
|
|
$partials->addPartial('content', $filePath); |
|
139
|
|
|
} else { |
|
140
|
|
|
$partials->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $filePath); |
|
141
|
|
|
} |
|
142
|
|
|
} else { |
|
143
|
|
|
parser()->loadFile($filePath); |
|
144
|
|
|
|
|
145
|
|
|
return parser()->parse(presenter()->getArrayCopy()); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
$vars = presenter()->getArrayCopy(); |
|
149
|
|
|
extract($vars); |
|
150
|
|
|
|
|
151
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
152
|
|
|
|
|
153
|
|
|
$error = new ErrorException( |
|
154
|
|
|
'E_VIEW_NOT_FOUND', |
|
155
|
|
|
0, |
|
156
|
|
|
@$backtrace[ 0 ][ 'file' ], |
|
157
|
|
|
@$backtrace[ 0 ][ 'line' ], |
|
158
|
|
|
[trim($filename)] |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
unset($backtrace); |
|
162
|
|
|
|
|
163
|
|
|
ob_start(); |
|
164
|
|
|
include output()->getFilePath('error'); |
|
|
|
|
|
|
165
|
|
|
$content = ob_get_contents(); |
|
166
|
|
|
ob_end_clean(); |
|
167
|
|
|
|
|
168
|
|
|
if ($return === false) { |
|
169
|
|
|
$partials = presenter()->get('partials'); |
|
170
|
|
|
|
|
171
|
|
|
if ($partials->hasPartial('content') === false) { |
|
172
|
|
|
$partials->addPartial('content', $content); |
|
173
|
|
|
} else { |
|
174
|
|
|
$partials->addPartial(pathinfo($filePath, PATHINFO_FILENAME), $content); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
} else { |
|
178
|
|
|
return $content; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function page($filename, array $vars = [], $return = false) |
|
184
|
|
|
{ |
|
185
|
|
|
if ($filename instanceof Page) { |
|
186
|
|
|
return $this->page($filename->getRealPath(), array_merge($vars, $filename->getVars())); |
|
|
|
|
|
|
187
|
|
|
} else { |
|
188
|
|
|
$pageDirectories = modules()->getDirs('Pages'); |
|
|
|
|
|
|
189
|
|
|
foreach ($pageDirectories as $pageDirectory) { |
|
190
|
|
|
if (is_file($pageFilePath = $pageDirectory . $filename . '.phtml')) { |
|
191
|
|
|
$filename = $pageFilePath; |
|
192
|
|
|
break; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
presenter()->merge($vars); |
|
198
|
|
|
|
|
199
|
|
|
if ($return === false) { |
|
200
|
|
|
$partials = presenter()->get('partials'); |
|
201
|
|
|
|
|
202
|
|
|
if ($partials->hasPartial('content') === false) { |
|
203
|
|
|
$partials->addPartial('content', $filename); |
|
204
|
|
|
} else { |
|
205
|
|
|
$partials->addPartial(pathinfo($filename, PATHINFO_FILENAME), $filename); |
|
206
|
|
|
} |
|
207
|
|
|
} elseif (parser()->loadFile($filename)) { |
|
208
|
|
|
return parser()->parse(presenter()->getArrayCopy()); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function modal($filename, array $vars = []) |
|
213
|
|
|
{ |
|
214
|
|
|
if (presenter()->theme->hasLayout('modal')) { |
|
|
|
|
|
|
215
|
|
|
if (presenter()->theme->hasLayout('modal')) { |
|
216
|
|
|
presenter()->theme->setLayout('modal'); |
|
217
|
|
|
echo $this->load($filename, $vars, true); |
|
218
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
presenter()->merge($vars); |
|
223
|
|
|
|
|
224
|
|
|
if (parser()->loadFile($filename)) { |
|
225
|
|
|
output()->send(parser()->parse(presenter()->getArrayCopy())); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
public function getFilePath($filename) |
|
230
|
|
|
{ |
|
231
|
|
|
$filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
|
232
|
|
|
|
|
233
|
|
|
if (is_file($filename)) { |
|
234
|
|
|
return realpath($filename); |
|
235
|
|
|
} else { |
|
236
|
|
|
$viewsFileExtensions = $this->fileExtensions; |
|
237
|
|
|
$viewsDirectories = modules()->getDirs('Views'); |
|
238
|
|
|
$viewsDirectories = array_merge($viewsDirectories, $this->filePaths); |
|
239
|
|
|
$viewsDirectories = array_unique($viewsDirectories); |
|
240
|
|
|
|
|
241
|
|
|
$deviceDirectory = null; |
|
|
|
|
|
|
242
|
|
|
if (services('userAgent')->isMobile()) { |
|
|
|
|
|
|
243
|
|
|
$deviceDirectory = 'mobile'; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
if (presenter()->theme->use === true) { |
|
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
$moduleReplacementPath = presenter()->theme->active->getPathName() |
|
249
|
|
|
. DIRECTORY_SEPARATOR |
|
250
|
|
|
. 'views' |
|
251
|
|
|
. DIRECTORY_SEPARATOR |
|
252
|
|
|
. strtolower( |
|
253
|
|
|
str_replace(PATH_APP, '', modules()->current()->getRealpath()) |
|
|
|
|
|
|
254
|
|
|
); |
|
255
|
|
|
|
|
256
|
|
|
if (is_dir($moduleReplacementPath)) { |
|
257
|
|
|
array_unshift($viewsDirectories, $moduleReplacementPath); |
|
258
|
|
|
|
|
259
|
|
|
// Add Theme File Extensions |
|
260
|
|
|
if (presenter()->theme->active->getPresets()->offsetExists('extension')) { |
|
261
|
|
|
array_unshift($viewsFileExtensions, |
|
262
|
|
|
presenter()->theme->active->getPresets()->offsetGet('extension')); |
|
263
|
|
|
} elseif (presenter()->theme->active->getPresets()->offsetExists('extensions')) { |
|
264
|
|
|
$viewsFileExtensions = array_merge( |
|
265
|
|
|
presenter()->theme->active->getPresets()->offsetGet('extensions'), |
|
266
|
|
|
$viewsFileExtensions |
|
267
|
|
|
); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
// Add Theme Parser Engine |
|
271
|
|
|
if (presenter()->theme->active->getPresets()->offsetExists('driver')) { |
|
272
|
|
|
$parserDriverClassName = '\O2System\Parser\Drivers\\' . camelcase( |
|
273
|
|
|
presenter()->theme->active->getPresets()->offsetGet('driver') |
|
274
|
|
|
); |
|
275
|
|
|
|
|
276
|
|
|
if (class_exists($parserDriverClassName)) { |
|
277
|
|
|
parser()->addDriver( |
|
278
|
|
|
new $parserDriverClassName(), |
|
279
|
|
|
presenter()->theme->active->getPresets()->offsetGet('driver') |
|
280
|
|
|
); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
foreach ($viewsDirectories as $viewsDirectory) { |
|
287
|
|
|
foreach ($viewsFileExtensions as $fileExtension) { |
|
288
|
|
|
$filename = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $filename); |
|
289
|
|
|
|
|
290
|
|
|
if (is_file($filePath = $viewsDirectory . $filename . $fileExtension)) { |
|
291
|
|
|
return realpath($filePath); |
|
292
|
|
|
break; |
|
|
|
|
|
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
return false; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function render(array $options = []) |
|
302
|
|
|
{ |
|
303
|
|
|
if(profiler() !== false) { |
|
|
|
|
|
|
304
|
|
|
profiler()->watch('Starting View Rendering'); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$htmlOutput = ''; |
|
308
|
|
|
parser()->loadVars(presenter()->getArrayCopy()); |
|
309
|
|
|
|
|
310
|
|
|
// set document meta title |
|
311
|
|
|
if (presenter()->meta->title instanceof Meta\Title) { |
|
|
|
|
|
|
312
|
|
|
$this->document->title->text(presenter()->meta->title->__toString()); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Injecting Meta Opengraph |
|
317
|
|
|
*/ |
|
318
|
|
|
if (presenter()->meta->opengraph instanceof Meta\Opengraph) { |
|
319
|
|
|
// set opengraph title |
|
320
|
|
|
if (presenter()->meta->title instanceof Meta\Title) { |
|
321
|
|
|
presenter()->meta->opengraph->setTitle(presenter()->meta->title->__toString()); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
// set opengraph site name |
|
325
|
|
|
if (presenter()->exists('siteName')) { |
|
326
|
|
|
presenter()->meta->opengraph->setSiteName(presenter()->offsetGet('siteName')); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
if (presenter()->meta->opengraph->count()) { |
|
330
|
|
|
$htmlElement = $this->document->getElementsByTagName('html')->item(0); |
|
331
|
|
|
$htmlElement->setAttribute('prefix', 'og: ' . presenter()->meta->opengraph->prefix); |
|
332
|
|
|
|
|
333
|
|
|
if (presenter()->meta->opengraph->exists('og:type') === false) { |
|
334
|
|
|
presenter()->meta->opengraph->setType('website'); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$opengraph = presenter()->meta->opengraph->getArrayCopy(); |
|
338
|
|
|
|
|
339
|
|
|
foreach ($opengraph as $tag) { |
|
340
|
|
|
$this->document->metaNodes->createElement($tag->attributes->getArrayCopy()); |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
if (false !== ($controller = controller())) { |
|
346
|
|
|
presenter()->meta->offsetSet('module-controller', $controller->getClassInfo()->getParameter()); |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
$meta = presenter()->meta->getArrayCopy(); |
|
350
|
|
|
|
|
351
|
|
|
foreach ($meta as $tag) { |
|
352
|
|
|
$this->document->metaNodes->createElement($tag->attributes->getArrayCopy()); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
if (presenter()->theme->use === true) { |
|
|
|
|
|
|
356
|
|
|
presenter()->theme->load(); |
|
357
|
|
|
if (false !== ($layout = presenter()->theme->active->getLayout())) { |
|
358
|
|
|
parser()->loadFile($layout->getRealPath()); |
|
359
|
|
|
$htmlOutput = parser()->parse(); |
|
360
|
|
|
} |
|
361
|
|
|
} else { |
|
362
|
|
|
$this->document->find('body')->append(presenter()->partials->__get('content')); |
|
|
|
|
|
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
$this->document->loadHTML(presenter()->assets->parseSourceCode($htmlOutput)); |
|
|
|
|
|
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Injecting Single Sign-On (SSO) iFrame |
|
369
|
|
|
*/ |
|
370
|
|
|
if (services()->has('user')) { |
|
371
|
|
|
$iframe = services()->get('user')->getIframeCode(); |
|
372
|
|
|
|
|
373
|
|
|
if ( ! empty($iframe)) { |
|
374
|
|
|
$this->document->find('body')->append($iframe); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
if (input()->env('DEBUG_STAGE') === 'DEVELOPER' and |
|
379
|
|
|
config()->getItem('presenter')->debugToolBar === true and |
|
|
|
|
|
|
380
|
|
|
services()->has('profiler') |
|
381
|
|
|
) { |
|
382
|
|
|
$this->document->find('body')->append((new Toolbar())->__toString()); |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Injecting Progressive Web Application (PWA) Manifest |
|
387
|
|
|
*/ |
|
388
|
|
|
$this->document->linkNodes->createElement([ |
|
389
|
|
|
'rel' => 'manifest', |
|
390
|
|
|
'href' => '/manifest.json', |
|
391
|
|
|
]); |
|
392
|
|
|
|
|
393
|
|
|
$htmlOutput = $this->document->saveHTML(); |
|
394
|
|
|
|
|
395
|
|
|
// Uglify Output |
|
396
|
|
|
if ($this->config->output['uglify'] === true) { |
|
397
|
|
|
$htmlOutput = preg_replace( |
|
398
|
|
|
[ |
|
399
|
|
|
'/\>[^\S ]+/s', // strip whitespaces after tags, except space |
|
400
|
|
|
'/[^\S ]+\</s', // strip whitespaces before tags, except space |
|
401
|
|
|
'/(\s)+/s', // shorten multiple whitespace sequences |
|
402
|
|
|
'/<!--(.|\s)*?-->/', // Remove HTML comments |
|
403
|
|
|
'/<!--(.*)-->/Uis', |
|
404
|
|
|
"/[[:blank:]]+/", |
|
405
|
|
|
], |
|
406
|
|
|
[ |
|
407
|
|
|
'>', |
|
408
|
|
|
'<', |
|
409
|
|
|
'\\1', |
|
410
|
|
|
'', |
|
411
|
|
|
'', |
|
412
|
|
|
' ', |
|
413
|
|
|
], |
|
414
|
|
|
str_replace(["\n", "\r", "\t"], '', $htmlOutput)); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
// Beautify Output |
|
418
|
|
|
if ($this->config->output['beautify'] === true) { |
|
419
|
|
|
$beautifier = new Html\Dom\Beautifier(); |
|
420
|
|
|
$htmlOutput = $beautifier->format($htmlOutput); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
if(profiler() !== false) { |
|
|
|
|
|
|
424
|
|
|
profiler()->watch('Ending View Rendering'); |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
return $htmlOutput; |
|
428
|
|
|
} |
|
429
|
|
|
} |
|
430
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.