Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / lib / cache_lite / Lite / Function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (7,04 ko)

1
<?php
2

    
3
/**
4
* This class extends Cache_Lite and can be used to cache the result and output of functions/methods
5
*
6
* This class is completly inspired from Sebastian Bergmann's
7
* PEAR/Cache_Function class. This is only an adaptation to
8
* Cache_Lite
9
*
10
* There are some examples in the 'docs/examples' file
11
* Technical choices are described in the 'docs/technical' file
12
*
13
* @package Cache_Lite
14
* @version $Id: Function.php,v 1.11 2006/12/14 12:59:43 cweiske Exp $
15
* @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
16
* @author Fabien MARTY <fab@php.net>
17
*/
18

    
19
require_once('Cache/Lite.php');
20

    
21
class Cache_Lite_Function extends Cache_Lite
22
{
23

    
24
    // --- Private properties ---
25

    
26
    /**
27
     * Default cache group for function caching
28
     *
29
     * @var string $_defaultGroup
30
     */
31
    var $_defaultGroup = 'Cache_Lite_Function';
32

    
33
    /**
34
     * Don't cache the method call when its output contains the string "NOCACHE"
35
     *
36
     * if set to true, the output of the method will never be displayed (because the output is used
37
     * to control the cache)
38
     *
39
     * @var boolean $_dontCacheWhenTheOutputContainsNOCACHE
40
     */
41
    var $_dontCacheWhenTheOutputContainsNOCACHE = false;
42

    
43
    /**
44
     * Don't cache the method call when its result is false
45
     *
46
     * @var boolean $_dontCacheWhenTheResultIsFalse
47
     */
48
    var $_dontCacheWhenTheResultIsFalse = false;
49

    
50
    /**
51
     * Don't cache the method call when its result is null
52
     *
53
     * @var boolean $_dontCacheWhenTheResultIsNull
54
     */
55
    var $_dontCacheWhenTheResultIsNull = false;
56

    
57
    /**
58
     * Debug the Cache_Lite_Function caching process
59
     *
60
     * @var boolean $_debugCacheLiteFunction
61
     */
62
    var $_debugCacheLiteFunction = false;
63

    
64
    // --- Public methods ----
65

    
66
    /**
67
    * Constructor
68
    *
69
    * $options is an assoc. To have a look at availables options,
70
    * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
71
    *
72
    * Comparing to Cache_Lite constructor, there is another option :
73
    * $options = array(
74
    *     (...) see Cache_Lite constructor
75
    *     'debugCacheLiteFunction' => (bool) debug the caching process,
76
    *     'defaultGroup' => default cache group for function caching (string),
77
    *     'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
78
    *     'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
79
    *     'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
80
    * );
81
    *
82
    * @param array $options options
83
    * @access public
84
    */
85
    function Cache_Lite_Function($options = array(NULL))
86
    {
87
        $availableOptions = array('debugCacheLiteFunction', 'defaultGroup', 'dontCacheWhenTheOutputContainsNOCACHE', 'dontCacheWhenTheResultIsFalse', 'dontCacheWhenTheResultIsNull');
88
        while (list($name, $value) = each($options)) {
89
            if (in_array($name, $availableOptions)) {
90
                $property = '_'.$name;
91
                $this->$property = $value;
92
            }
93
        }
94
        reset($options);
95
        $this->Cache_Lite($options);
96
    }
97

    
98
    /**
99
    * Calls a cacheable function or method (or not if there is already a cache for it)
100
    *
101
    * Arguments of this method are read with func_get_args. So it doesn't appear
102
    * in the function definition. Synopsis :
103
    * call('functionName', $arg1, $arg2, ...)
104
    * (arg1, arg2... are arguments of 'functionName')
105
    *
106
    * @return mixed result of the function/method
107
    * @access public
108
    */
109
    function call()
110
    {
111
        $arguments = func_get_args();
112
        $id = $this->_makeId($arguments);
113
        $data = $this->get($id, $this->_defaultGroup);
114
        if ($data !== false) {
115
            if ($this->_debugCacheLiteFunction) {
116
                echo "Cache hit !\n";
117
            }
118
            $array = unserialize($data);
119
            $output = $array['output'];
120
            $result = $array['result'];
121
        } else {
122
            if ($this->_debugCacheLiteFunction) {
123
                echo "Cache missed !\n";
124
            }
125
            ob_start();
126
            ob_implicit_flush(false);
127
            $target = array_shift($arguments);
128
            if (is_array($target)) {
129
                // in this case, $target is for example array($obj, 'method')
130
                $object = $target[0];
131
                $method = $target[1];
132
                $result = call_user_func_array(array(&$object, $method), $arguments);
133
            } else {
134
                if (strstr($target, '::')) { // classname::staticMethod
135
                    list($class, $method) = explode('::', $target);
136
                    $result = call_user_func_array(array($class, $method), $arguments);
137
                } else if (strstr($target, '->')) { // object->method
138
                    // use a stupid name ($objet_123456789 because) of problems where the object
139
                    // name is the same as this var name
140
                    list($object_123456789, $method) = explode('->', $target);
141
                    global $$object_123456789;
142
                    $result = call_user_func_array(array($$object_123456789, $method), $arguments);
143
                } else { // function
144
                    $result = call_user_func_array($target, $arguments);
145
                }
146
            }
147
            $output = ob_get_contents();
148
            ob_end_clean();
149
            if ($this->_dontCacheWhenTheResultIsFalse) {
150
                if ((is_bool($result)) && (!($result))) {
151
                    echo($output);
152
                    return $result;
153
                }
154
            }
155
            if ($this->_dontCacheWhenTheResultIsNull) {
156
                if (is_null($result)) {
157
                    echo($output);
158
                    return $result;
159
                }
160
            }
161
            if ($this->_dontCacheWhenTheOutputContainsNOCACHE) {
162
                if (strpos($output, 'NOCACHE') > -1) {
163
                    return $result;
164
                }
165
            }
166
            $array['output'] = $output;
167
            $array['result'] = $result;
168
            $this->save(serialize($array), $id, $this->_defaultGroup);
169
        }
170
        echo($output);
171
        return $result;
172
    }
173

    
174
    /**
175
    * Drop a cache file
176
    *
177
    * Arguments of this method are read with func_get_args. So it doesn't appear
178
    * in the function definition. Synopsis :
179
    * remove('functionName', $arg1, $arg2, ...)
180
    * (arg1, arg2... are arguments of 'functionName')
181
    *
182
    * @return boolean true if no problem
183
    * @access public
184
    */
185
    function drop()
186
    {
187
        $id = $this->_makeId(func_get_args());
188
        return $this->remove($id, $this->_defaultGroup);
189
    }
190

    
191
    /**
192
    * Make an id for the cache
193
    *
194
    * @var array result of func_get_args for the call() or the remove() method
195
    * @return string id
196
    * @access private
197
    */
198
    function _makeId($arguments)
199
    {
200
        $id = serialize($arguments); // Generate a cache id
201
        if (!$this->_fileNameProtection) {
202
            $id = md5($id);
203
            // if fileNameProtection is set to false, then the id has to be hashed
204
            // because it's a very bad file name in most cases
205
        }
206
        return $id;
207
    }
208

    
209
}
210

    
211
?>
Redmine Appliance - Powered by TurnKey Linux