Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / acm / bootstrap.php
blobea671706e5acdbd8b1c308be72a34f2033ecdbdb
1 <?php
2 /**
4 * @package acm
5 * @version $Id: acm_file.php 9233 2008-12-27 12:18:04Z acydburn $
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Base cache class.
22 * A prefix of # for $var_name indicates global data.
24 * @method mixed get($var_name) Get cached data.
25 * @method mixed put($var_name, $data, $ttl = 31536000) Put data into cache.
26 * @method mixed destroy($var_name) Destroy cached data.
27 * @method mixed exists($var_name) Check if cached data exists.
29 * @package acm
31 class phpbb_acm
33 /**
34 * @var array required phpBB objects
36 public $phpbb_required = array();
38 /**
39 * @var array Optional phpBB objects
41 public $phpbb_optional = array();
43 /**
44 * @var array Currently registered core acm types.
46 public $cache_types = array('data' => NULL, 'sql' => NULL);
48 /**
49 * Constructor
50 * @access public
52 public function __construct() { }
54 /**
55 * Magic method for calling type-specific functions.
56 * Functions directly supported are: get(), put(), exists(), destroy()
58 * The type is added to the methods name, for getting sql data just use get_sql() for example.
60 * see {@link phpbb_acm_abstract phpbb_acm_abstract} for more information
62 * @access public
64 public function __call($method, $arguments)
66 $supported_internal_functions = array('get', 'put', 'exists', 'destroy');
67 $internal_method = explode('_', $method, 2);
69 // Get cache type and method
70 if (in_array($internal_method[0], $supported_internal_functions))
72 $cache_type = (empty($internal_method[1])) ? 'data' : $internal_method[1];
73 $method = $internal_method[0];
75 else
77 $cache_type = $arguments[0];
78 array_shift($arguments);
81 // Check if the cache type is initialized and exist
82 if (!$this->type_exists($cache_type))
84 return false;
87 // $this->cache_types[$cache_type]->$method($arguments);
88 return call_user_func_array(array($this->cache_types[$cache_type], $method), $arguments);
91 /**
92 * Tidy cache. This removes all expired cache data.
93 * @access public
95 public function tidy()
97 foreach ($this->cache_types as $cache_type => $object)
99 if ($object === NULL)
101 continue;
104 $this->cache_types[$cache_type]->tidy();
109 * Purge cache. This removes all cache data, not only the expired one.
110 * @access public
112 public function purge()
114 foreach ($this->cache_types as $cache_type => $object)
116 if ($object === NULL)
118 continue;
121 $this->cache_types[$cache_type]->purge();
126 * Load cache data. This is usually only used internally.
127 * @access public
129 public function load()
131 foreach ($this->cache_types as $cache_type => $object)
133 if ($object === NULL)
135 continue;
138 $this->cache_types[$cache_type]->load();
143 * Unload everything from cache and make sure non-stored cache items are properly saved.
144 * @access public
146 public function unload()
148 foreach ($this->cache_types as $cache_type => $object)
150 if ($object === NULL)
152 continue;
155 $this->cache_types[$cache_type]->unload();
160 * Register a custom cache type/class.
162 * @param string $cache_type The cache type to register/set
163 * @param string $cache_append String to append to the cached data as identifier (if the coder has different types to distinct from)
164 * @param string $cache_object The exact name of the cache class to load.
165 * The filename must be: <code>includes/acm/acm_{$cache_object}.php</code>
166 * The class definition must be: <code>class phpbb_acm_{$cache_object} extends phpbb_acm_abstract</code>
167 * Additionally it is possible to define classes for every cache type...
168 * for example: <code>phpbb_acm_{$cache_object}_{$cache_type} extends phpbb_acm_{$cache_object}</code>
170 * @return bool Returns true on success, else false.
171 * @access public
173 public function register($cache_type, $cache_append = false, $cache_object = false)
175 $cache_object = ($cache_object === false) ? basename(phpbb::$base_config['acm_type']) : basename($cache_object);
177 // We need to init every cache type...
178 if (!isset($this->cache_types[$cache_type]))
180 $this->cache_types[$cache_type] = NULL;
183 // Unregister if already registered
184 if ($this->cache_types[$cache_type] !== NULL)
186 $this->cache_types[$cache_type] = NULL;
189 if ($this->cache_types[$cache_type] === NULL)
191 $class_name = 'phpbb_acm_' . $cache_object;
193 if (!class_exists($class_name))
195 if (!file_exists(PHPBB_ROOT_PATH . 'includes/acm/acm_' . $cache_object . '.' . PHP_EXT))
197 return false;
200 require_once PHPBB_ROOT_PATH . 'includes/acm/acm_' . $cache_object . '.' . PHP_EXT;
203 $class_name = (class_exists('phpbb_acm_' . $cache_object . '_' . $cache_type)) ? 'phpbb_acm_' . $cache_object . '_' . $cache_type : 'phpbb_acm_' . $cache_object;
205 // Set cache prefix, for example ctpl_prosilver
206 $cache_prefix = ($cache_append === false) ? $cache_type : $cache_type . '_' . $cache_append;
208 $this->cache_types[$cache_type] = new $class_name($cache_prefix);
210 if (!$this->supported($cache_type))
212 $this->cache_types[$cache_type] = NULL;
213 return false;
217 return true;
221 * Check if a specified cache type is supported with the ACM class
223 * @param string $cache_type The cache type to check.
225 * @return bool True if the type is supported, else false.
226 * @access public
228 public function supported($cache_type)
230 if (!$this->type_exists($cache_type))
232 return false;
235 return !empty($this->cache_types[$cache_type]->supported[$cache_type]) || $this->cache_types[$cache_type]->supported === true;
239 * Check if the cache type exists. Sometimes some types do not exist if the relevant files are not there or do not support the given cache type.
241 * @param string $cache_type The cache type to check.
243 * @return bool True if the type exist, else false.
244 * @access private
246 private function type_exists($cache_type)
248 if (!isset($this->cache_types[$cache_type]) || $this->cache_types[$cache_type] === NULL)
250 $this->register($cache_type);
253 return $this->cache_types[$cache_type] !== NULL;
259 * The abstract class all ACM plugins must extend.
260 * @package acm
262 abstract class phpbb_acm_abstract
265 * @var string The current cache prefix
267 public $cache_prefix = '';
270 * @var array Cached global data
272 protected $vars = array();
275 * @var array Expire information for cached global data
277 protected $var_expires = array();
280 * @var bool Is true if global data is modified
282 protected $is_modified = false;
285 * Get cached data
287 * @param string $var_name Variable name. Global variable name is prefixed with #.
289 * @return mixed Returns false if there is no data available, else returns the data
290 * @access public
292 abstract public function get($var_name);
295 * Put data into cache
297 * @param string $var_name Variable name. Global variable name is prefixed with #.
298 * @param mixed $data Data to be put into cache.
299 * @param int $ttl Cache lifetime in seconds.
301 * @return mixed Returns $data
302 * @access public
304 abstract public function put($var_name, $data, $ttl = 31536000);
307 * Destroy cached data.
309 * @param string $var_name Variable name. Global variable name is prefixed with #.
311 * @return mixed Returns false if the cached data does not exist
312 * @access public
314 abstract public function destroy($var_name);
317 * Check if cached data exists.
319 * @param string $var_name Variable name. Global variable name is prefixed with #.
321 * @return bool True if it exists
322 * @access public
324 abstract public function exists($var_name);
327 * Load cache data. This is usually only used internally.
328 * @access public
330 abstract public function load();
333 * Unload everything from cache and make sure non-stored cache items are properly saved.
334 * @access public
336 abstract public function unload();
339 * Tidy cache. This removes all expired cache data.
340 * @access public
342 public function tidy()
344 $this->tidy_local();
345 $this->tidy_global();
347 set_config('cache_last_gc', time(), true);
351 * Purge cache. This removes all cache data, not only the expired one.
352 * @access public
354 public function purge()
356 $this->purge_local();
357 $this->purge_global();
361 * Tidy only local cache data
362 * @access protected
364 abstract protected function tidy_local();
367 * Purge only local cache data
368 * @access protected
370 abstract protected function purge_local();
373 * Get global cache data. See {@link get() get()}.
374 * @access protected
376 protected function get_global($var_name)
378 // Check if we have all variables
379 if (!sizeof($this->vars))
381 $this->load();
384 if (!isset($this->var_expires[$var_name]))
386 return false;
389 // If expired... we remove this entry now...
390 if (time() > $this->var_expires[$var_name])
392 $this->destroy('#' . $var_name);
393 return false;
396 if (isset($this->vars[$var_name]))
398 return $this->vars[$var_name];
401 return false;
405 * Put data into global cache. See {@link put() put()}.
406 * @access protected
408 protected function put_global($var_name, $data, $ttl = 31536000)
410 $this->vars[$var_name] = $data;
411 $this->var_expires[$var_name] = time() + $ttl;
412 $this->is_modified = true;
414 return $data;
418 * Check if global data exists. See {@link exists() exists()}.
419 * @access protected
421 protected function exists_global($var_name)
423 return !empty($this->vars[$var_name]) && time() <= $this->var_expires[$var_name];
427 * Destroy global cache data. See {@link destroy() destroy()}.
428 * @access protected
430 protected function destroy_global($var_name)
432 $this->is_modified = true;
434 unset($this->vars[$var_name]);
435 unset($this->var_expires[$var_name]);
437 // We save here to let the following cache hits succeed
438 $this->unload();
442 * Tidy global cache data. See {@link tidy() tidy()}.
443 * @access protected
445 protected function tidy_global()
447 // Now tidy global settings
448 if (!sizeof($this->vars))
450 $this->load();
453 foreach ($this->var_expires as $var_name => $expires)
455 if (time() > $expires)
457 // We only unset, then save later
458 unset($this->vars[$var_name]);
459 unset($this->var_expires[$var_name]);
463 $this->is_modified = true;
464 $this->unload();
468 * Purge global cache data. See {@link purge() purge()}.
469 * @access protected
471 protected function purge_global()
473 // Now purge global settings
474 unset($this->vars);
475 unset($this->var_expires);
477 $this->vars = array();
478 $this->var_expires = array();
480 $this->is_modified = true;
481 $this->unload();