Elgg  Version 2.3
SimpleCache.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Cache;
3 
6 use Elgg\ViewsService as Views;
7 
8 
16 class SimpleCache {
17 
19  private $config;
20 
22  private $datalist;
23 
25  private $views;
26 
34  public function __construct(Config $config, Datalist $datalist, Views $views) {
35  $this->config = $config;
36  $this->datalist = $datalist;
37  $this->views = $views;
38  }
39 
57  function registerView($view_name) {
58  $view_name = $this->views->canonicalizeViewName($view_name);
59  elgg_register_external_view($view_name, true);
60  }
61 
86  function getUrl($view, $subview = '') {
87  // handle `getUrl('js', 'js/blog/save_draft')`
88  if (($view === 'js' || $view === 'css') && 0 === strpos($subview, $view . '/')) {
89  $view = $subview;
90  $subview = '';
91  }
92 
93  // handle `getUrl('js', 'blog/save_draft')`
94  if (!empty($subview)) {
95  $view = "$view/$subview";
96  }
97 
98  $view = $this->views->canonicalizeViewName($view);
99 
100  // should be normalized to canonical form by now: `getUrl('blog/save_draft.js')`
101  $this->registerView($view);
102  return $this->getRoot() . $view;
103  }
104 
111  function getRoot() {
112  $viewtype = elgg_get_viewtype();
113  if ($this->isEnabled()) {
114  // stored in datalist as 'simplecache_lastupdate'
115  $lastcache = (int)$this->config->get('lastcache');
116  } else {
117  $lastcache = 0;
118  }
119 
120  return elgg_normalize_url("/cache/$lastcache/$viewtype/");
121  }
122 
128  function isEnabled() {
129  return (bool) $this->config->get('simplecache_enabled');
130  }
131 
138  function enable() {
139  $this->datalist->set('simplecache_enabled', 1);
140  $this->config->set('simplecache_enabled', 1);
141  $this->invalidate();
142  }
143 
152  function disable() {
153  if ($this->config->get('simplecache_enabled')) {
154  $this->datalist->set('simplecache_enabled', 0);
155  $this->config->set('simplecache_enabled', 0);
156 
157  $this->invalidate();
158  }
159  }
160 
166  private function getPath() {
167  $realpath = realpath($this->config->getCachePath());
168  return rtrim($realpath, DIRECTORY_SEPARATOR) . "/views_simplecache";
169  }
170 
177  function invalidate() {
178  _elgg_rmdir($this->getPath(), true);
179 
180  $time = time();
181  $this->datalist->set("simplecache_lastupdate", $time);
182  $this->config->set('lastcache', $time);
183 
184  return true;
185  }
186 
192  function init() {
193  $lastcache = $this->config->get('lastcache');
194  if (!defined('UPGRADING') && empty($lastcache)) {
195  $this->config->set('lastcache', (int)$this->datalist->get('simplecache_lastupdate'));
196  }
197  }
198 }
$view
Definition: crop.php:34
elgg_normalize_url($url)
Definition: output.php:280
__construct(Config $config, Datalist $datalist, Views $views)
Constructor.
Definition: SimpleCache.php:34
Access to configuration values.
Definition: Config.php:11
isEnabled()
Is simple cache enabled.
disable()
Disables the simple cache.
init()
Set up config appropriately on engine boot.
getRoot()
Get the base url for simple cache requests.
elgg_get_viewtype()
Return the current view type.
Definition: views.php:95
elgg_register_external_view($view, $cacheable=false)
Registers a view as being available externally (i.e.
Definition: views.php:244
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:86
enable()
Enables the simple cache.
registerView($view_name)
Registers a view to simple cache.
Definition: SimpleCache.php:57
WARNING: API IN FLUX.
_elgg_rmdir($dir, $empty=false)
Recursively deletes a directory, including all hidden files.
Definition: cache.php:185
elgg subtext time
WARNING: API IN FLUX.
Definition: SimpleCache.php:16
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5