23 const VIEW_HOOK =
'view';
24 const VIEW_VARS_HOOK =
'view_vars';
25 const OUTPUT_KEY =
'__view_output';
26 const BASE_VIEW_PRIORITY = 500;
32 protected $file_exists_cache = [];
39 private $locations = [];
46 private $overrides = [];
53 private $simplecache_views = [];
65 private $fallbacks = [];
95 $this->hooks = $hooks;
96 $this->logger = $logger;
109 $this->viewtype = null;
128 if ($this->viewtype === null) {
129 $this->viewtype = $this->resolveViewtype();
142 if (empty($this->locations[
$viewtype])) {
143 $this->viewtype =
'default';
152 private function resolveViewtype() {
153 if ($this->request) {
154 $view = $this->request->getParam(
'view',
'',
false);
155 if ($this->isValidViewtype(
$view)) {
161 if ($this->isValidViewtype(
$view)) {
195 if (!is_string($alias)) {
201 $extension = pathinfo($canonical, PATHINFO_EXTENSION);
204 if (strpos($canonical,
"js/") === 0) {
205 $canonical = substr($canonical, 3);
206 if (!$hasValidFileExtension) {
209 }
else if (strpos($canonical,
"css/") === 0) {
210 $canonical = substr($canonical, 4);
211 if (!$hasValidFileExtension) {
212 $canonical .=
".css";
231 $folder = rtrim($folder,
'/\\');
232 $view_base = rtrim($view_base,
'/\\');
234 $handle = opendir($folder);
239 while ($entry = readdir($handle)) {
240 if ($entry[0] ===
'.') {
244 $path =
"$folder/$entry";
246 if (!empty($view_base)) {
247 $view_base_new = $view_base .
"/";
253 $this->autoregisterViews($view_base_new . $entry,
$path,
$viewtype);
255 $view = $view_base_new . basename($entry,
'.php');
277 if ($this->fileExists(
$path)) {
304 if ($this->fileExists(
"$location/$viewtype/$view.php")) {
305 $this->setViewLocation(
$view,
$viewtype,
"$location/$viewtype/$view.php");
306 }
else if ($this->fileExists(
"$location/$viewtype/$view")) {
307 $this->setViewLocation(
$view,
$viewtype,
"$location/$viewtype/$view");
335 return in_array(
$viewtype, $this->fallbacks);
353 $rendered = $this->renderView(
$view, $vars,
'',
false);
374 return [self::BASE_VIEW_PRIORITY =>
$view];
395 $this->logger->notice(
"View and Viewtype in views must be a strings: $view");
400 if (strpos(
$view,
'..') !==
false) {
405 if (in_array(
$view, $extensions_tree)) {
406 $this->logger->error(
"View $view is detected as an extension of itself. This is not allowed");
410 $extensions_tree[] =
$view;
418 $vars_hook_params = [
423 $vars = $this->hooks->trigger(self::VIEW_VARS_HOOK,
$view, $vars_hook_params,
$vars);
426 if (isset(
$vars[self::OUTPUT_KEY])) {
427 return (
string)
$vars[self::OUTPUT_KEY];
430 $viewlist = $this->getViewList(
$view);
433 foreach ($viewlist as
$priority => $view_name) {
434 if (
$priority !== self::BASE_VIEW_PRIORITY) {
441 $rendering = $this->renderViewFile($view_name,
$vars,
$viewtype, $issue_missing_notice);
442 if ($rendering !==
false) {
449 $rendering = $this->renderViewFile($view_name,
$vars,
'default', $issue_missing_notice);
450 if ($rendering !==
false) {
476 if (!isset($this->file_exists_cache[
$path])) {
477 $this->file_exists_cache[
$path] = file_exists($path);
480 return $this->file_exists_cache[
$path];
496 if ($issue_missing_notice) {
497 $this->logger->notice(
"$viewtype/$view view does not exist.");
503 if (pathinfo(
$file, PATHINFO_EXTENSION) ===
'php') {
510 return ob_get_clean();
517 return file_get_contents(
$file);
552 foreach ($this->
extensions[$view] as $view_extension) {
554 if ($this->viewExists($view_extension,
$viewtype,
false)) {
562 return $this->viewExists($view,
'default');
582 $view_extension = self::canonicalizeViewName($view_extension);
584 if (
$view === $view_extension) {
610 return count($this->getViewList(
$view)) > 1;
621 return $this->hooks->hasHandler(
'view',
$view) || $this->hooks->hasHandler(
'view_vars',
$view);
636 $view_extension = self::canonicalizeViewName($view_extension);
665 $this->simplecache_views[
$view] =
true;
677 if (isset($this->simplecache_views[
$view])) {
682 $current_viewtype = $this->getViewtype();
683 $viewtypes = [$current_viewtype];
685 if ($this->doesViewtypeFallback($current_viewtype) && $current_viewtype !=
'default') {
686 $viewtypes[] =
'default';
691 $file = $this->findViewFile($view, $viewtype);
693 if (
$file && pathinfo(
$file, PATHINFO_EXTENSION) !==
'php') {
694 $this->simplecache_views[
$view] =
true;
714 $view_dir =
"$path/views/";
717 if (!is_dir($view_dir)) {
722 $handle = opendir($view_dir);
724 $failed_dir = $view_dir;
729 while (
false !== ($view_type = readdir($handle))) {
730 $view_type_dir = $view_dir . $view_type;
732 if (
'.' !== substr($view_type, 0, 1) && is_dir($view_type_dir)) {
733 if (!$this->autoregisterViews(
'', $view_type_dir, $view_type)) {
734 $failed_dir = $view_type_dir;
762 if (preg_match(
'~^([/\\\\]|[a-zA-Z]\:)~', $path)) {
766 $path = Directory\Local::projectRoot()->getPath($path);
769 if (substr(
$view, -1) ===
'/') {
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.
if(!array_key_exists($filename, $text_files)) $file
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.
__construct(PluginHooksService $hooks, LoggerInterface $logger, HttpRequest $request=null)
Constructor.
static canonicalizeViewName($alias)
Takes a view name and returns the canonical name for that view.
cacheConfiguration(SystemCache $cache)
Cache the configuration.
$request
Page handler for autocomplete endpoint.
doesViewtypeFallback($viewtype)
Checks if a viewtype falls back to default.
trait Loggable
Enables adding a logger.
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.
getViewtype()
Get the viewtype.
mergeViewsSpec(array $spec)
Merge a specification of absolute view paths.
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
registerPluginViews($path, &$failed_dir= '')
Register a plugin's views.
renderView($view, array $vars=[], $viewtype= '', $issue_missing_notice=true, array $extensions_tree=[])
Renders a view.
clampViewtypeToPopulatedViews()
If the current viewtype has no views, reset it to "default".
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
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.
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.
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.
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.