source: trunk/www.guidonia.net/wp/wp-content/plugins/tubepress/classes/net/php/pear/Cache/Lite/Function.class.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 7.1 KB
Line 
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
19class net_php_pear_Cache_Lite_Function extends net_php_pear_Cache_Lite
20{
21
22 // --- Private properties ---
23
24 /**
25 * Default cache group for function caching
26 *
27 * @var string $_defaultGroup
28 */
29 var $_defaultGroup = 'Cache_Lite_Function';
30
31 /**
32 * Don't cache the method call when its output contains the string "NOCACHE"
33 *
34 * if set to true, the output of the method will never be displayed (because the output is used
35 * to control the cache)
36 *
37 * @var boolean $_dontCacheWhenTheOutputContainsNOCACHE
38 */
39 var $_dontCacheWhenTheOutputContainsNOCACHE = false;
40
41 /**
42 * Don't cache the method call when its result is false
43 *
44 * @var boolean $_dontCacheWhenTheResultIsFalse
45 */
46 var $_dontCacheWhenTheResultIsFalse = false;
47
48 /**
49 * Don't cache the method call when its result is null
50 *
51 * @var boolean $_dontCacheWhenTheResultIsNull
52 */
53 var $_dontCacheWhenTheResultIsNull = false;
54
55 /**
56 * Debug the Cache_Lite_Function caching process
57 *
58 * @var boolean $_debugCacheLiteFunction
59 */
60 var $_debugCacheLiteFunction = false;
61
62 // --- Public methods ----
63
64 /**
65 * Constructor
66 *
67 * $options is an assoc. To have a look at availables options,
68 * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
69 *
70 * Comparing to Cache_Lite constructor, there is another option :
71 * $options = array(
72 * (...) see Cache_Lite constructor
73 * 'debugCacheLiteFunction' => (bool) debug the caching process,
74 * 'defaultGroup' => default cache group for function caching (string),
75 * 'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
76 * 'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
77 * 'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
78 * );
79 *
80 * @param array $options options
81 * @access public
82 */
83 function net_php_pear_Cache_Lite_Function($options = array(NULL))
84 {
85 $availableOptions = array('debugCacheLiteFunction', 'defaultGroup', 'dontCacheWhenTheOutputContainsNOCACHE', 'dontCacheWhenTheResultIsFalse', 'dontCacheWhenTheResultIsNull');
86 while (list($name, $value) = each($options)) {
87 if (in_array($name, $availableOptions)) {
88 $property = '_'.$name;
89 $this->$property = $value;
90 }
91 }
92 reset($options);
93 $this->net_php_pear_Cache_Lite($options);
94 }
95
96 /**
97 * Calls a cacheable function or method (or not if there is already a cache for it)
98 *
99 * Arguments of this method are read with func_get_args. So it doesn't appear
100 * in the function definition. Synopsis :
101 * call('functionName', $arg1, $arg2, ...)
102 * (arg1, arg2... are arguments of 'functionName')
103 *
104 * @return mixed result of the function/method
105 * @access public
106 */
107 function call()
108 {
109 $arguments = func_get_args();
110 $id = $this->_makeId($arguments);
111 $data = $this->get($id, $this->_defaultGroup);
112 if ($data !== false) {
113 if ($this->_debugCacheLiteFunction) {
114 echo "Cache hit !\n";
115 }
116 $array = unserialize($data);
117 $output = $array['output'];
118 $result = $array['result'];
119 } else {
120 if ($this->_debugCacheLiteFunction) {
121 echo "Cache missed !\n";
122 }
123 ob_start();
124 ob_implicit_flush(false);
125 $target = array_shift($arguments);
126 if (is_array($target)) {
127 // in this case, $target is for example array($obj, 'method')
128 $object = $target[0];
129 $method = $target[1];
130 $result = call_user_func_array(array(&$object, $method), $arguments);
131 } else {
132 if (strstr($target, '::')) { // classname::staticMethod
133 list($class, $method) = explode('::', $target);
134 $result = call_user_func_array(array($class, $method), $arguments);
135 } else if (strstr($target, '->')) { // object->method
136 // use a stupid name ($objet_123456789 because) of problems where the object
137 // name is the same as this var name
138 list($object_123456789, $method) = explode('->', $target);
139 global $$object_123456789;
140 $result = call_user_func_array(array($$object_123456789, $method), $arguments);
141 } else { // function
142 $result = call_user_func_array($target, $arguments);
143 }
144 }
145 $output = ob_get_contents();
146 ob_end_clean();
147 if ($this->_dontCacheWhenTheResultIsFalse) {
148 if ((is_bool($result)) && (!($result))) {
149 echo($output);
150 return $result;
151 }
152 }
153 if ($this->_dontCacheWhenTheResultIsNull) {
154 if (is_null($result)) {
155 echo($output);
156 return $result;
157 }
158 }
159 if ($this->_dontCacheWhenTheOutputContainsNOCACHE) {
160 if (strpos($output, 'NOCACHE') > -1) {
161 return $result;
162 }
163 }
164 $array['output'] = $output;
165 $array['result'] = $result;
166 $this->save(serialize($array), $id, $this->_defaultGroup);
167 }
168 echo($output);
169 return $result;
170 }
171
172 /**
173 * Drop a cache file
174 *
175 * Arguments of this method are read with func_get_args. So it doesn't appear
176 * in the function definition. Synopsis :
177 * remove('functionName', $arg1, $arg2, ...)
178 * (arg1, arg2... are arguments of 'functionName')
179 *
180 * @return boolean true if no problem
181 * @access public
182 */
183 function drop()
184 {
185 $id = $this->_makeId(func_get_args());
186 return $this->remove($id, $this->_defaultGroup);
187 }
188
189 /**
190 * Make an id for the cache
191 *
192 * @var array result of func_get_args for the call() or the remove() method
193 * @return string id
194 * @access private
195 */
196 function _makeId($arguments)
197 {
198 $id = serialize($arguments); // Generate a cache id
199 if (!$this->_fileNameProtection) {
200 $id = md5($id);
201 // if fileNameProtection is set to false, then the id has to be hashed
202 // because it's a very bad file name in most cases
203 }
204 return $id;
205 }
206
207}
208
209?>
Note: See TracBrowser for help on using the repository browser.