Source code for file /pear/cache/lite/Function.php
Documentation is available at Function.php
* This class extends Cache_Lite and can be used to cache the result and output of functions/methods
* This class is completly inspired from Sebastian Bergmann's
* PEAR/Cache_Function class. This is only an adaptation to
* There are some examples in the 'docs/examples' file
* Technical choices are described in the 'docs/technical' file
* @version $Id: Function.php,v 1.10 2006/02/04 18:36:36 fab Exp $
// Check to ensure this file is within the rest of the framework
//require_once('Cache/Lite.php');
// --- Private properties ---
* Default cache group for function caching
* @var string $_defaultGroup
* Don't cache the method call when its output contains the string "NOCACHE"
* if set to true, the output of the method will never be displayed (because the output is used
* @var boolean $_dontCacheWhenTheOutputContainsNOCACHE
* Don't cache the method call when its result is false
* @var boolean $_dontCacheWhenTheResultIsFalse
* Don't cache the method call when its result is null
* @var boolean $_dontCacheWhenTheResultIsNull
* Debug the Cache_Lite_Function caching process
* @var boolean $_debugCacheLiteFunction
// --- Public methods ----
* $options is an assoc. To have a look at availables options,
* see the constructor of the Cache_Lite class in 'Cache_Lite.php'
* Comparing to Cache_Lite constructor, there is another option :
* (...) see Cache_Lite constructor
* 'debugCacheLiteFunction' => (bool) debug the caching process,
* 'defaultGroup' => default cache group for function caching (string),
* 'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
* 'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
* 'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
* @param array $options options
$availableOptions =
array('debugCacheLiteFunction', 'defaultGroup', 'dontCacheWhenTheOutputContainsNOCACHE', 'dontCacheWhenTheResultIsFalse', 'dontCacheWhenTheResultIsNull');
while (list
($name, $value) =
each($options)) {
if (in_array($name, $availableOptions)) {
$this->$property =
$value;
* Calls a cacheable function or method (or not if there is already a cache for it)
* Arguments of this method are read with func_get_args. So it doesn't appear
* in the function definition. Synopsis :
* call('functionName', $arg1, $arg2, ...)
* (arg1, arg2... are arguments of 'functionName')
* @return mixed result of the function/method
$id =
$this->_makeId($arguments);
$output =
$array['output'];
$result =
$array['result'];
// in this case, $target is for example array($obj, 'method')
if (strstr($target, '::')) { // classname::staticMethod
list
($class, $method) =
explode('::', $target);
} else if (strstr($target, '->')) { // object->method
// use a stupid name ($objet_123456789 because) of problems where the object
// name is the same as this var name
list
($object_123456789, $method) =
explode('->', $target);
global $
$object_123456789;
if ((is_bool($result)) &&
(!($result))) {
if (strpos($output, 'NOCACHE') > -
1) {
$array['output'] =
$output;
$array['result'] =
$result;
* Arguments of this method are read with func_get_args. So it doesn't appear
* in the function definition. Synopsis :
* remove('functionName', $arg1, $arg2, ...)
* (arg1, arg2... are arguments of 'functionName')
* @return boolean true if no problem
* Make an id for the cache
* @var array result of func_get_args for the call() or the remove() method
function _makeId($arguments)
$id =
serialize($arguments); // Generate a cache id
// if fileNameProtection is set to false, then the id has to be hashed
// because it's a very bad file name in most cases