Elgg  Version 6.0
SimpleCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use Elgg\Config;
8 
15 class SimpleCache {
16 
22  protected array $simplecache_views = [];
23 
30  public function __construct(
31  protected Config $config,
32  protected ViewsService $views
33  ) {
34  }
35 
50  public function getUrl(string $view): string {
51  $this->registerCacheableView($view);
52 
53  return $this->getRoot() . $view;
54  }
55 
61  public function getRoot(): string {
62  $viewtype = $this->views->getViewtype();
63  if ($this->isEnabled()) {
64  $lastcache = (int) $this->config->lastcache;
65  } else {
66  $lastcache = 0;
67  }
68 
69  return elgg_normalize_url("/cache/{$lastcache}/{$viewtype}/");
70  }
71 
79  public function registerCacheableView(string $view): void {
80  $this->simplecache_views[$view] = true;
81  }
82 
90  public function isCacheableView(string $view): bool {
91  if (isset($this->simplecache_views[$view])) {
92  return true;
93  }
94 
95  // build list of viewtypes to check
96  $current_viewtype = $this->views->getViewtype();
97  $viewtypes = [$current_viewtype];
98 
99  if ($this->views->doesViewtypeFallback($current_viewtype) && $current_viewtype != 'default') {
100  $viewtypes[] = 'default';
101  }
102 
103  // If a static view file is found in any viewtype, it's considered cacheable
104  foreach ($viewtypes as $viewtype) {
105  $file = $this->views->findViewFile($view, $viewtype);
106 
107  if ($file && pathinfo($file, PATHINFO_EXTENSION) !== 'php') {
108  $this->simplecache_views[$view] = true;
109 
110  return true;
111  }
112  }
113 
114  // Assume not-cacheable by default
115  return false;
116  }
117 
125  public function getCacheableViews(): array {
127  }
128 
134  public function isEnabled(): bool {
135  return (bool) $this->config->simplecache_enabled;
136  }
137 
144  public function enable(): void {
145  $this->config->save('simplecache_enabled', 1);
146  }
147 
154  public function disable(): void {
155  if (!$this->isEnabled()) {
156  return;
157  }
158 
159  $this->config->save('simplecache_enabled', 0);
160  }
161 
167  protected function getPath(): string {
168  return (string) $this->config->assetroot;
169  }
170 
178  public function clear(): void {
179  elgg_delete_directory($this->getPath(), true);
180  }
181 
187  public function purge(): void {
188  $lastcache = (int) $this->config->lastcache;
189 
190  if (!is_dir($this->getPath())) {
191  return;
192  }
193 
194  $di = new \DirectoryIterator($this->getPath());
195 
196  /* @var $file_info \DirectoryIterator */
197  foreach ($di as $file_info) {
198  if (!$file_info->isDir() || $file_info->isDot()) {
199  continue;
200  }
201 
202  if ((int) $file_info->getBasename() === $lastcache) {
203  continue;
204  }
205 
206  elgg_delete_directory($file_info->getPathname());
207  }
208  }
209 
220  public function cachedAssetExists(int $cache_time, string $viewtype, string $view): bool {
221  $filename = $this->getCacheFilename($viewtype, $view, $cache_time);
222 
223  return file_exists($filename);
224  }
225 
236  public function getCachedAssetLocation(int $cache_time, string $viewtype, string $view): ?string {
237  if (!$this->cachedAssetExists($cache_time, $viewtype, $view)) {
238  return null;
239  }
240 
241  return $this->getCacheFilename($viewtype, $view, $cache_time);
242  }
243 
253  public function cacheAsset(string $viewtype, string $view, string $contents): int {
254  $filename = $this->getCacheFilename($viewtype, $view);
255  $dir = dirname($filename);
256 
257  if (!is_dir($dir)) {
258  // PHP and the server accessing the cache symlink may be a different user. And here
259  // it's safe to make everything readable anyway.
260  mkdir($dir, 0775, true);
261  }
262 
263  $result = file_put_contents($filename, $contents);
264  chmod($filename, 0664);
265 
266  return $result;
267  }
268 
278  protected function getCacheFilename(string $viewtype, string $view, int $cache_time = null): string {
279  if (!isset($cache_time)) {
280  $cache_time = $this->config->lastcache;
281  }
282 
283  $filename = $this->getPath() . "{$cache_time}/{$viewtype}/{$view}";
284  return Paths::sanitize($filename, false);
285  }
286 }
isCacheableView(string $view)
Is the view cacheable.
Definition: SimpleCache.php:90
getCacheFilename(string $viewtype, string $view, int $cache_time=null)
Get the cache file location.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH)!== '/') if(file_exists(elgg_get_root_path(). 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
getUrl(string $view)
Get the URL for the cached view.
Definition: SimpleCache.php:50
cachedAssetExists(int $cache_time, string $viewtype, string $view)
Check if a asset exists in the cache.
clear()
Deletes all cached views in the simplecache.
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
Definition: LICENSE.txt:215
purge()
Purge old/stale cache content.
__construct(protected Config $config, protected ViewsService $views)
Constructor.
Definition: SimpleCache.php:30
isEnabled()
Is simple cache enabled.
disable()
Disables the simple cache.
getRoot()
Get the base url for simple cache requests.
Definition: SimpleCache.php:61
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
enable()
Enables the simple cache.
if(!empty($avatar)&&!$avatar->isValid()) elseif(empty($avatar)) if(!$owner->saveIconFromUploadedFile('avatar')) if(!elgg_trigger_event('profileiconupdate', $owner->type, $owner)) $view
Definition: upload.php:39
getCachedAssetLocation(int $cache_time, string $viewtype, string $view)
Get the cache location of an existing cached asset.
registerCacheableView(string $view)
Register a view as cacheable.
Definition: SimpleCache.php:79
cacheAsset(string $viewtype, string $view, string $contents)
Store an asset for caching.
Views service.
$viewtype
Definition: default.php:11
elgg_delete_directory(string $directory, bool $leave_base_directory=false)
Delete a directory and all its contents.
Definition: filestore.php:51
getPath()
Returns the path to where views are simplecached.
Simple cache service.
Definition: SimpleCache.php:15
static sanitize($path, $append_slash=true)
Sanitize file paths ensuring that they begin and end with slashes etc.
Definition: Paths.php:76
elgg_normalize_url(string $url)
Definition: output.php:163
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:73
getCacheableViews()
Returns the cacheable views.
$views
Definition: item.php:17