Elgg  Version 5.1
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 
20  protected $config;
21 
25  protected $views;
26 
33  public function __construct(
36  ) {
37  $this->config = $config;
38  $this->views = $views;
39  }
40 
65  public function getUrl(string $view, string $subview = ''): string {
66  // handle `getUrl('js', 'js/blog/save_draft')`
67  if (($view === 'js' || $view === 'css') && str_starts_with($subview, $view . '/')) {
68  $view = $subview;
69  $subview = '';
70  }
71 
72  // handle `getUrl('js', 'blog/save_draft')`
73  if (!empty($subview)) {
74  $view = "{$view}/{$subview}";
75  }
76 
78 
79  // should be normalized to canonical form by now: `getUrl('blog/save_draft.js')`
80  $this->views->registerCacheableView($view);
81 
82  return $this->getRoot() . $view;
83  }
84 
90  public function getRoot() {
91  $viewtype = $this->views->getViewtype();
92  if ($this->isEnabled()) {
93  $lastcache = (int) $this->config->lastcache;
94  } else {
95  $lastcache = 0;
96  }
97 
98  return elgg_normalize_url("/cache/{$lastcache}/{$viewtype}/");
99  }
100 
106  public function isEnabled() {
107  return (bool) $this->config->simplecache_enabled;
108  }
109 
116  public function enable() {
117  $this->config->save('simplecache_enabled', 1);
118  }
119 
126  public function disable() {
127  if (!$this->isEnabled()) {
128  return;
129  }
130 
131  $this->config->save('simplecache_enabled', 0);
132  }
133 
139  protected function getPath() {
140  return (string) $this->config->assetroot;
141  }
142 
149  public function clear() {
150  elgg_delete_directory($this->getPath(), true);
151 
152  return true;
153  }
154 
160  public function purge() {
161  $lastcache = (int) $this->config->lastcache;
162 
163  if (!is_dir($this->getPath())) {
164  return;
165  }
166 
167  $di = new \DirectoryIterator($this->getPath());
168 
169  /* @var $file_info \DirectoryIterator */
170  foreach ($di as $file_info) {
171  if (!$file_info->isDir() || $file_info->isDot()) {
172  continue;
173  }
174 
175  if ((int) $file_info->getBasename() === $lastcache) {
176  continue;
177  }
178 
179  elgg_delete_directory($file_info->getPathname());
180  }
181  }
182 
193  public function cachedAssetExists(int $cache_time, string $viewtype, string $view): bool {
194  $filename = $this->getCacheFilename($viewtype, $view, $cache_time);
195 
196  return file_exists($filename);
197  }
198 
209  public function getCachedAssetLocation(int $cache_time, string $viewtype, string $view): ?string {
210  if (!$this->cachedAssetExists($cache_time, $viewtype, $view)) {
211  return null;
212  }
213 
214  return $this->getCacheFilename($viewtype, $view, $cache_time);
215  }
216 
226  public function cacheAsset(string $viewtype, string $view, string $contents): int {
227  $filename = $this->getCacheFilename($viewtype, $view);
228  $dir = dirname($filename);
229 
230  if (!is_dir($dir)) {
231  // PHP and the server accessing the cache symlink may be a different user. And here
232  // it's safe to make everything readable anyway.
233  mkdir($dir, 0775, true);
234  }
235 
236  $result = file_put_contents($filename, $contents);
237  chmod($filename, 0664);
238 
239  return $result;
240  }
241 
251  protected function getCacheFilename(string $viewtype, string $view, int $cache_time = null): string {
252  if (!isset($cache_time)) {
253  $cache_time = $this->config->lastcache;
254  }
255 
256  $filename = $this->getPath() . "{$cache_time}/{$viewtype}/{$view}";
257  return Paths::sanitize($filename, false);
258  }
259 }
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
__construct(Config $config, ViewsService $views)
Constructor.
Definition: SimpleCache.php:33
cachedAssetExists(int $cache_time, string $viewtype, string $view)
Check if a asset exists in the cache.
clear()
Deletes all cached views in the simplecache.
purge()
Purge old/stale cache content.
isEnabled()
Is simple cache enabled.
disable()
Disables the simple cache.
getRoot()
Get the base url for simple cache requests.
Definition: SimpleCache.php:90
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
static canonicalizeViewName(string $alias)
Takes a view name and returns the canonical name for that view.
getCachedAssetLocation(int $cache_time, string $viewtype, string $view)
Get the cache location of an existing cached asset.
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
getUrl(string $view, string $subview= '')
Get the URL for the cached view.
Definition: SimpleCache.php:65
if(!empty($title)&&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
Definition: message.php:73