20 const VIEW_HOOK =
'view';
21 const VIEW_VARS_HOOK =
'view_vars';
22 const OUTPUT_KEY =
'__view_output';
23 const BASE_VIEW_PRIORITY = 500;
29 protected array $file_exists_cache = [];
36 protected array $locations = [];
43 protected array $overrides = [];
55 protected array $fallbacks = [];
59 protected bool $locations_loaded_from_cache =
false;
86 $this->viewtype = null;
91 if ($this->isValidViewtype($viewtype)) {
106 if (!isset($this->viewtype)) {
107 $this->viewtype = $this->resolveViewtype();
119 if ($this->request) {
120 $view = $this->request->getParam(
'view',
'',
false);
121 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
126 $view = (string) $this->config->view;
127 if ($this->isValidViewtype(
$view) && !empty($this->locations[
$view])) {
142 if ($viewtype ===
'') {
163 $folder =
rtrim($folder,
'/\\');
164 $view_base =
rtrim($view_base,
'/\\');
166 if (!is_dir($folder) || !is_readable($folder)) {
167 $this->
getLogger()->notice(
"Unable to register views from the directory: {$folder}");
172 $dir = new \DirectoryIterator($folder);
173 }
catch (\Exception $e) {
174 $this->
getLogger()->error($e->getMessage());
179 if (!empty($view_base)) {
180 $view_base_new = $view_base .
'/';
184 foreach ($dir as $fileinfo) {
185 if ($fileinfo->isDot()) {
189 $path = $fileinfo->getPathname();
191 if ($fileinfo->isDir()) {
193 $this->autoregisterViews($view_base_new . $fileinfo->getFilename(),
$path,
$viewtype);
198 $view = $view_base_new . $fileinfo->getBasename(
'.php');
199 $this->setViewLocation(
$view, $viewtype,
$path);
214 if (!isset($this->locations[$viewtype][$view])) {
219 if ($this->fileExists(
$path)) {
248 return in_array($viewtype, $this->fallbacks);
264 $rendered = $this->renderView($view, $vars,
'',
false);
298 public function renderView(
string $view, array
$vars = [],
string $viewtype =
'',
bool $issue_missing_notice = null, array $extensions_tree = []): string {
300 if (str_contains($view,
'..')) {
305 if (in_array($view, $extensions_tree)) {
306 $this->
getLogger()->error(
"View $view is detected as an extension of itself. This is not allowed");
311 $extensions_tree[] =
$view;
314 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
315 $viewtype = $this->getViewtype();
318 if (!isset($issue_missing_notice)) {
319 $issue_missing_notice = $viewtype ===
'default';
323 $vars_event_params = [
328 $vars = $this->events->triggerResults(self::VIEW_VARS_HOOK, $view, $vars_event_params,
$vars);
331 if (isset(
$vars[self::OUTPUT_KEY])) {
332 return (
string)
$vars[self::OUTPUT_KEY];
335 $viewlist = $this->getViewList($view);
338 foreach ($viewlist as
$priority => $view_name) {
339 if (
$priority !== self::BASE_VIEW_PRIORITY) {
341 $content .= $this->renderView($view_name,
$vars, $viewtype, $issue_missing_notice, $extensions_tree);
346 $rendering = $this->renderViewFile($view_name,
$vars, $viewtype, $issue_missing_notice);
347 if ($rendering !==
false) {
353 if ($viewtype !==
'default' && $this->doesViewtypeFallback($viewtype)) {
354 $rendering = $this->renderViewFile($view_name,
$vars,
'default', $issue_missing_notice);
355 if ($rendering !==
false) {
367 return (
string) $this->events->triggerResults(self::VIEW_HOOK, $view,
$params,
$content);
379 if (!isset($this->file_exists_cache[$path])) {
380 $this->file_exists_cache[
$path] = file_exists($path);
383 return $this->file_exists_cache[
$path];
397 $file = $this->findViewFile($view, $viewtype);
399 if ($issue_missing_notice) {
400 $this->
getLogger()->notice(
"$viewtype/$view view does not exist.");
406 if (pathinfo($file, PATHINFO_EXTENSION) ===
'php') {
413 return ob_get_clean();
414 }
catch (\Exception $e) {
420 return file_get_contents($file);
434 public function viewExists(
string $view,
string $viewtype =
'',
bool $recurse =
true): bool {
440 if ($viewtype ===
'' || !$this->isValidViewtype($viewtype)) {
441 $viewtype = $this->getViewtype();
445 $file = $this->findViewFile($view, $viewtype);
452 if ($recurse && isset($this->
extensions[$view])) {
453 foreach ($this->
extensions[$view] as $view_extension) {
455 if ($this->viewExists($view_extension, $viewtype,
false)) {
462 if ($viewtype !=
'default' && $this->doesViewtypeFallback($viewtype)) {
463 return $this->viewExists($view,
'default');
481 if ($view === $view_extension) {
515 unset($extensions[self::BASE_VIEW_PRIORITY]);
517 $priority = array_search($view_extension, $extensions);
535 $path = Paths::sanitize($path);
536 $view_dir =
"{$path}views/";
539 if (!is_dir($view_dir)) {
544 $handle = opendir($view_dir);
545 if ($handle ===
false) {
546 $this->
getLogger()->notice(
"Unable to register views from the directory: {$view_dir}");
551 while (($view_type = readdir($handle)) !==
false) {
552 $view_type_dir = $view_dir . $view_type;
554 if (!str_starts_with($view_type,
'.') && is_dir($view_type_dir)) {
555 if (!$this->autoregisterViews(
'', $view_type_dir, $view_type)) {
575 foreach ($spec as $viewtype =>
$list) {
582 if (!
preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
587 if (str_ends_with(
$view,
'/')) {
589 $this->autoregisterViews(
$view, $path, $viewtype);
591 $this->setViewLocation(
$view, $viewtype, $path);
605 public function listViews(
string $viewtype =
'default'): array {
606 if (empty($this->locations[$viewtype])) {
610 return array_keys($this->locations[$viewtype]);
619 $overrides = $this->overrides;
621 if ($this->server_cache) {
622 $data = $this->server_cache->
load(
'view_overrides');
623 if (is_array(
$data)) {
629 'locations' => $this->locations,
630 'overrides' => $overrides,
632 'simplecache' =>
_elgg_services()->simpleCache->getCacheableViews(),
642 if (!$this->server_cache->isEnabled()) {
646 $data = $this->server_cache->
load(
'view_locations');
647 if (!is_array(
$data)) {
651 $this->locations =
$data[
'locations'];
652 $this->locations_loaded_from_cache =
true;
661 if (!$this->server_cache->isEnabled()) {
666 if ($this->isViewLocationsLoadedFromCache()) {
670 if (empty($this->locations)) {
671 $this->server_cache->delete(
'view_locations');
675 $this->server_cache->save(
'view_locations', [
'locations' => $this->locations]);
678 $this->server_cache->save(
'view_overrides', $this->overrides);
688 return $this->locations_loaded_from_cache;
697 $modules = $this->server_cache->load(
'esmodules');
698 if (is_array($modules)) {
703 foreach ($this->locations[
'default'] as
$name =>
$path) {
704 if (!str_ends_with(
$name,
'.mjs')) {
711 $this->server_cache->save(
'esmodules', $modules);
726 $path = strtr($path,
'\\',
'/');
728 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.