Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
storage.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Cache
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Abstract cache storage handler
14  *
15  * @package Joomla.Platform
16  * @subpackage Cache
17  * @since 11.1
18  */
20 {
21  /**
22  * @var string Rawname
23  * @since 11.1
24  */
25  protected $rawname;
26 
27  /**
28  * @var datetime Now
29  * @since 11.1
30  */
31  public $_now;
32 
33  /**
34  * @var integer Cache lifetime
35  * @since 11.1
36  */
37  public $_lifetime;
38 
39  /**
40  * @var boolean Locking
41  * @since 11.1
42  */
43  public $_locking;
44 
45  /**
46  * @var string Language
47  * @since 11.1
48  */
49  public $_language;
50 
51  /**
52  * @var string Application name.
53  * @since 11.1
54  */
55  public $_application;
56 
57  /**
58  * @var string Hash
59  * @since 11.1
60  */
61  public $_hash;
62 
63  /**
64  * Constructor
65  *
66  * @param array $options Optional parameters
67  *
68  * @since 11.1
69  */
70  public function __construct($options = array())
71  {
72  $config = JFactory::getConfig();
73  $this->_hash = md5($config->get('secret'));
74  $this->_application = (isset($options['application'])) ? $options['application'] : null;
75  $this->_language = (isset($options['language'])) ? $options['language'] : 'en-GB';
76  $this->_locking = (isset($options['locking'])) ? $options['locking'] : true;
77  $this->_lifetime = (isset($options['lifetime'])) ? $options['lifetime'] * 60 : $config->get('cachetime') * 60;
78  $this->_now = (isset($options['now'])) ? $options['now'] : time();
79 
80  // Set time threshold value. If the lifetime is not set, default to 60 (0 is BAD)
81  // _threshold is now available ONLY as a legacy (it's deprecated). It's no longer used in the core.
82  if (empty($this->_lifetime))
83  {
84  $this->_threshold = $this->_now - 60;
85  $this->_lifetime = 60;
86  }
87  else
88  {
89  $this->_threshold = $this->_now - $this->_lifetime;
90  }
91 
92  }
93 
94  /**
95  * Returns a cache storage handler object, only creating it
96  * if it doesn't already exist.
97  *
98  * @param string $handler The cache storage handler to instantiate
99  * @param array $options Array of handler options
100  *
101  * @return JCacheStorage A JCacheStorage instance
102  *
103  * @since 11.1
104  * @throws UnexpectedValueException
105  * @throws RuntimeException
106  */
107  public static function getInstance($handler = null, $options = array())
108  {
109  static $now = null;
110 
111  self::addIncludePath(JPATH_PLATFORM . '/joomla/cache/storage');
112 
113  if (!isset($handler))
114  {
115  $conf = JFactory::getConfig();
116  $handler = $conf->get('cache_handler');
117  if (empty($handler))
118  {
119  throw new UnexpectedValueException('Cache Storage Handler not set.');
120  }
121  }
122 
123  if (is_null($now))
124  {
125  $now = time();
126  }
127 
128  $options['now'] = $now;
129 
130  // We can't cache this since options may change...
131  $handler = strtolower(preg_replace('/[^A-Z0-9_\.-]/i', '', $handler));
132 
133  $class = 'JCacheStorage' . ucfirst($handler);
134  if (!class_exists($class))
135  {
136  // Search for the class file in the JCacheStorage include paths.
137  jimport('joomla.filesystem.path');
138  if ($path = JPath::find(self::addIncludePath(), strtolower($handler) . '.php'))
139  {
140  include_once $path;
141  }
142  else
143  {
144  throw new RuntimeException(sprintf('Unable to load Cache Storage: %s', $handler));
145  }
146  }
147 
148  return new $class($options);
149  }
150 
151  /**
152  * Get cached data by id and group
153  *
154  * @param string $id The cache data id
155  * @param string $group The cache data group
156  * @param boolean $checkTime True to verify cache time expiration threshold
157  *
158  * @return mixed Boolean false on failure or a cached data object
159  *
160  * @since 11.1
161  */
162  public function get($id, $group, $checkTime = true)
163  {
164  return false;
165  }
166 
167  /**
168  * Get all cached data
169  *
170  * @return mixed Boolean false on failure or a cached data object
171  *
172  * @since 11.1
173  * @todo Review this method. The docblock doesn't fit what it actually does.
174  */
175  public function getAll()
176  {
177  if (!class_exists('JCacheStorageHelper', false))
178  {
179  include_once JPATH_PLATFORM . '/joomla/cache/storage/helper.php';
180  }
181  return;
182  }
183 
184  /**
185  * Store the data to cache by id and group
186  *
187  * @param string $id The cache data id
188  * @param string $group The cache data group
189  * @param string $data The data to store in cache
190  *
191  * @return boolean True on success, false otherwise
192  *
193  * @since 11.1
194  */
195  public function store($id, $group, $data)
196  {
197  return true;
198  }
199 
200  /**
201  * Remove a cached data entry by id and group
202  *
203  * @param string $id The cache data id
204  * @param string $group The cache data group
205  *
206  * @return boolean True on success, false otherwise
207  *
208  * @since 11.1
209  */
210  public function remove($id, $group)
211  {
212  return true;
213  }
214 
215  /**
216  * Clean cache for a group given a mode.
217  *
218  * @param string $group The cache data group
219  * @param string $mode The mode for cleaning cache [group|notgroup]
220  * group mode : cleans all cache in the group
221  * notgroup mode : cleans all cache not in the group
222  *
223  * @return boolean True on success, false otherwise
224  *
225  * @since 11.1
226  */
227  public function clean($group, $mode = null)
228  {
229  return true;
230  }
231 
232  /**
233  * Garbage collect expired cache data
234  *
235  * @return boolean True on success, false otherwise.
236  *
237  * @since 11.1
238  */
239  public function gc()
240  {
241  return true;
242  }
243 
244  /**
245  * Test to see if the storage handler is available.
246  *
247  * @return boolean True on success, false otherwise
248  *
249  * @since 12.1.
250  */
251  public static function isSupported()
252  {
253  return true;
254  }
255 
256  /**
257  * Test to see if the storage handler is available.
258  *
259  * @return boolean True on success, false otherwise.
260  *
261  * @since 11.1
262  * @deprecated 12.3 (Platform) & 4.0 (CMS)
263  */
264  public static function test()
265  {
266  JLog::add('JCacheStorage::test() is deprecated. Use JCacheStorage::isSupported() instead.', JLog::WARNING, 'deprecated');
267 
268  return static::isSupported();
269  }
270 
271  /**
272  * Lock cached item
273  *
274  * @param string $id The cache data id
275  * @param string $group The cache data group
276  * @param integer $locktime Cached item max lock time
277  *
278  * @return boolean True on success, false otherwise.
279  *
280  * @since 11.1
281  */
282  public function lock($id, $group, $locktime)
283  {
284  return false;
285  }
286 
287  /**
288  * Unlock cached item
289  *
290  * @param string $id The cache data id
291  * @param string $group The cache data group
292  *
293  * @return boolean True on success, false otherwise.
294  *
295  * @since 11.1
296  */
297  public function unlock($id, $group = null)
298  {
299  return false;
300  }
301 
302  /**
303  * Get a cache_id string from an id/group pair
304  *
305  * @param string $id The cache data id
306  * @param string $group The cache data group
307  *
308  * @return string The cache_id string
309  *
310  * @since 11.1
311  */
312  protected function _getCacheId($id, $group)
313  {
314  $name = md5($this->_application . '-' . $id . '-' . $this->_language);
315  $this->rawname = $this->_hash . '-' . $name;
316  return $this->_hash . '-cache-' . $group . '-' . $name;
317  }
318 
319  /**
320  * Add a directory where JCacheStorage should search for handlers. You may
321  * either pass a string or an array of directories.
322  *
323  * @param string $path A path to search.
324  *
325  * @return array An array with directory elements
326  *
327  * @since 11.1
328  */
329  public static function addIncludePath($path = '')
330  {
331  static $paths;
332 
333  if (!isset($paths))
334  {
335  $paths = array();
336  }
337 
338  if (!empty($path) && !in_array($path, $paths))
339  {
340  jimport('joomla.filesystem.path');
341  array_unshift($paths, JPath::clean($path));
342  }
343 
344  return $paths;
345  }
346 }