19 const VIEW_HOOK =
'view';
20 const VIEW_VARS_HOOK =
'view_vars';
21 const OUTPUT_KEY =
'__view_output';
27 protected $file_exists_cache = [];
34 private $locations = [];
41 private $overrides = [];
48 private $simplecache_views = [];
60 private $fallbacks = [];
89 $this->hooks = $hooks;
90 $this->logger = $logger;
101 if (!is_string($alias)) {
107 $extension = pathinfo($canonical, PATHINFO_EXTENSION);
110 if (strpos($canonical,
"js/") === 0) {
111 $canonical = substr($canonical, 3);
112 if (!$hasValidFileExtension) {
115 }
else if (strpos($canonical,
"css/") === 0) {
116 $canonical = substr($canonical, 4);
117 if (!$hasValidFileExtension) {
118 $canonical .=
".css";
129 $folder = rtrim($folder,
'/\\');
130 $view_base = rtrim($view_base,
'/\\');
132 $handle = opendir($folder);
137 while ($entry = readdir($handle)) {
138 if ($entry[0] ===
'.') {
142 $path =
"$folder/$entry";
144 if (!empty($view_base)) {
145 $view_base_new = $view_base .
"/";
151 $this->autoregisterViews($view_base_new . $entry,
$path, $viewtype);
153 $view = $view_base_new . basename($entry,
'.php');
154 $this->setViewLocation(
$view, $viewtype,
$path);
172 if (!isset($this->locations[$viewtype][
$view])) {
177 if ($this->fileExists(
$path)) {
194 if (empty($viewtype)) {
195 $viewtype =
'default';
198 $location = rtrim($location,
'/\\');
200 if ($this->fileExists(
"$location/$viewtype/$view.php")) {
201 $this->setViewLocation(
$view, $viewtype,
"$location/$viewtype/$view.php");
202 } elseif ($this->fileExists(
"$location/$viewtype/$view")) {
203 $this->setViewLocation(
$view, $viewtype,
"$location/$viewtype/$view");
211 $this->fallbacks[] = $viewtype;
218 return in_array($viewtype, $this->fallbacks);
237 $rendered = $this->renderView(
$view, $vars,
false,
'',
false);
257 return [500 =>
$view];
264 public function renderView(
$view, array
$vars = [], $ignored =
false, $viewtype =
'', $issue_missing_notice =
true) {
267 if (!is_string(
$view) || !is_string($viewtype)) {
268 $this->logger->log(
"View and Viewtype in views must be a strings: $view",
'NOTICE');
272 if (strpos(
$view,
'..') !==
false) {
276 if (!is_array(
$vars)) {
277 $this->logger->log(
"Vars in views must be an array: $view",
'ERROR');
287 $vars_hook_params = [
290 'viewtype' => $viewtype,
292 $vars = $this->hooks->trigger(self::VIEW_VARS_HOOK,
$view, $vars_hook_params,
$vars);
295 if (isset(
$vars[self::OUTPUT_KEY])) {
296 return (
string)
$vars[self::OUTPUT_KEY];
301 $viewlist = $this->getViewList(
$view);
304 foreach ($viewlist as
$view) {
306 $rendering = $this->renderViewFile($view,
$vars, $viewtype, $issue_missing_notice);
307 if ($rendering !==
false) {
313 if ($viewtype !==
'default' && $this->doesViewtypeFallback($viewtype)) {
315 $rendering = $this->renderViewFile($view,
$vars,
'default', $issue_missing_notice);
316 if ($rendering !==
false) {
324 'view' => $view_orig,
326 'viewtype' => $viewtype,
341 if (!isset($this->file_exists_cache[
$path])) {
342 $this->file_exists_cache[
$path] = file_exists($path);
344 return $this->file_exists_cache[
$path];
357 private function renderViewFile(
$view, array
$vars, $viewtype, $issue_missing_notice) {
358 $file = $this->findViewFile(
$view, $viewtype);
360 if ($issue_missing_notice) {
361 $this->logger->log(
"$viewtype/$view view does not exist.",
'NOTICE');
366 if (pathinfo(
$file, PATHINFO_EXTENSION) ===
'php') {
372 return ob_get_clean();
375 return file_get_contents(
$file);
394 $file = $this->findViewFile(
$view, $viewtype);
402 foreach ($this->
extensions[$view] as $view_extension) {
404 if ($this->viewExists($view_extension, $viewtype,
false)) {
411 if ($viewtype !=
'default' && $this->doesViewtypeFallback($viewtype)) {
412 return $this->viewExists($view,
'default');
424 $view_extension = $this->canonicalizeViewName($view_extension);
449 return count($this->getViewList(
$view)) > 1;
462 return $this->hooks->hasHandler(
'view',
$view) || $this->hooks->hasHandler(
'view_vars',
$view);
470 $view_extension = $this->canonicalizeViewName($view_extension);
492 $this->simplecache_views[
$view] =
true;
500 if (isset($this->simplecache_views[
$view])) {
506 $viewtypes = array($current_viewtype);
508 if ($this->doesViewtypeFallback($current_viewtype) && $current_viewtype !=
'default') {
509 $viewtypes[] =
'default';
513 foreach ($viewtypes as $viewtype) {
514 $file = $this->findViewFile($view, $viewtype);
516 if (
$file && pathinfo(
$file, PATHINFO_EXTENSION) !==
'php') {
517 $this->simplecache_views[
$view] =
true;
537 $view_dir =
"$path/views/";
540 if (!is_dir($view_dir)) {
545 $handle = opendir($view_dir);
547 $failed_dir = $view_dir;
551 while (
false !== ($view_type = readdir($handle))) {
552 $view_type_dir = $view_dir . $view_type;
554 if (
'.' !== substr($view_type, 0, 1) && is_dir($view_type_dir)) {
555 if ($this->autoregisterViews(
'', $view_type_dir, $view_type)) {
558 $failed_dir = $view_type_dir;
578 foreach ($spec as $viewtype =>
$list) {
585 if (preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
589 $path = Directory\Local::root()->getPath($path);
592 if (substr(
$view, -1) ===
'/') {
594 $this->autoregisterViews(
$view, $path, $viewtype);
596 $this->setViewLocation(
$view, $viewtype, $path);
613 if (empty($this->locations[$viewtype])) {
616 return array_keys($this->locations[$viewtype]);
627 $overrides = $this->overrides;
630 $data = $this->cache->load(
'view_overrides');
632 $overrides = unserialize(
$data);
637 'locations' => $this->locations,
638 'overrides' => $overrides,
640 'simplecache' => $this->simplecache_views,
653 if (!is_string(
$data)) {
658 if (empty(
$data[
'version']) ||
$data[
'version'] !==
'2.0') {
661 $this->locations =
$data[
'locations'];
662 $this->cache = $cache;
675 $cache->
save(
'view_locations', serialize([
677 'locations' => $this->locations,
681 $cache->
save(
'view_overrides', serialize($this->overrides));
693 private function setViewLocation(
$view, $viewtype,
$path) {
697 if (isset($this->locations[$viewtype][
$view]) &&
$path !== $this->locations[$viewtype][$view]) {
698 $this->overrides[$viewtype][
$view][] = $this->locations[$viewtype][
$view];
if(!array_key_exists($filename, $text_files)) $file
isCacheableView($view)
private
listViews($viewtype= 'default')
List all views in a viewtype.
$paths
We handle here two possible locations of composer-generated autoload file.
extendView($view, $view_extension, $priority=501)
private
registerCacheableView($view)
private
cacheConfiguration(SystemCache $cache)
Cache the configuration.
doesViewtypeFallback($viewtype)
private
_elgg_is_valid_viewtype($viewtype)
Checks if $viewtype is a string suitable for use as a viewtype name.
registerViewtypeFallback($viewtype)
private
viewExists($view, $viewtype= '', $recurse=true)
private
elgg_get_viewtype()
Return the current view type.
getInspectorData()
Get inspector data.
mergeViewsSpec(array $spec)
Merge a specification of absolute view paths.
registerPluginViews($path, &$failed_dir= '')
Register a plugin's views.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
renderView($view, array $vars=[], $ignored=false, $viewtype= '', $issue_missing_notice=true)
private
save($type, $data)
Saves a system cache.
viewIsExtended($view)
Is the given view extended?
fileExists($path)
Wrapper for file_exists() that caches false results (the stat cache only caches true results)...
configureFromCache(SystemCache $cache)
Configure locations from the cache.
$content
Set robots.txt action.
canonicalizeViewName($alias)
Takes a view name and returns the canonical name for that view.
elgg_register_viewtype($viewtype)
Register a viewtype.
unextendView($view, $view_extension)
private
setViewDir($view, $location, $viewtype= '')
findViewFile($view, $viewtype)
Find the view file.
__construct(PluginHooksService $hooks, Logger $logger)
Constructor.
getViewList($view)
Get the views, including extensions, used to render a view.
renderDeprecatedView($view, array $vars, $suggestion, $version)
Display a view with a deprecation notice.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
autoregisterViews($view_base, $folder, $viewtype)
private
viewHasHookHandlers($view)
Do hook handlers exist to modify the view?
load($type)
Retrieve the contents of a system cache.