Elgg  Version 3.0
SimpleCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
5 use Elgg\Config;
7 
14 class SimpleCache {
15 
19  protected $config;
20 
24  protected $views;
25 
32  public function __construct(
35  ) {
36  $this->config = $config;
37  $this->views = $views;
38  }
39 
64  public function getUrl($view, $subview = '') {
65  // handle `getUrl('js', 'js/blog/save_draft')`
66  if (($view === 'js' || $view === 'css') && 0 === strpos($subview, $view . '/')) {
67  $view = $subview;
68  $subview = '';
69  }
70 
71  // handle `getUrl('js', 'blog/save_draft')`
72  if (!empty($subview)) {
73  $view = "$view/$subview";
74  }
75 
77 
78  // should be normalized to canonical form by now: `getUrl('blog/save_draft.js')`
79  $this->views->registerCacheableView($view);
80 
81  return $this->getRoot() . $view;
82  }
83 
89  public function getRoot() {
91  if ($this->isEnabled()) {
92  $lastcache = (int) $this->config->lastcache;
93  } else {
94  $lastcache = 0;
95  }
96 
97  return elgg_normalize_url("/cache/$lastcache/$viewtype/");
98  }
99 
105  public function isEnabled() {
106  return (bool) $this->config->simplecache_enabled;
107  }
108 
115  public function enable() {
116  $this->config->save('simplecache_enabled', 1);
117  $this->invalidate();
118  }
119 
128  public function disable() {
129  if ($this->config->simplecache_enabled) {
130  $this->config->save('simplecache_enabled', 0);
131 
132  $this->invalidate();
133  }
134  }
135 
141  private function getPath() {
142  return $this->config->assetroot;
143  }
144 
151  public function invalidate() {
152  _elgg_rmdir($this->getPath(), true);
153 
154  $time = time();
155  $this->config->save("simplecache_lastupdate", $time);
156  $this->config->lastcache = $time;
157 
158  return true;
159  }
160 }
__construct(Config $config, ViewsService $views)
Constructor.
Definition: SimpleCache.php:32
elgg_normalize_url($url)
Definition: output.php:186
static canonicalizeViewName($alias)
Takes a view name and returns the canonical name for that view.
isEnabled()
Is simple cache enabled.
disable()
Disables the simple cache.
getRoot()
Get the base url for simple cache requests.
Definition: SimpleCache.php:89
elgg_get_viewtype()
Return the current view type.
Definition: views.php:78
invalidate()
Deletes all cached views in the simplecache and sets the lastcache and lastupdate time to 0 for every...
getUrl($view, $subview= '')
Get the URL for the cached view.
Definition: SimpleCache.php:64
enable()
Enables the simple cache.
if(!$owner||!$owner->canEdit()) if(!$owner->hasIcon('master')) if(!$owner->saveIconFromElggFile($owner->getIcon('master'), 'icon', $coords)) $view
Definition: crop.php:30
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
WARNING: API IN FLUX.
$viewtype
Definition: default.php:11
_elgg_rmdir($dir, $empty=false)
Recursively deletes a directory, including all hidden files.
Definition: cache.php:190
WARNING: API IN FLUX.
Definition: SimpleCache.php:14