[ Index ] |
|
Code source de CakePHP 1.1.13.4450 |
1 <?php 2 /* SVN FILE: $Id: cache.php 4409 2007-02-02 13:20:59Z phpnut $ */ 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-2007, Cake Software Foundation, Inc. 12 * 1785 E. Sahara Avenue, Suite 490-204 13 * Las Vegas, Nevada 89104 14 * 15 * Licensed under The MIT License 16 * Redistributions of files must retain the above copyright notice. 17 * 18 * @filesource 19 * @copyright Copyright 2005-2007, Cake Software Foundation, Inc. 20 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 21 * @package cake 22 * @subpackage cake.cake.libs.view.helpers 23 * @since CakePHP(tm) v 1.0.0.2277 24 * @version $Revision: 4409 $ 25 * @modifiedby $LastChangedBy: phpnut $ 26 * @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $ 27 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 28 */ 29 /** 30 * Short description for file. 31 * 32 * Long description for file 33 * 34 * @package cake 35 * @subpackage cake.cake.libs.view.helpers 36 */ 37 class CacheHelper extends Helper{ 38 /** 39 * Array of strings replaced in cached views. 40 * The strings are found between <cake:nocache><cake:nocache> in views 41 * 42 * @var array 43 * @access private 44 */ 45 var $__replace = array(); 46 /** 47 * Array of string that are replace with there var replace above. 48 * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views 49 * 50 * @var array 51 * @access private 52 */ 53 var $__match = array(); 54 /** 55 * holds the View object passed in final call to CacheHelper::cache() 56 * 57 * @var object 58 * @access public 59 */ 60 var $view; 61 /** 62 * Main method used to cache a view 63 * 64 * @param string $file File to cache 65 * @param string $out output to cache 66 * @param boolean $cache 67 * @return view ouput 68 */ 69 function cache($file, $out, $cache = false) { 70 if (is_array($this->cacheAction)) { 71 $check = str_replace('/', '_', $this->here); 72 $replace = str_replace('/', '_', $this->base); 73 $match = str_replace($this->base, '', $this->here); 74 $match = str_replace('//', '/', $match); 75 $match = str_replace('/' . $this->controllerName . '/', '', $match); 76 $check = str_replace($replace, '', $check); 77 $check = str_replace('_' . $this->controllerName . '_', '', $check); 78 $check = convertSlash($check); 79 $check = preg_replace('/^_+/', '', $check); 80 $keys = str_replace('/', '_', array_keys($this->cacheAction)); 81 $found = array_keys($this->cacheAction); 82 $index = null; 83 $count = 0; 84 85 foreach($keys as $key => $value) { 86 if (strpos($check, $value) === 0) { 87 $index = $found[$count]; 88 break; 89 } 90 $count++; 91 } 92 93 if (isset($index)) { 94 $pos1 = strrpos($match, '/'); 95 $char = strlen($match) - 1; 96 97 if ($pos1 == $char) { 98 $match = substr($match, 0, $char); 99 } 100 101 $key = $match; 102 } elseif ($this->action == 'index') { 103 $index = 'index'; 104 } 105 if (isset($this->cacheAction[$index])) { 106 $cacheTime = $this->cacheAction[$index]; 107 } else { 108 $cacheTime = 0; 109 } 110 } else { 111 $cacheTime = $this->cacheAction; 112 } 113 114 if ($cacheTime != '' && $cacheTime > 0) { 115 $this->__parseFile($file, $out); 116 117 if ($cache === true) { 118 $cached = $this->__parseOutput($out); 119 $this->__writeFile($cached, $cacheTime); 120 } 121 return $out; 122 } else { 123 return $out; 124 } 125 } 126 /** 127 * Parse file searching for no cache tags 128 * 129 * @param string $file 130 * @param boolean $cache 131 * @access private 132 */ 133 function __parseFile($file, $cache) { 134 if (is_file($file)) { 135 $file = file_get_contents($file); 136 } elseif ($file = fileExistsInPath($file)) { 137 $file = file_get_contents($file); 138 } 139 140 preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER); 141 preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER); 142 143 if (!empty($result['0'])) { 144 $count = 0; 145 146 foreach($result['0'] as $result) { 147 if (isset($oresult['0'][$count])) { 148 $this->__replace[] = $result; 149 $this->__match[] = $oresult['0'][$count]; 150 } 151 $count++; 152 } 153 } 154 } 155 /** 156 * Parse the output and replace cache tags 157 * 158 * @param sting $cache 159 * @return string with all replacements made to <cake:nocache><cake:nocache> 160 * @access private 161 */ 162 function __parseOutput($cache) { 163 $count = 0; 164 if (!empty($this->__match)) { 165 166 foreach($this->__match as $found) { 167 $original = $cache; 168 $length = strlen($found); 169 $position = 0; 170 171 for ($i = 1; $i <= 1; $i++) { 172 $position = strpos($cache, $found, $position); 173 174 if($position !== false) { 175 $cache = substr($original, 0, $position); 176 $cache .= $this->__replace[$count]; 177 $cache .= substr($original, $position + $length); 178 } else { 179 break; 180 } 181 } 182 $count++; 183 } 184 return $cache; 185 } 186 return $cache; 187 } 188 /** 189 * Write a cached version of the file 190 * 191 * @param string $content 192 * @param sting $timestamp 193 * @return cached view 194 * @access private 195 */ 196 function __writeFile($content, $timestamp) { 197 $now = time(); 198 199 if (is_numeric($timestamp)) { 200 $cacheTime = $now + $timestamp; 201 } else { 202 $cacheTime = strtotime($timestamp, $now); 203 } 204 205 $cache = convertSlash($this->here); 206 if(empty($cache)){ 207 return; 208 } 209 210 $cache = $cache . '.php'; 211 $file = '<!--cachetime:' . $cacheTime . '--><?php'; 212 if(empty($this->plugin)) { 213 $file .= ' 214 loadController(\'' . $this->view->name. '\'); 215 loadModels(); 216 '; 217 } else { 218 $file .= ' 219 if (!class_exists(\'AppController\')) { 220 if (file_exists(\'' . APP . 'app_controller.php\')) { 221 require(\''. APP . 'app_controller.php\'); 222 } else { 223 require(\''.CAKE . 'app_controller.php\'); 224 } 225 } 226 loadPluginController(\''.$this->plugin.'\',\''.$this->view->name.'\'); 227 loadPluginModels(\''.$this->plugin.'\'); 228 '; 229 } 230 $file .= '$this->controller = new ' . $this->view->name . 'Controller(); 231 $this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\'); 232 $this->base = \'' . $this->view->base . '\'; 233 $this->layout = \'' . $this->view->layout. '\'; 234 $this->webroot = \'' . $this->view->webroot . '\'; 235 $this->here = \'' . $this->view->here . '\'; 236 $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->view->params)) . '\')); 237 $this->action = unserialize(\'' . serialize($this->view->action) . '\'); 238 $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->view->data)) . '\')); 239 $this->themeWeb = \'' . $this->view->themeWeb . '\'; 240 $this->plugin = \'' . $this->view->plugin . '\'; 241 $loadedHelpers = array(); 242 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); 243 foreach(array_keys($loadedHelpers) as $helper) 244 { 245 $replace = strtolower(substr($helper, 0, 1)); 246 $camelBackedHelper = preg_replace(\'/\\w/\', $replace, $helper, 1); 247 ${$camelBackedHelper} =& $loadedHelpers[$helper]; 248 249 if(isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) 250 { 251 foreach(${$camelBackedHelper}->helpers as $subHelper) 252 { 253 ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper]; 254 } 255 } 256 $this->loaded[$camelBackedHelper] = (${$camelBackedHelper}); 257 } 258 ?>' . $content; 259 return cache('views' . DS . $cache, $file, $timestamp); 260 } 261 } 262 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 19:27:47 2007 | par Balluche grâce à PHPXref 0.7 |