|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elgg; |
|
4
|
|
|
|
|
5
|
|
|
use Elgg\Cache\ServerCache; |
|
6
|
|
|
use Elgg\Http\Request as HttpRequest; |
|
7
|
|
|
use Elgg\Project\Paths; |
|
8
|
|
|
use Elgg\Traits\Loggable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Views service |
|
12
|
|
|
* |
|
13
|
|
|
* @internal |
|
14
|
|
|
* @since 1.9.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class ViewsService { |
|
17
|
|
|
|
|
18
|
|
|
use Loggable; |
|
19
|
|
|
|
|
20
|
|
|
const VIEW_HOOK = 'view'; |
|
21
|
|
|
const VIEW_VARS_HOOK = 'view_vars'; |
|
22
|
|
|
const OUTPUT_KEY = '__view_output'; |
|
23
|
|
|
const BASE_VIEW_PRIORITY = 500; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @see ViewsService::fileExists() |
|
27
|
|
|
*/ |
|
28
|
|
|
protected array $file_exists_cache = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* [viewtype][view] => '/path/to/views/style.css' |
|
32
|
|
|
*/ |
|
33
|
|
|
protected array $locations = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Tracks location changes for views |
|
37
|
|
|
* |
|
38
|
|
|
* [viewtype][view][] => '/path/to/views/style.css' |
|
39
|
|
|
*/ |
|
40
|
|
|
protected array $overrides = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* [view][priority] = extension_view |
|
44
|
|
|
*/ |
|
45
|
|
|
protected array $extensions = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string[] A list of fallback viewtypes |
|
49
|
|
|
*/ |
|
50
|
|
|
protected array $fallbacks = []; |
|
51
|
|
|
|
|
52
|
|
|
protected ?string $viewtype; |
|
53
|
|
|
|
|
54
|
|
|
protected bool $locations_loaded_from_cache = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Constructor |
|
58
|
|
|
* |
|
59
|
|
|
* @param EventsService $events Events service |
|
60
|
|
|
* @param \Elgg\Http\Request $request Http Request |
|
61
|
|
|
* @param \Elgg\Config $config Elgg configuration |
|
62
|
|
|
* @param ServerCache $server_cache Server cache |
|
63
|
|
|
*/ |
|
64
|
9919 |
|
public function __construct( |
|
65
|
|
|
protected EventsService $events, |
|
66
|
|
|
protected HttpRequest $request, |
|
67
|
|
|
protected Config $config, |
|
68
|
|
|
protected ServerCache $server_cache |
|
69
|
|
|
) { |
|
70
|
9919 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set the viewtype |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $viewtype Viewtype |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
178 |
|
public function setViewtype(string $viewtype = ''): bool { |
|
80
|
178 |
|
if (!$viewtype) { |
|
81
|
142 |
|
$this->viewtype = null; |
|
82
|
|
|
|
|
83
|
142 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
38 |
|
if ($this->isValidViewtype($viewtype)) { |
|
87
|
38 |
|
$this->viewtype = $viewtype; |
|
88
|
|
|
|
|
89
|
38 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get the viewtype |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
2256 |
|
public function getViewtype(): string { |
|
101
|
2256 |
|
if (!isset($this->viewtype)) { |
|
102
|
2087 |
|
$this->viewtype = $this->resolveViewtype(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2256 |
|
return $this->viewtype; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Resolve the initial viewtype |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
2087 |
|
protected function resolveViewtype(): string { |
|
114
|
2087 |
|
if ($this->request) { |
|
115
|
2087 |
|
$view = $this->request->getParam('view', '', false); |
|
116
|
2087 |
|
if ($this->isValidViewtype($view) && !empty($this->locations[$view])) { |
|
117
|
10 |
|
return $view; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2085 |
|
$view = (string) $this->config->view; |
|
122
|
2085 |
|
if ($this->isValidViewtype($view) && !empty($this->locations[$view])) { |
|
123
|
2 |
|
return $view; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2085 |
|
return 'default'; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Checks if $viewtype is a string suitable for use as a viewtype name |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $viewtype Potential viewtype name. Alphanumeric chars plus _ allowed. |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
2138 |
|
public function isValidViewtype(string $viewtype): bool { |
|
137
|
2138 |
|
if ($viewtype === '') { |
|
138
|
2085 |
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1362 |
|
if (preg_match('/\W/', $viewtype)) { |
|
142
|
2 |
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1362 |
|
return true; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Discover the core views if the system cache did not load |
|
150
|
|
|
* |
|
151
|
|
|
* @return void |
|
152
|
|
|
* @since 6.1 |
|
153
|
|
|
*/ |
|
154
|
674 |
|
public function registerCoreViews(): void { |
|
155
|
674 |
|
if ($this->isViewLocationsLoadedFromCache()) { |
|
156
|
602 |
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Core view files in /views |
|
160
|
72 |
|
$this->registerViewsFromPath(Paths::elgg()); |
|
161
|
|
|
|
|
162
|
|
|
// Core view definitions in /engine/views.php |
|
163
|
72 |
|
$file = Paths::elgg() . 'engine/views.php'; |
|
164
|
72 |
|
if (!is_file($file)) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
72 |
|
$spec = Includer::includeFile($file); |
|
169
|
72 |
|
if (is_array($spec)) { |
|
170
|
|
|
// check for uploaded fontawesome font |
|
171
|
72 |
|
if ($this->config->font_awesome_zip) { |
|
172
|
|
|
$spec['default']['font-awesome/'] = elgg_get_data_path() . 'fontawesome/webfont/'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
72 |
|
$this->mergeViewsSpec($spec); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Auto-registers views from a location. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $view_base The base of the view name without the view type. |
|
183
|
|
|
* @param string $folder Required The folder to begin looking in |
|
184
|
|
|
* @param string $viewtype The type of view we're looking at (default, rss, etc) |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool returns false if folder can't be read |
|
187
|
|
|
*/ |
|
188
|
1533 |
|
public function autoregisterViews(string $view_base, string $folder, string $viewtype): bool { |
|
189
|
1533 |
|
$folder = Paths::sanitize($folder); |
|
190
|
1533 |
|
$view_base = Paths::sanitize($view_base, false); |
|
191
|
1533 |
|
$view_base = $view_base ? $view_base . '/' : $view_base; |
|
192
|
|
|
|
|
193
|
|
|
try { |
|
194
|
1533 |
|
$dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($folder, \RecursiveDirectoryIterator::SKIP_DOTS)); |
|
195
|
|
|
} catch (\Throwable $t) { |
|
196
|
|
|
$this->getLogger()->error($t->getMessage()); |
|
197
|
|
|
return false; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/* @var $file \SplFileInfo */ |
|
201
|
1533 |
|
foreach ($dir as $file) { |
|
202
|
1533 |
|
$path = $file->getPath() . '/' . $file->getBasename('.php'); |
|
203
|
1533 |
|
$path = Paths::sanitize($path, false); |
|
204
|
|
|
|
|
205
|
|
|
// found a file add it to the views |
|
206
|
1533 |
|
$view = $view_base . substr($path, strlen($folder)); |
|
207
|
1533 |
|
$this->setViewLocation($view, $viewtype, $file->getPathname()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
1533 |
|
return true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Find the view file |
|
215
|
|
|
* |
|
216
|
|
|
* @param string $view View name |
|
217
|
|
|
* @param string $viewtype Viewtype |
|
218
|
|
|
* |
|
219
|
|
|
* @return string Empty string if not found |
|
220
|
|
|
*/ |
|
221
|
2243 |
|
public function findViewFile(string $view, string $viewtype): string { |
|
222
|
2243 |
|
if (!isset($this->locations[$viewtype][$view])) { |
|
223
|
1536 |
|
return ''; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
2204 |
|
$path = $this->locations[$viewtype][$view]; |
|
227
|
|
|
|
|
228
|
2204 |
|
return $this->fileExists($path) ? $path : ''; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Register a viewtype to fall back to a default view if a view isn't |
|
233
|
|
|
* found for that viewtype. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $viewtype The viewtype to register |
|
236
|
|
|
* |
|
237
|
|
|
* @return void |
|
238
|
|
|
* |
|
239
|
|
|
* @see elgg_register_viewtype_fallback() |
|
240
|
|
|
*/ |
|
241
|
4 |
|
public function registerViewtypeFallback(string $viewtype): void { |
|
242
|
4 |
|
$this->fallbacks[] = $viewtype; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Checks if a viewtype falls back to default. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $viewtype Viewtype |
|
249
|
|
|
* |
|
250
|
|
|
* @return bool |
|
251
|
|
|
*/ |
|
252
|
1535 |
|
public function doesViewtypeFallback(string $viewtype): bool { |
|
253
|
1535 |
|
return in_array($viewtype, $this->fallbacks); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Display a view with a deprecation notice. No missing view NOTICE is logged |
|
258
|
|
|
* |
|
259
|
|
|
* @param string $view The name and location of the view to use |
|
260
|
|
|
* @param array $vars Variables to pass to the view |
|
261
|
|
|
* @param string $suggestion Suggestion with the deprecation message |
|
262
|
|
|
* @param string $version Human-readable *release* version: 1.7, 1.8, ... |
|
263
|
|
|
* |
|
264
|
|
|
* @return string The parsed view |
|
265
|
|
|
* |
|
266
|
|
|
* @see elgg_view() |
|
267
|
|
|
*/ |
|
268
|
|
|
public function renderDeprecatedView(string $view, array $vars, string $suggestion, string $version): string { |
|
269
|
|
|
$rendered = $this->renderView($view, $vars, '', false); |
|
270
|
|
|
if ($rendered) { |
|
271
|
|
|
$this->logDeprecatedMessage("The '{$view}' view has been deprecated. {$suggestion}", $version); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
return $rendered; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get the views, including extensions, used to render a view |
|
279
|
|
|
* |
|
280
|
|
|
* Keys returned are view priorities. View existence is not checked. |
|
281
|
|
|
* |
|
282
|
|
|
* @param string $view View name |
|
283
|
|
|
* |
|
284
|
|
|
* @return string[] |
|
285
|
|
|
*/ |
|
286
|
1502 |
|
public function getViewList(string $view): array { |
|
287
|
1502 |
|
return $this->extensions[$view] ?? [self::BASE_VIEW_PRIORITY => $view]; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Renders a view |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $view Name of the view |
|
294
|
|
|
* @param array $vars Variables to pass to the view |
|
295
|
|
|
* @param string $viewtype Viewtype to use |
|
296
|
|
|
* @param null|bool $issue_missing_notice Should a missing notice be issued |
|
297
|
|
|
* @param array $extensions_tree Array of views that are before the current view in the extension path |
|
298
|
|
|
* |
|
299
|
|
|
* @return string |
|
300
|
|
|
* |
|
301
|
|
|
* @see elgg_view() |
|
302
|
|
|
*/ |
|
303
|
275 |
|
public function renderView(string $view, array $vars = [], string $viewtype = '', ?bool $issue_missing_notice = null, array $extensions_tree = []): string { |
|
304
|
|
|
// basic checking for bad paths |
|
305
|
275 |
|
if (str_contains($view, '..')) { |
|
306
|
|
|
return ''; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
// check for extension deadloops |
|
310
|
275 |
|
if (in_array($view, $extensions_tree)) { |
|
311
|
2 |
|
$this->getLogger()->error("View {$view} is detected as an extension of itself. This is not allowed"); |
|
312
|
|
|
|
|
313
|
2 |
|
return ''; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
275 |
|
$extensions_tree[] = $view; |
|
317
|
|
|
|
|
318
|
|
|
// Get the current viewtype |
|
319
|
275 |
|
if ($viewtype === '' || !$this->isValidViewtype($viewtype)) { |
|
320
|
258 |
|
$viewtype = $this->getViewtype(); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
275 |
|
if (!isset($issue_missing_notice)) { |
|
324
|
275 |
|
$issue_missing_notice = $viewtype === 'default'; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
// allow altering $vars |
|
328
|
275 |
|
$vars_event_params = [ |
|
329
|
275 |
|
'view' => $view, |
|
330
|
275 |
|
'vars' => $vars, |
|
331
|
275 |
|
'viewtype' => $viewtype, |
|
332
|
275 |
|
]; |
|
333
|
275 |
|
$vars = $this->events->triggerResults(self::VIEW_VARS_HOOK, $view, $vars_event_params, $vars); |
|
334
|
|
|
|
|
335
|
|
|
// allow $vars to hijack output |
|
336
|
275 |
|
if (isset($vars[self::OUTPUT_KEY])) { |
|
337
|
2 |
|
return (string) $vars[self::OUTPUT_KEY]; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
273 |
|
$viewlist = $this->getViewList($view); |
|
341
|
|
|
|
|
342
|
273 |
|
$content = ''; |
|
343
|
273 |
|
foreach ($viewlist as $priority => $view_name) { |
|
344
|
273 |
|
if ($priority !== self::BASE_VIEW_PRIORITY) { |
|
345
|
|
|
// the others are extensions |
|
346
|
68 |
|
$content .= $this->renderView($view_name, $vars, $viewtype, $issue_missing_notice, $extensions_tree); |
|
347
|
68 |
|
continue; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
// actual rendering of a single view |
|
351
|
273 |
|
$rendering = $this->renderViewFile($view_name, $vars, $viewtype, $issue_missing_notice); |
|
352
|
243 |
|
if ($rendering !== false) { |
|
353
|
236 |
|
$content .= $rendering; |
|
354
|
236 |
|
continue; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
// attempt to load default view |
|
358
|
65 |
|
if ($viewtype !== 'default' && $this->doesViewtypeFallback($viewtype)) { |
|
359
|
2 |
|
$rendering = $this->renderViewFile($view_name, $vars, 'default', $issue_missing_notice); |
|
360
|
2 |
|
if ($rendering !== false) { |
|
361
|
2 |
|
$content .= $rendering; |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
243 |
|
$params = [ |
|
367
|
243 |
|
'view' => $view, |
|
368
|
243 |
|
'vars' => $vars, |
|
369
|
243 |
|
'viewtype' => $viewtype, |
|
370
|
243 |
|
]; |
|
371
|
|
|
|
|
372
|
243 |
|
return (string) $this->events->triggerResults(self::VIEW_HOOK, $view, $params, $content); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Wrapper for file_exists() that caches false results (the stat cache only caches true results). |
|
377
|
|
|
* This saves us from many unneeded file stat calls when a common view uses a fallback. |
|
378
|
|
|
* |
|
379
|
|
|
* @param string $path Path to the file |
|
380
|
|
|
* |
|
381
|
|
|
* @return bool |
|
382
|
|
|
*/ |
|
383
|
2204 |
|
protected function fileExists(string $path): bool { |
|
384
|
2204 |
|
if (!isset($this->file_exists_cache[$path])) { |
|
385
|
2062 |
|
$this->file_exists_cache[$path] = file_exists($path); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
2204 |
|
return $this->file_exists_cache[$path]; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* Includes view PHP or static file |
|
393
|
|
|
* |
|
394
|
|
|
* @param string $view The view name |
|
395
|
|
|
* @param array $vars Variables passed to view |
|
396
|
|
|
* @param string $viewtype The viewtype |
|
397
|
|
|
* @param bool $issue_missing_notice Log a notice if the view is missing |
|
398
|
|
|
* |
|
399
|
|
|
* @return string|false output generated by view file inclusion or false |
|
400
|
|
|
*/ |
|
401
|
273 |
|
protected function renderViewFile(string $view, array $vars, string $viewtype, bool $issue_missing_notice): string|false { |
|
402
|
273 |
|
$file = $this->findViewFile($view, $viewtype); |
|
403
|
273 |
|
if (!$file) { |
|
404
|
65 |
|
if ($issue_missing_notice) { |
|
405
|
59 |
|
$this->getLogger()->notice("{$viewtype}/{$view} view does not exist."); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
65 |
|
return false; |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
268 |
|
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { |
|
412
|
252 |
|
ob_start(); |
|
413
|
|
|
|
|
414
|
|
|
try { |
|
415
|
|
|
// don't isolate, scripts use the local $vars |
|
416
|
252 |
|
include $file; |
|
417
|
|
|
|
|
418
|
222 |
|
return ob_get_clean(); |
|
419
|
30 |
|
} catch (\Exception $e) { |
|
420
|
30 |
|
ob_get_clean(); |
|
421
|
30 |
|
throw $e; |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
84 |
|
return file_get_contents($file); |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* Returns whether the specified view exists |
|
430
|
|
|
* |
|
431
|
|
|
* @param string $view The view name |
|
432
|
|
|
* @param string $viewtype If set, forces the viewtype |
|
433
|
|
|
* @param bool $recurse If false, do not check extensions |
|
434
|
|
|
* |
|
435
|
|
|
* @return bool |
|
436
|
|
|
* |
|
437
|
|
|
* @see elgg_view_exists() |
|
438
|
|
|
*/ |
|
439
|
2094 |
|
public function viewExists(string $view, string $viewtype = '', bool $recurse = true): bool { |
|
440
|
2094 |
|
if (empty($view)) { |
|
441
|
71 |
|
return false; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
// Detect view type |
|
445
|
2094 |
|
if ($viewtype === '' || !$this->isValidViewtype($viewtype)) { |
|
446
|
866 |
|
$viewtype = $this->getViewtype(); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
|
|
450
|
2094 |
|
$file = $this->findViewFile($view, $viewtype); |
|
451
|
2094 |
|
if ($file) { |
|
452
|
2069 |
|
return true; |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
// If we got here then check whether this exists as an extension |
|
456
|
|
|
// We optionally recursively check whether the extended view exists also for the viewtype |
|
457
|
108 |
|
if ($recurse && isset($this->extensions[$view])) { |
|
458
|
12 |
|
foreach ($this->extensions[$view] as $view_extension) { |
|
459
|
|
|
// do not recursively check to stay away from infinite loops |
|
460
|
12 |
|
if ($this->viewExists($view_extension, $viewtype, false)) { |
|
461
|
|
|
return true; |
|
462
|
|
|
} |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
// Now check if the default view exists if the view is registered as a fallback |
|
467
|
108 |
|
if ($viewtype !== 'default' && $this->doesViewtypeFallback($viewtype)) { |
|
468
|
2 |
|
return $this->viewExists($view, 'default'); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
106 |
|
return false; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* Extends a view with another view |
|
476
|
|
|
* |
|
477
|
|
|
* @param string $view The view to extend. |
|
478
|
|
|
* @param string $view_extension This view is added to $view |
|
479
|
|
|
* @param int $priority The priority, from 0 to 1000, to add at (lowest numbers displayed first) |
|
480
|
|
|
* |
|
481
|
|
|
* @return void |
|
482
|
|
|
* |
|
483
|
|
|
* @see elgg_extend_view() |
|
484
|
|
|
*/ |
|
485
|
747 |
|
public function extendView(string $view, string $view_extension, int $priority = 501): void { |
|
486
|
747 |
|
if ($view === $view_extension) { |
|
487
|
|
|
// do not allow direct extension on self with self |
|
488
|
2 |
|
return; |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
745 |
|
if (!isset($this->extensions[$view])) { |
|
492
|
717 |
|
$this->extensions[$view][self::BASE_VIEW_PRIORITY] = $view; |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
// raise priority until it doesn't match one already registered |
|
496
|
745 |
|
while (isset($this->extensions[$view][$priority])) { |
|
497
|
715 |
|
$priority++; |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
745 |
|
$this->extensions[$view][$priority] = $view_extension; |
|
501
|
745 |
|
ksort($this->extensions[$view]); |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* Unextends a view. |
|
506
|
|
|
* |
|
507
|
|
|
* @param string $view The view that was extended. |
|
508
|
|
|
* @param string $view_extension This view that was added to $view |
|
509
|
|
|
* |
|
510
|
|
|
* @return bool |
|
511
|
|
|
* |
|
512
|
|
|
* @see elgg_unextend_view() |
|
513
|
|
|
*/ |
|
514
|
6 |
|
public function unextendView(string $view, string $view_extension): bool { |
|
515
|
6 |
|
if (!isset($this->extensions[$view])) { |
|
516
|
|
|
return false; |
|
517
|
|
|
} |
|
518
|
|
|
|
|
519
|
6 |
|
$extensions = $this->extensions[$view]; |
|
520
|
6 |
|
unset($extensions[self::BASE_VIEW_PRIORITY]); // we do not want the base view to be removed from the list |
|
521
|
|
|
|
|
522
|
6 |
|
$priority = array_search($view_extension, $extensions); |
|
523
|
6 |
|
if ($priority === false) { |
|
524
|
5 |
|
return false; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
3 |
|
unset($this->extensions[$view][$priority]); |
|
528
|
|
|
|
|
529
|
3 |
|
return true; |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Register all views in a given path |
|
534
|
|
|
* |
|
535
|
|
|
* @param string $path Base path to scan for views |
|
536
|
|
|
* |
|
537
|
|
|
* @return bool |
|
538
|
|
|
*/ |
|
539
|
1381 |
|
public function registerViewsFromPath(string $path): bool { |
|
540
|
1381 |
|
$path = Paths::sanitize($path) . 'views/'; |
|
541
|
|
|
|
|
542
|
|
|
// do not fail on non existing views folder |
|
543
|
1381 |
|
if (!is_dir($path)) { |
|
544
|
20 |
|
return true; |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
try { |
|
548
|
1365 |
|
$dir = new \DirectoryIterator($path); |
|
549
|
|
|
} catch (\Throwable $t) { |
|
550
|
|
|
$this->getLogger()->error($t->getMessage()); |
|
551
|
|
|
return false; |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
1365 |
|
foreach ($dir as $folder) { |
|
555
|
1365 |
|
$folder_name = $folder->getBasename(); |
|
556
|
1365 |
|
if (!$folder->isDir() || str_starts_with($folder_name, '.')) { |
|
557
|
1365 |
|
continue; |
|
558
|
|
|
} |
|
559
|
|
|
|
|
560
|
1365 |
|
if (!$this->autoregisterViews('', $folder->getPathname(), $folder_name)) { |
|
561
|
|
|
return false; |
|
562
|
|
|
} |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
1365 |
|
return true; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Merge a specification of absolute view paths |
|
570
|
|
|
* |
|
571
|
|
|
* @param array $spec Specification |
|
572
|
|
|
* viewtype => [ |
|
573
|
|
|
* view_name => path or array of paths |
|
574
|
|
|
* ] |
|
575
|
|
|
* |
|
576
|
|
|
* @return void |
|
577
|
|
|
*/ |
|
578
|
79 |
|
public function mergeViewsSpec(array $spec): void { |
|
579
|
79 |
|
foreach ($spec as $viewtype => $list) { |
|
580
|
79 |
|
foreach ($list as $view => $paths) { |
|
581
|
79 |
|
if (!is_array($paths)) { |
|
582
|
79 |
|
$paths = [$paths]; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
79 |
|
foreach ($paths as $path) { |
|
586
|
79 |
|
if (!preg_match('~^([/\\\\]|[a-zA-Z]\:)~', $path)) { |
|
587
|
|
|
// relative path |
|
588
|
74 |
|
$path = Paths::project() . $path; |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
79 |
|
if (str_ends_with($view, '/')) { |
|
592
|
|
|
// prefix |
|
593
|
77 |
|
$this->autoregisterViews($view, $path, $viewtype); |
|
594
|
|
|
} else { |
|
595
|
79 |
|
$this->setViewLocation($view, $viewtype, $path); |
|
596
|
|
|
} |
|
597
|
|
|
} |
|
598
|
|
|
} |
|
599
|
|
|
} |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* List all views in a viewtype |
|
604
|
|
|
* |
|
605
|
|
|
* @param string $viewtype Viewtype |
|
606
|
|
|
* |
|
607
|
|
|
* @return string[] |
|
608
|
|
|
*/ |
|
609
|
2 |
|
public function listViews(string $viewtype = 'default'): array { |
|
610
|
2 |
|
return array_keys($this->locations[$viewtype] ?? []); |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
/** |
|
614
|
|
|
* Get inspector data |
|
615
|
|
|
* |
|
616
|
|
|
* @return array |
|
617
|
|
|
*/ |
|
618
|
|
|
public function getInspectorData(): array { |
|
619
|
|
|
$cached_overrides = $this->server_cache->load('view_overrides'); |
|
620
|
|
|
|
|
621
|
|
|
return [ |
|
622
|
|
|
'locations' => $this->locations, |
|
623
|
|
|
'overrides' => is_array($cached_overrides) ? $cached_overrides : $this->overrides, |
|
624
|
|
|
'extensions' => $this->extensions, |
|
625
|
|
|
'simplecache' => _elgg_services()->simpleCache->getCacheableViews(), |
|
626
|
|
|
]; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* Configure locations from the cache |
|
631
|
|
|
* |
|
632
|
|
|
* @return void |
|
633
|
|
|
*/ |
|
634
|
9919 |
|
public function configureFromCache(): void { |
|
635
|
9919 |
|
if (!$this->server_cache->isEnabled()) { |
|
636
|
9288 |
|
return; |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
633 |
|
$data = $this->server_cache->load('view_locations'); |
|
640
|
633 |
|
if (!is_array($data)) { |
|
641
|
28 |
|
return; |
|
642
|
|
|
} |
|
643
|
|
|
|
|
644
|
607 |
|
$this->locations = $data['locations']; |
|
645
|
607 |
|
$this->locations_loaded_from_cache = true; |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* Cache the configuration |
|
650
|
|
|
* |
|
651
|
|
|
* @return void |
|
652
|
|
|
*/ |
|
653
|
685 |
|
public function cacheConfiguration(): void { |
|
654
|
685 |
|
if (!$this->server_cache->isEnabled()) { |
|
655
|
52 |
|
return; |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
// only cache if not already loaded |
|
659
|
633 |
|
if ($this->isViewLocationsLoadedFromCache()) { |
|
660
|
607 |
|
return; |
|
661
|
|
|
} |
|
662
|
|
|
|
|
663
|
28 |
|
if (empty($this->locations)) { |
|
664
|
10 |
|
$this->server_cache->delete('view_locations'); |
|
665
|
10 |
|
return; |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
18 |
|
$this->server_cache->save('view_locations', ['locations' => $this->locations]); |
|
669
|
|
|
|
|
670
|
|
|
// this is saved just for the inspector and is not loaded in loadAll() |
|
671
|
18 |
|
$this->server_cache->save('view_overrides', $this->overrides); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
/** |
|
675
|
|
|
* Checks if view_locations have been loaded from cache. |
|
676
|
|
|
* This can be used to determine if there is a need to (re)load view locations |
|
677
|
|
|
* |
|
678
|
|
|
* @return bool |
|
679
|
|
|
*/ |
|
680
|
747 |
|
public function isViewLocationsLoadedFromCache(): bool { |
|
681
|
747 |
|
return $this->locations_loaded_from_cache; |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
/** |
|
685
|
|
|
* Returns an array of names of ES modules detected based on view location |
|
686
|
|
|
* |
|
687
|
|
|
* @return array |
|
688
|
|
|
*/ |
|
689
|
54 |
|
public function getESModules(): array { |
|
690
|
54 |
|
$modules = $this->server_cache->load('esmodules'); |
|
691
|
54 |
|
if (is_array($modules)) { |
|
692
|
44 |
|
return $modules; |
|
693
|
|
|
} |
|
694
|
|
|
|
|
695
|
10 |
|
$modules = []; |
|
696
|
10 |
|
foreach ($this->locations['default'] as $name => $path) { |
|
697
|
10 |
|
if (!str_ends_with($name, '.mjs')) { |
|
698
|
10 |
|
continue; |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
10 |
|
$modules[] = $name; |
|
702
|
|
|
} |
|
703
|
|
|
|
|
704
|
10 |
|
$this->server_cache->save('esmodules', $modules); |
|
705
|
|
|
|
|
706
|
10 |
|
return $modules; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Update the location of a view file |
|
711
|
|
|
* |
|
712
|
|
|
* @param string $view View name |
|
713
|
|
|
* @param string $viewtype Viewtype |
|
714
|
|
|
* @param string $path File path |
|
715
|
|
|
* |
|
716
|
|
|
* @return void |
|
717
|
|
|
*/ |
|
718
|
1533 |
|
protected function setViewLocation(string $view, string $viewtype, string $path): void { |
|
719
|
1533 |
|
$path = strtr($path, '\\', '/'); |
|
720
|
|
|
|
|
721
|
1533 |
|
if (isset($this->locations[$viewtype][$view]) && $path !== $this->locations[$viewtype][$view]) { |
|
722
|
33 |
|
$this->overrides[$viewtype][$view][] = $this->locations[$viewtype][$view]; |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
1533 |
|
$this->locations[$viewtype][$view] = $path; |
|
726
|
|
|
|
|
727
|
|
|
// Test if view is cacheable and push it to the cacheable views stack, |
|
728
|
|
|
// if it's not registered as cacheable explicitly |
|
729
|
1533 |
|
_elgg_services()->simpleCache->isCacheableView($view); |
|
730
|
|
|
} |
|
731
|
|
|
} |
|
732
|
|
|
|