cache.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: view_2helpers_2cache_8php-source.html 675 2008-12-26 00:27:14Z 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: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 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 Helper{
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  * Main method used to cache a view
00063  *
00064  * @param string $file File to cache
00065  * @param string $out output to cache
00066  * @param boolean $cache
00067  * @return view ouput
00068  */
00069     function cache($file, $out, $cache = false) {
00070         if (is_array($this->cacheAction)) {
00071             $check = str_replace('/', '_', $this->here);
00072             $replace = str_replace('/', '_', $this->base);
00073             $match = str_replace($this->base, '', $this->here);
00074             $match = str_replace('//', '/', $match);
00075             $match = str_replace('/' . $this->controllerName . '/', '', $match);
00076             $check = str_replace($replace, '', $check);
00077             $check = str_replace('_' . $this->controllerName . '_', '', $check);
00078             $check = convertSlash($check);
00079             $check = preg_replace('/^_+/', '', $check);
00080             $keys = str_replace('/', '_', array_keys($this->cacheAction));
00081             $found = array_keys($this->cacheAction);
00082             $index = null;
00083             $count = 0;
00084 
00085             foreach ($keys as $key => $value) {
00086                 if (strpos($check, $value) === 0) {
00087                     $index = $found[$count];
00088                     break;
00089                 }
00090                 $count++;
00091             }
00092 
00093             if (isset($index)) {
00094                 $pos1 = strrpos($match, '/');
00095                 $char = strlen($match) - 1;
00096 
00097                 if ($pos1 == $char) {
00098                     $match = substr($match, 0, $char);
00099                 }
00100 
00101                 $key = $match;
00102             } elseif ($this->action == 'index') {
00103                 $index = 'index';
00104             }
00105             if (isset($this->cacheAction[$index])) {
00106                 $cacheTime = $this->cacheAction[$index];
00107             } else {
00108                 $cacheTime = 0;
00109             }
00110         } else {
00111             $cacheTime = $this->cacheAction;
00112         }
00113 
00114         if ($cacheTime != '' && $cacheTime > 0) {
00115             $this->__parseFile($file, $out);
00116 
00117             if ($cache === true) {
00118                 $cached = $this->__parseOutput($out);
00119                 $this->__writeFile($cached, $cacheTime);
00120             }
00121         }
00122         return $out;
00123     }
00124 /**
00125  * Parse file searching for no cache tags
00126  *
00127  * @param string $file
00128  * @param boolean $cache
00129  * @access private
00130  */
00131     function __parseFile($file, $cache) {
00132         if (is_file($file)) {
00133             $file = file_get_contents($file);
00134         } elseif ($file = fileExistsInPath($file)) {
00135             $file = file_get_contents($file);
00136         }
00137 
00138         preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER);
00139         preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER);
00140 
00141         if (!empty($this->__replace)) {
00142             foreach ($oresult['0'] as $k => $element) {
00143                 if (array_search($element, $this->__match) !== false) {
00144                     array_splice($oresult[0], $k, 1);
00145                 }
00146             }
00147         }
00148 
00149         if (!empty($result['0'])) {
00150             $count = 0;
00151 
00152             foreach ($result['0'] as $block) {
00153                 if (isset($oresult['0'][$count])) {
00154                     $this->__replace[] = $block;
00155                     $this->__match[] = $oresult['0'][$count];
00156                 }
00157                 $count++;
00158             }
00159         }
00160     }
00161 /**
00162  * Parse the output and replace cache tags
00163  *
00164  * @param sting $cache
00165  * @return string with all replacements made to <cake:nocache><cake:nocache>
00166  * @access private
00167  */
00168     function __parseOutput($cache) {
00169         $count = 0;
00170         if (!empty($this->__match)) {
00171 
00172             foreach ($this->__match as $found) {
00173                 $original = $cache;
00174                 $length = strlen($found);
00175                 $position = 0;
00176 
00177                     for ($i = 1; $i <= 1; $i++) {
00178                         $position = strpos($cache, $found, $position);
00179 
00180                         if ($position !== false) {
00181                             $cache = substr($original, 0, $position);
00182                             $cache .= $this->__replace[$count];
00183                             $cache .= substr($original, $position + $length);
00184                         } else {
00185                             break;
00186                         }
00187                     }
00188                     $count++;
00189             }
00190             return $cache;
00191         }
00192         return $cache;
00193     }
00194 /**
00195  * Write a cached version of the file
00196  *
00197  * @param string $content
00198  * @param sting $timestamp
00199  * @return cached view
00200  * @access private
00201  */
00202     function __writeFile($content, $timestamp) {
00203         $now = time();
00204 
00205         if (is_numeric($timestamp)) {
00206             $cacheTime = $now + $timestamp;
00207         } else {
00208             $cacheTime = strtotime($timestamp, $now);
00209         }
00210 
00211         $cache = convertSlash($this->here);
00212         if (empty($cache)) {
00213             return;
00214         }
00215 
00216         $cache = $cache . '.php';
00217         $file = '<!--cachetime:' . $cacheTime . '--><?php';
00218         if (empty($this->plugin)) {
00219             $file .= '
00220             loadController(\'' . $this->view->name. '\');
00221             loadModels();
00222             ';
00223         } else {
00224             $file .= '
00225             if (!class_exists(\'AppController\')) {
00226                 if (file_exists(\'' . APP . 'app_controller.php\')) {
00227                     require(\''. APP . 'app_controller.php\');
00228                 } else {
00229                     require(\''.CAKE . 'app_controller.php\');
00230                 }
00231             }
00232             loadPluginController(\''.$this->plugin.'\',\''.$this->view->name.'\');
00233             loadPluginModels(\''.$this->plugin.'\');
00234             ';
00235         }
00236         $file .= '$this->controller = new ' . $this->view->name . 'Controller();
00237                     $this->controller->plugin = \''.$this->plugin.'\';
00238                     $this->controller->_initComponents();
00239                     $this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\');
00240                     $this->base = \'' . $this->view->base . '\';
00241                     $this->layout = \'' . $this->view->layout. '\';
00242                     $this->webroot = \'' . $this->view->webroot . '\';
00243                     $this->here = \'' . $this->view->here . '\';
00244                     $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->view->params)) . '\'));
00245                     $this->action = unserialize(\'' . serialize($this->view->action) . '\');
00246                     $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->view->data)) . '\'));
00247                     $this->themeWeb = \'' . $this->view->themeWeb . '\';
00248                     $this->plugin = \'' . $this->view->plugin . '\';
00249                     $loadedHelpers = array();
00250                     $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
00251                     foreach (array_keys($loadedHelpers) as $helper)
00252                     {
00253                         $replace = strtolower(substr($helper, 0, 1));
00254                         $camelBackedHelper = preg_replace(\'/\\w/\', $replace, $helper, 1);
00255                         ${$camelBackedHelper} =& $loadedHelpers[$helper];
00256 
00257                         if (isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers))
00258                         {
00259                             foreach (${$camelBackedHelper}->helpers as $subHelper)
00260                             {
00261                                 ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper];
00262                             }
00263                         }
00264                         $this->loaded[$camelBackedHelper] = (${$camelBackedHelper});
00265                     }
00266                     ?>';
00267         $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
00268         $file .= $content;
00269         return cache('views' . DS . $cache, $file, $timestamp);
00270     }
00271 }
00272 ?>