Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: cache.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Short description for file.
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.libs.view.helpers
21
 * @since         CakePHP(tm) v 1.0.0.2277
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
/**
28
 * Short description for file.
29
 *
30
 * Long description for file
31
 *
32
 * @package       cake
33
 * @subpackage    cake.cake.libs.view.helpers
34
 */
35
class CacheHelper extends AppHelper {
36
/**
37
 * Array of strings replaced in cached views.
38
 * The strings are found between <cake:nocache><cake:nocache> in views
39
 *
40
 * @var array
41
 * @access private
42
 */
43
	var $__replace = array();
44
/**
45
 * Array of string that are replace with there var replace above.
46
 * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
47
 *
48
 * @var array
49
 * @access private
50
 */
51
	var $__match = array();
52
/**
53
 * holds the View object passed in final call to CacheHelper::cache()
54
 *
55
 * @var object
56
 * @access public
57
 */
58
	var $view;
59
/**
60
 * cache action time
61
 *
62
 * @var object
63
 * @access public
64
 */
65
	var $cacheAction;
66
/**
67
 * Main method used to cache a view
68
 *
69
 * @param string $file File to cache
70
 * @param string $out output to cache
71
 * @param boolean $cache
72
 * @return view ouput
73
 */
74
	function cache($file, $out, $cache = false) {
75
		$cacheTime = 0;
76
		$useCallbacks = false;
77
		if (is_array($this->cacheAction)) {
78
			$contoller = Inflector::underscore($this->controllerName);
79
			$check = str_replace('/', '_', $this->here);
80
			$replace = str_replace('/', '_', $this->base);
81
			$match = str_replace($this->base, '', $this->here);
82
			$match = str_replace('//', '/', $match);
83
			$match = str_replace('/' . $contoller . '/', '', $match);
84
			$match = str_replace('/' . $this->controllerName . '/', '', $match);
85
			$check = str_replace($replace, '', $check);
86
			$check = str_replace('_' . $contoller . '_', '', $check);
87
			$check = str_replace('_' . $this->controllerName . '_', '', $check);
88
			$check = Inflector::slug($check);
89
			$check = preg_replace('/^_+/', '', $check);
90
			$keys = str_replace('/', '_', array_keys($this->cacheAction));
91
			$found = array_keys($this->cacheAction);
92
			$index = null;
93
			$count = 0;
94
 
95
			foreach ($keys as $key => $value) {
96
				if (strpos($check, $value) === 0) {
97
					$index = $found[$count];
98
					break;
99
				}
100
				$count++;
101
			}
102
 
103
			if (isset($index)) {
104
				$pos1 = strrpos($match, '/');
105
				$char = strlen($match) - 1;
106
 
107
				if ($pos1 == $char) {
108
					$match = substr($match, 0, $char);
109
				}
110
 
111
				$key = $match;
112
			} elseif ($this->action == 'index') {
113
				$index = 'index';
114
			}
115
 
116
			$options = $this->cacheAction;
117
			if (isset($this->cacheAction[$index])) {
118
				if (is_array($this->cacheAction[$index])) {
119
					$options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
120
				} else {
121
					$cacheTime = $this->cacheAction[$index];
122
				}
123
			}
124
 
125
			if (array_key_exists('duration', $options)) {
126
				$cacheTime = $options['duration'];
127
			}
128
			if (array_key_exists('callbacks', $options)) {
129
				$useCallbacks = $options['callbacks'];
130
			}
131
 
132
		} else {
133
			$cacheTime = $this->cacheAction;
134
		}
135
 
136
		if ($cacheTime != '' && $cacheTime > 0) {
137
			$this->__parseFile($file, $out);
138
			if ($cache === true) {
139
				$cached = $this->__parseOutput($out);
140
				$this->__writeFile($cached, $cacheTime, $useCallbacks);
141
			}
142
			return $out;
143
		} else {
144
			return $out;
145
		}
146
	}
147
/**
148
 * Parse file searching for no cache tags
149
 *
150
 * @param string $file
151
 * @param boolean $cache
152
 * @access private
153
 */
154
	function __parseFile($file, $cache) {
155
		if (is_file($file)) {
156
			$file = file_get_contents($file);
157
		} elseif ($file = fileExistsInPath($file)) {
158
			$file = file_get_contents($file);
159
		}
160
 
161
		preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
162
		preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER);
163
 
164
		if (!empty($this->__replace)) {
165
			foreach ($oresult['0'] as $k => $element) {
166
				$index = array_search($element, $this->__match);
167
				if ($index !== false) {
168
					array_splice($oresult[0], $k, 1);
169
				}
170
			}
171
		}
172
 
173
		if (!empty($result['0'])) {
174
			$count = 0;
175
			foreach ($result['0'] as $block) {
176
				if (isset($oresult['0'][$count])) {
177
					$this->__replace[] = $block;
178
					$this->__match[] = $oresult['0'][$count];
179
				}
180
				$count++;
181
			}
182
		}
183
	}
184
/**
185
 * Parse the output and replace cache tags
186
 *
187
 * @param sting $cache
188
 * @return string with all replacements made to <cake:nocache><cake:nocache>
189
 * @access private
190
 */
191
	function __parseOutput($cache) {
192
		$count = 0;
193
		if (!empty($this->__match)) {
194
 
195
			foreach ($this->__match as $found) {
196
				$original = $cache;
197
				$length = strlen($found);
198
				$position = 0;
199
 
200
					for ($i = 1; $i <= 1; $i++) {
201
						$position = strpos($cache, $found, $position);
202
 
203
						if ($position !== false) {
204
							$cache = substr($original, 0, $position);
205
							$cache .= $this->__replace[$count];
206
							$cache .= substr($original, $position + $length);
207
						} else {
208
							break;
209
						}
210
					}
211
					$count++;
212
			}
213
			return $cache;
214
		}
215
		return $cache;
216
	}
217
/**
218
 * Write a cached version of the file
219
 *
220
 * @param string $file
221
 * @param sting $timestamp
222
 * @return cached view
223
 * @access private
224
 */
225
	function __writeFile($content, $timestamp, $useCallbacks = false) {
226
		$now = time();
227
 
228
		if (is_numeric($timestamp)) {
229
			$cacheTime = $now + $timestamp;
230
		} else {
231
			$cacheTime = strtotime($timestamp, $now);
232
		}
233
		$path = $this->here;
234
		if ($this->here == '/') {
235
			$path = 'home';
236
		}
237
		$cache = strtolower(Inflector::slug($path));
238
 
239
		if (empty($cache)) {
240
			return;
241
		}
242
		$cache = $cache . '.php';
243
		$file = '<!--cachetime:' . $cacheTime . '--><?php';
244
 
245
		if (empty($this->plugin)) {
246
			$file .= '
247
			App::import(\'Controller\', \'' . $this->controllerName. '\');
248
			';
249
		} else {
250
			$file .= '
251
			App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
252
			';
253
		}
254
 
255
		$file .= '$controller =& new ' . $this->controllerName . 'Controller();
256
				$controller->plugin = $this->plugin = \''.$this->plugin.'\';
257
				$controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
258
				$controller->base = $this->base = \'' . $this->base . '\';
259
				$controller->layout = $this->layout = \'' . $this->layout. '\';
260
				$controller->webroot = $this->webroot = \'' . $this->webroot . '\';
261
				$controller->here = $this->here = \'' . $this->here . '\';
262
				$controller->namedArgs  = $this->namedArgs  = \'' . $this->namedArgs . '\';
263
				$controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
264
				$controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
265
				$controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
266
				$controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
267
				$controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';';
268
 
269
		if ($useCallbacks == true) {
270
			$file .= '
271
				$controller->constructClasses();
272
				$controller->Component->initialize($controller);
273
				$controller->beforeFilter();
274
				$controller->Component->startup($controller);';
275
		}
276
 
277
		$file .= '
278
				Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));
279
				$loadedHelpers = array();
280
				$loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
281
				foreach (array_keys($loadedHelpers) as $helper) {
282
					$camelBackedHelper = Inflector::variable($helper);
283
					${$camelBackedHelper} =& $loadedHelpers[$helper];
284
					$this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
285
				}
286
		?>';
287
		$content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
288
		$file .= $content;
289
		return cache('views' . DS . $cache, $file, $timestamp);
290
	}
291
}
292
 
293
?>