22 const VIEW_HOOK =
'view';
23 const VIEW_VARS_HOOK =
'view_vars';
24 const OUTPUT_KEY =
'__view_output';
25 const BASE_VIEW_PRIORITY = 500;
31 protected $file_exists_cache = [];
38 private $locations = [];
45 private $overrides = [];
52 private $simplecache_views = [];
64 private $fallbacks = [];
93 $this->hooks = $hooks;
106 $this->viewtype = null;
125 if ($this->viewtype === null) {
126 $this->viewtype = $this->resolveViewtype();
139 if (empty($this->locations[
$viewtype])) {
140 $this->viewtype =
'default';
149 private function resolveViewtype() {
150 if ($this->request) {
151 $view = $this->request->getParam(
'view',
'',
false);
152 if ($this->isValidViewtype(
$view)) {
158 if ($this->isValidViewtype(
$view)) {
192 if (!is_string($alias)) {
198 $extension = pathinfo($canonical, PATHINFO_EXTENSION);
201 if (
strpos($canonical,
"js/") === 0) {
202 $canonical =
substr($canonical, 3);
203 if (!$hasValidFileExtension) {
206 }
else if (
strpos($canonical,
"css/") === 0) {
207 $canonical =
substr($canonical, 4);
208 if (!$hasValidFileExtension) {
209 $canonical .=
".css";
226 $folder =
rtrim($folder,
'/\\');
227 $view_base =
rtrim($view_base,
'/\\');
229 if (!is_dir($folder) || !is_readable($folder)) {
230 $this->
getLogger()->notice(
"Unable to register views from the directory: {$folder}");
235 $dir = new \DirectoryIterator($folder);
237 $this->
getLogger()->error($e->getMessage());
242 if (!empty($view_base)) {
243 $view_base_new = $view_base .
'/';
247 foreach ($dir as $fileinfo) {
248 if ($fileinfo->isDot()) {
252 $path = $fileinfo->getPathname();
254 if ($fileinfo->isDir()) {
256 $this->autoregisterViews($view_base_new . $fileinfo->getFilename(),
$path,
$viewtype);
261 $view = $view_base_new . $fileinfo->getBasename(
'.php');
282 if ($this->fileExists(
$path)) {
309 if ($this->fileExists(
"$location/$viewtype/$view.php")) {
310 $this->setViewLocation(
$view,
$viewtype,
"$location/$viewtype/$view.php");
311 }
else if ($this->fileExists(
"$location/$viewtype/$view")) {
312 $this->setViewLocation(
$view,
$viewtype,
"$location/$viewtype/$view");
338 return in_array(
$viewtype, $this->fallbacks);
356 $rendered = $this->renderView(
$view, $vars,
'',
false);
377 return [self::BASE_VIEW_PRIORITY =>
$view];
398 $this->
getLogger()->notice(
"View and Viewtype in views must be a strings: $view");
408 if (in_array(
$view, $extensions_tree)) {
409 $this->
getLogger()->error(
"View $view is detected as an extension of itself. This is not allowed");
413 $extensions_tree[] =
$view;
421 $vars_hook_params = [
426 $vars = $this->hooks->trigger(self::VIEW_VARS_HOOK,
$view, $vars_hook_params,
$vars);
429 if (isset(
$vars[self::OUTPUT_KEY])) {
430 return (
string)
$vars[self::OUTPUT_KEY];
433 $viewlist = $this->getViewList(
$view);
436 foreach ($viewlist as
$priority => $view_name) {
437 if (
$priority !== self::BASE_VIEW_PRIORITY) {
444 $rendering = $this->renderViewFile($view_name,
$vars,
$viewtype, $issue_missing_notice);
445 if ($rendering !==
false) {
452 $rendering = $this->renderViewFile($view_name,
$vars,
'default', $issue_missing_notice);
453 if ($rendering !==
false) {
479 if (!isset($this->file_exists_cache[
$path])) {
480 $this->file_exists_cache[
$path] = file_exists($path);
483 return $this->file_exists_cache[
$path];
499 if ($issue_missing_notice) {
500 $this->
getLogger()->notice(
"$viewtype/$view view does not exist.");
506 if (pathinfo($file, PATHINFO_EXTENSION) ===
'php') {
513 return ob_get_clean();
520 return file_get_contents($file);
555 foreach ($this->
extensions[$view] as $view_extension) {
557 if ($this->viewExists($view_extension,
$viewtype,
false)) {
565 return $this->viewExists($view,
'default');
585 $view_extension = self::canonicalizeViewName($view_extension);
587 if (
$view === $view_extension) {
624 return $this->hooks->hasHandler(
'view',
$view) || $this->hooks->hasHandler(
'view_vars',
$view);
639 $view_extension = self::canonicalizeViewName($view_extension);
668 $this->simplecache_views[
$view] =
true;
680 if (isset($this->simplecache_views[
$view])) {
685 $current_viewtype = $this->getViewtype();
686 $viewtypes = [$current_viewtype];
688 if ($this->doesViewtypeFallback($current_viewtype) && $current_viewtype !=
'default') {
689 $viewtypes[] =
'default';
694 $file = $this->findViewFile($view, $viewtype);
696 if ($file && pathinfo($file, PATHINFO_EXTENSION) !==
'php') {
697 $this->simplecache_views[
$view] =
true;
716 $view_dir =
"{$path}views/";
719 if (!is_dir($view_dir)) {
724 $handle = opendir($view_dir);
725 if ($handle ===
false) {
726 $this->
getLogger()->notice(
"Unable to register views from the directory: {$view_dir}");
731 while (
false !== ($view_type = readdir($handle))) {
732 $view_type_dir = $view_dir . $view_type;
734 if (
'.' !==
substr($view_type, 0, 1) && is_dir($view_type_dir)) {
735 if (!$this->autoregisterViews(
'', $view_type_dir, $view_type)) {
762 if (
preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
766 $path = Directory\Local::projectRoot()->getPath($path);
788 if (empty($this->locations[
$viewtype])) {
792 return array_keys($this->locations[$viewtype]);
801 $overrides = $this->overrides;
804 $data = $this->cache->load(
'view_overrides');
805 if (is_array(
$data)) {
811 'locations' => $this->locations,
812 'overrides' => $overrides,
814 'simplecache' => $this->simplecache_views,
827 if (!is_array(
$data)) {
831 if (empty(
$data[
'version']) ||
$data[
'version'] !==
'2.0') {
835 $this->locations =
$data[
'locations'];
836 $this->cache = $cache;
849 $cache->
save(
'view_locations', [
851 'locations' => $this->locations,
855 $cache->
save(
'view_overrides', $this->overrides);
878 $this->isCacheableView($view);
setViewtype($viewtype= '')
Set the viewtype.
__construct(PluginHooksService $hooks, HttpRequest $request)
Constructor.
isCacheableView($view)
Is the view cacheable.
listViews($viewtype= 'default')
List all views in a viewtype.
$params
Saves global plugin settings.
$paths
We handle here two possible locations of composer-generated autoload file.
extendView($view, $view_extension, $priority=501)
Extends a view with another view.
registerCacheableView($view)
Register a view a cacheable.
static canonicalizeViewName($alias)
Takes a view name and returns the canonical name for that view.
cacheConfiguration(SystemCache $cache)
Cache the configuration.
doesViewtypeFallback($viewtype)
Checks if a viewtype falls back to default.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
registerViewtypeFallback($viewtype)
Register a viewtype to fall back to a default view if a view isn't found for that viewtype...
viewExists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
getInspectorData()
Get inspector data.
trait Loggable
Enables adding a logger.
getViewtype()
Get the viewtype.
mergeViewsSpec(array $spec)
Merge a specification of absolute view paths.
renderView($view, array $vars=[], $viewtype= '', $issue_missing_notice=true, array $extensions_tree=[])
Renders a view.
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
clampViewtypeToPopulatedViews()
If the current viewtype has no views, reset it to "default".
getLogger()
Returns logger.
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.
unextendView($view, $view_extension)
Unextends a view.
setViewDir($view, $location, $viewtype= '')
Set an alternative base location for a view.
findViewFile($view, $viewtype)
Find the view file.
registerPluginViews($path)
Register a plugin's views.
getViewList($view)
Get the views, including extensions, used to render a view.
save($type, $data, int $expire_after=null)
Saves a system cache.
renderDeprecatedView($view, array $vars, $suggestion, $version)
Display a view with a deprecation notice.
isValidViewtype($viewtype)
Checks if $viewtype is a string suitable for use as a viewtype name.
autoregisterViews($view_base, $folder, $viewtype)
Auto-registers views from a location.
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
viewHasHookHandlers($view)
Do hook handlers exist to modify the view?
load($type)
Retrieve the contents of a system cache.
logDeprecatedMessage(string $message, string $version)
Sends a message about deprecated use of a function, view, etc.