21 const VIEW_HOOK =
'view';
22 const VIEW_VARS_HOOK =
'view_vars';
23 const OUTPUT_KEY =
'__view_output';
24 const BASE_VIEW_PRIORITY = 500;
30 protected array $file_exists_cache = [];
37 protected array $locations = [];
44 protected array $overrides = [];
56 protected array $fallbacks = [];
60 protected bool $locations_loaded_from_cache =
false;
87 $this->viewtype = null;
92 if ($this->isValidViewtype($viewtype)) {
107 if (!isset($this->viewtype)) {
108 $this->viewtype = $this->resolveViewtype();
120 if ($this->request) {
121 $view = $this->request->getParam(
'view',
'',
false);
122 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
127 $view = (string) $this->config->view;
128 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
143 if ($viewtype ===
'') {
164 $folder =
rtrim($folder,
'/\\');
165 $view_base =
rtrim($view_base,
'/\\');
167 if (!is_dir($folder) || !is_readable($folder)) {
168 $this->
getLogger()->notice(
"Unable to register views from the directory: {$folder}");
173 $dir = new \DirectoryIterator($folder);
175 $this->
getLogger()->error($e->getMessage());
180 if (!empty($view_base)) {
181 $view_base_new = $view_base .
'/';
185 foreach ($dir as $fileinfo) {
186 if ($fileinfo->isDot()) {
190 $path = $fileinfo->getPathname();
192 if ($fileinfo->isDir()) {
194 $this->autoregisterViews($view_base_new . $fileinfo->getFilename(),
$path,
$viewtype);
199 $view = $view_base_new . $fileinfo->getBasename(
'.php');
200 $this->setViewLocation(
$view, $viewtype,
$path);
215 if (!isset($this->locations[$viewtype][$view])) {
220 if ($this->fileExists(
$path)) {
249 return in_array($viewtype, $this->fallbacks);
265 $rendered = $this->renderView($view, $vars,
'',
false);
299 public function renderView(
string $view, array
$vars = [],
string $viewtype =
'',
bool $issue_missing_notice = null, array $extensions_tree = []): string {
301 if (str_contains($view,
'..')) {
306 if (in_array($view, $extensions_tree)) {
307 $this->
getLogger()->error(
"View $view is detected as an extension of itself. This is not allowed");
312 $extensions_tree[] =
$view;
315 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
316 $viewtype = $this->getViewtype();
319 if (!isset($issue_missing_notice)) {
320 $issue_missing_notice = $viewtype ===
'default';
324 $vars_event_params = [
329 $vars = $this->events->triggerResults(self::VIEW_VARS_HOOK, $view, $vars_event_params,
$vars);
332 if (isset(
$vars[self::OUTPUT_KEY])) {
333 return (
string)
$vars[self::OUTPUT_KEY];
336 $viewlist = $this->getViewList($view);
339 foreach ($viewlist as
$priority => $view_name) {
340 if (
$priority !== self::BASE_VIEW_PRIORITY) {
342 $content .= $this->renderView($view_name,
$vars, $viewtype, $issue_missing_notice, $extensions_tree);
347 $rendering = $this->renderViewFile($view_name,
$vars, $viewtype, $issue_missing_notice);
348 if ($rendering !==
false) {
354 if ($viewtype !==
'default' && $this->doesViewtypeFallback($viewtype)) {
355 $rendering = $this->renderViewFile($view_name,
$vars,
'default', $issue_missing_notice);
356 if ($rendering !==
false) {
368 return (
string) $this->events->triggerResults(self::VIEW_HOOK, $view,
$params,
$content);
380 if (!isset($this->file_exists_cache[$path])) {
381 $this->file_exists_cache[
$path] = file_exists($path);
384 return $this->file_exists_cache[
$path];
398 $file = $this->findViewFile($view, $viewtype);
400 if ($issue_missing_notice) {
401 $this->
getLogger()->notice(
"$viewtype/$view view does not exist.");
407 if (pathinfo($file, PATHINFO_EXTENSION) ===
'php') {
414 return ob_get_clean();
421 return file_get_contents($file);
435 public function viewExists(
string $view,
string $viewtype =
'',
bool $recurse =
true): bool {
441 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
442 $viewtype = $this->getViewtype();
446 $file = $this->findViewFile($view, $viewtype);
453 if ($recurse && isset($this->
extensions[$view])) {
454 foreach ($this->
extensions[$view] as $view_extension) {
456 if ($this->viewExists($view_extension, $viewtype,
false)) {
463 if ($viewtype !=
'default' && $this->doesViewtypeFallback($viewtype)) {
464 return $this->viewExists($view,
'default');
482 if ($view === $view_extension) {
516 unset($extensions[self::BASE_VIEW_PRIORITY]);
518 $priority = array_search($view_extension, $extensions);
536 $path = Paths::sanitize($path);
537 $view_dir =
"{$path}views/";
540 if (!is_dir($view_dir)) {
545 $handle = opendir($view_dir);
546 if ($handle ===
false) {
547 $this->
getLogger()->notice(
"Unable to register views from the directory: {$view_dir}");
552 while (($view_type = readdir($handle)) !==
false) {
553 $view_type_dir = $view_dir . $view_type;
555 if (!str_starts_with($view_type,
'.') && is_dir($view_type_dir)) {
556 if (!$this->autoregisterViews(
'', $view_type_dir, $view_type)) {
576 foreach ($spec as $viewtype =>
$list) {
583 if (!
preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
585 $path = Directory\Local::projectRoot()->getPath($path);
588 if (str_ends_with(
$view,
'/')) {
590 $this->autoregisterViews(
$view, $path, $viewtype);
592 $this->setViewLocation(
$view, $viewtype, $path);
606 public function listViews(
string $viewtype =
'default'): array {
607 if (empty($this->locations[$viewtype])) {
611 return array_keys($this->locations[$viewtype]);
620 $overrides = $this->overrides;
622 if ($this->server_cache) {
623 $data = $this->server_cache->
load(
'view_overrides');
624 if (is_array(
$data)) {
630 'locations' => $this->locations,
631 'overrides' => $overrides,
633 'simplecache' =>
_elgg_services()->simpleCache->getCacheableViews(),
643 if (!$this->server_cache->isEnabled()) {
647 $data = $this->server_cache->
load(
'view_locations');
648 if (!is_array(
$data)) {
652 $this->locations =
$data[
'locations'];
653 $this->locations_loaded_from_cache =
true;
662 if (!$this->server_cache->isEnabled()) {
667 if ($this->isViewLocationsLoadedFromCache()) {
671 if (empty($this->locations)) {
672 $this->server_cache->delete(
'view_locations');
676 $this->server_cache->save(
'view_locations', [
'locations' => $this->locations]);
679 $this->server_cache->save(
'view_overrides', $this->overrides);
689 return $this->locations_loaded_from_cache;
698 $modules = $this->server_cache->load(
'esmodules');
699 if (is_array($modules)) {
704 foreach ($this->locations[
'default'] as
$name =>
$path) {
705 if (!str_ends_with(
$name,
'.mjs')) {
712 $this->server_cache->save(
'esmodules', $modules);
727 $path = strtr($path,
'\\',
'/');
729 if (isset($this->locations[$viewtype][$view]) && $path !== $this->locations[$viewtype][$view]) {
viewExists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
$params
Saves global plugin settings.
$paths
We handle here two possible locations of composer-generated autoload file.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH)!== '/') if(file_exists(elgg_get_root_path(). 'robots.txt'))
Set robots.txt.
isValidViewtype(string $viewtype)
Checks if $viewtype is a string suitable for use as a viewtype name.
if(!$user||!$user->canDelete()) $name
listViews(string $viewtype= 'default')
List all views in a viewtype.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
renderDeprecatedView(string $view, array $vars, string $suggestion, string $version)
Display a view with a deprecation notice.
registerViewsFromPath(string $path)
Register all views in a given path.
cacheConfiguration()
Cache the configuration.
renderView(string $view, array $vars=[], string $viewtype= '', bool $issue_missing_notice=null, array $extensions_tree=[])
Renders a view.
configureFromCache()
Configure locations from the cache.
$config
Advanced site settings, debugging section.
resolveViewtype()
Resolve the initial viewtype.
if(!$entity instanceof\ElggUser) $data
getInspectorData()
Get inspector data.
trait Loggable
Enables adding a logger.
getViewtype()
Get the viewtype.
unextendView(string $view, string $view_extension)
Unextends a view.
mergeViewsSpec(array $spec)
Merge a specification of absolute view paths.
setViewtype(string $viewtype= '')
Set the viewtype.
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
load(stdClass $row)
Loads attributes from the entities table into the object.
extendView(string $view, string $view_extension, int $priority=501)
Extends a view with another view.
findViewFile(string $view, string $viewtype)
Find the view file.
isViewLocationsLoadedFromCache()
Checks if view_locations have been loaded from cache.
doesViewtypeFallback(string $viewtype)
Checks if a viewtype falls back to default.
getLogger()
Returns logger.
registerViewtypeFallback(string $viewtype)
Register a viewtype to fall back to a default view if a view isn't found for that viewtype...
getViewList(string $view)
Get the views, including extensions, used to render a view.
$content
Set robots.txt action.
_elgg_services()
Get the global service provider.
autoregisterViews(string $view_base, string $folder, string $viewtype)
Auto-registers views from a location.
__construct(protected EventsService $events, protected HttpRequest $request, protected Config $config, protected SystemCache $server_cache)
Constructor.
fileExists(string $path)
Wrapper for file_exists() that caches false results (the stat cache only caches true results)...
getESModules()
Returns an array of names of ES modules detected based on view location.
renderViewFile(string $view, array $vars, string $viewtype, bool $issue_missing_notice)
Includes view PHP or static file.
setViewLocation(string $view, string $viewtype, string $path)
Update the location of a view file.
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.