cache.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: view_2helpers_2cache_8php-source.html 580 2008-07-01 14:45:49Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs.view.helpers
00023  * @since           CakePHP(tm) v 1.0.0.2277
00024  * @version         $Revision: 580 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Short description for file.
00031  *
00032  * Long description for file
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs.view.helpers
00036  */
00037 class CacheHelper extends AppHelper {
00038 /**
00039  * Array of strings replaced in cached views.
00040  * The strings are found between <cake:nocache><cake:nocache> in views
00041  *
00042  * @var array
00043  * @access private
00044  */
00045     var $__replace = array();
00046 /**
00047  * Array of string that are replace with there var replace above.
00048  * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
00049  *
00050  * @var array
00051  * @access private
00052  */
00053     var $__match = array();
00054 /**
00055  * holds the View object passed in final call to CacheHelper::cache()
00056  *
00057  * @var object
00058  * @access public
00059  */
00060     var $view;
00061 /**
00062  * cache action time
00063  *
00064  * @var object
00065  * @access public
00066  */
00067     var $cacheAction;
00068 /**
00069  * Main method used to cache a view
00070  *
00071  * @param string $file File to cache
00072  * @param string $out output to cache
00073  * @param boolean $cache
00074  * @return view ouput
00075  */
00076     function cache($file, $out, $cache = false) {
00077         $cacheTime = 0;
00078         $useCallbacks = false;
00079         if (is_array($this->cacheAction)) {
00080             $check = str_replace('/', '_', $this->here);
00081             $replace = str_replace('/', '_', $this->base);
00082             $match = str_replace($this->base, '', $this->here);
00083             $match = str_replace('//', '/', $match);
00084             $match = str_replace('/' . $this->controllerName . '/', '', $match);
00085             $check = str_replace($replace, '', $check);
00086             $check = str_replace('_' . $this->controllerName . '_', '', $check);
00087             $check = Inflector::slug($check);
00088             $check = preg_replace('/^_+/', '', $check);
00089             $keys = str_replace('/', '_', array_keys($this->cacheAction));
00090             $found = array_keys($this->cacheAction);
00091             $index = null;
00092             $count = 0;
00093 
00094             foreach ($keys as $key => $value) {
00095                 if (strpos($check, $value) === 0) {
00096                     $index = $found[$count];
00097                     break;
00098                 }
00099                 $count++;
00100             }
00101 
00102             if (isset($index)) {
00103                 $pos1 = strrpos($match, '/');
00104                 $char = strlen($match) - 1;
00105 
00106                 if ($pos1 == $char) {
00107                     $match = substr($match, 0, $char);
00108                 }
00109 
00110                 $key = $match;
00111             } elseif ($this->action == 'index') {
00112                 $index = 'index';
00113             }
00114 
00115             $options = $this->cacheAction;
00116             if (isset($this->cacheAction[$index])) {
00117                 if (is_array($this->cacheAction[$index])) {
00118                     $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
00119                 } else {
00120                     $cacheTime = $this->cacheAction[$index];
00121                 }
00122             }
00123 
00124             if (array_key_exists('duration', $options)) {
00125                 $cacheTime = $options['duration'];
00126             }
00127             if (array_key_exists('callbacks', $options)) {
00128                 $useCallbacks = $options['callbacks'];
00129             }
00130 
00131         } else {
00132             $cacheTime = $this->cacheAction;
00133         }
00134 
00135         if ($cacheTime != '' && $cacheTime > 0) {
00136             $this->__parseFile($file, $out);
00137             if ($cache === true) {
00138                 $cached = $this->__parseOutput($out);
00139                 $this->__writeFile($cached, $cacheTime, $useCallbacks);
00140             }
00141             return $out;
00142         } else {
00143             return $out;
00144         }
00145     }
00146 /**
00147  * Parse file searching for no cache tags
00148  *
00149  * @param string $file
00150  * @param boolean $cache
00151  * @access private
00152  */
00153     function __parseFile($file, $cache) {
00154         if (is_file($file)) {
00155             $file = file_get_contents($file);
00156         } elseif ($file = fileExistsInPath($file)) {
00157             $file = file_get_contents($file);
00158         }
00159 
00160         preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
00161         preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER);
00162 
00163         if (!empty($result['0'])) {
00164             $count = 0;
00165 
00166             foreach ($result['0'] as $result) {
00167                 if (isset($oresult['0'][$count])) {
00168                     $this->__replace[] = $result;
00169                     $this->__match[] = $oresult['0'][$count];
00170                 }
00171                 $count++;
00172             }
00173         }
00174     }
00175 /**
00176  * Parse the output and replace cache tags
00177  *
00178  * @param sting $cache
00179  * @return string with all replacements made to <cake:nocache><cake:nocache>
00180  * @access private
00181  */
00182     function __parseOutput($cache) {
00183         $count = 0;
00184         if (!empty($this->__match)) {
00185 
00186             foreach ($this->__match as $found) {
00187                 $original = $cache;
00188                 $length = strlen($found);
00189                 $position = 0;
00190 
00191                     for ($i = 1; $i <= 1; $i++) {
00192                         $position = strpos($cache, $found, $position);
00193 
00194                         if ($position !== false) {
00195                             $cache = substr($original, 0, $position);
00196                             $cache .= $this->__replace[$count];
00197                             $cache .= substr($original, $position + $length);
00198                         } else {
00199                             break;
00200                         }
00201                     }
00202                     $count++;
00203             }
00204             return $cache;
00205         }
00206         return $cache;
00207     }
00208 /**
00209  * Write a cached version of the file
00210  *
00211  * @param string $file
00212  * @param sting $timestamp
00213  * @return cached view
00214  * @access private
00215  */
00216     function __writeFile($content, $timestamp, $useCallbacks = false) {
00217         $now = time();
00218 
00219         if (is_numeric($timestamp)) {
00220             $cacheTime = $now + $timestamp;
00221         } else {
00222             $cacheTime = strtotime($timestamp, $now);
00223         }
00224         $path = $this->here;
00225         if ($this->here == '/') {
00226             $path = 'home';
00227         }
00228         $cache = Inflector::slug($path);
00229 
00230         if (empty($cache)) {
00231             return;
00232         }
00233         $cache = $cache . '.php';
00234         $file = '<!--cachetime:' . $cacheTime . '--><?php';
00235 
00236         if (empty($this->plugin)) {
00237             $file .= '
00238             App::import(\'Controller\', \'' . $this->controllerName. '\');
00239             ';
00240         } else {
00241             $file .= '
00242             App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
00243             ';
00244         }
00245 
00246         $file .= '$controller =& new ' . $this->controllerName . 'Controller();
00247                 $controller->plugin = $this->plugin = \''.$this->plugin.'\';
00248                 $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
00249                 $controller->base = $this->base = \'' . $this->base . '\';
00250                 $controller->layout = $this->layout = \'' . $this->layout. '\';
00251                 $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
00252                 $controller->here = $this->here = \'' . $this->here . '\';
00253                 $controller->namedArgs  = $this->namedArgs  = \'' . $this->namedArgs . '\';
00254                 $controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
00255                 $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
00256                 $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
00257                 $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
00258                 $controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';';
00259 
00260         if ($useCallbacks == true) {
00261             $file .= '
00262                 $controller->constructClasses();
00263                 $controller->Component->initialize($controller);
00264                 $controller->beforeFilter();
00265                 $controller->Component->startup($controller);';
00266         }
00267 
00268         $file .= '
00269                 Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));
00270                 $loadedHelpers = array();
00271                 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
00272                 foreach (array_keys($loadedHelpers) as $helper) {
00273                     $camelBackedHelper = Inflector::variable($helper);
00274                     ${$camelBackedHelper} =& $loadedHelpers[$helper];
00275                     $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
00276                 }
00277         ?>';
00278         $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
00279         $file .= $content;
00280         return cache('views' . DS . $cache, $file, $timestamp);
00281     }
00282 }
00283 
00284 ?>