CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.2
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
  1: <?php
  2: /* SVN FILE: $Id$ */
  3: /**
  4:  * CacheHelper helps create full page view caching.
  5:  *
  6:  * PHP versions 4 and 5
  7:  *
  8:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  *
 11:  * Licensed under The MIT License
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       cake
 17:  * @subpackage    cake.cake.libs.view.helpers
 18:  * @since         CakePHP(tm) v 1.0.0.2277
 19:  * @version       $Revision$
 20:  * @modifiedby    $LastChangedBy$
 21:  * @lastmodified  $Date$
 22:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 23:  */
 24: /**
 25:  * CacheHelper helps create full page view caching.
 26:  *
 27:  * When using CacheHelper you don't call any of its methods, they are all automatically
 28:  * called by View, and use the $cacheAction settings set in the controller.
 29:  *
 30:  * @package       cake
 31:  * @subpackage    cake.cake.libs.view.helpers
 32:  */
 33: class CacheHelper extends AppHelper {
 34: /**
 35:  * Array of strings replaced in cached views.
 36:  * The strings are found between <cake:nocache><cake:nocache> in views
 37:  *
 38:  * @var array
 39:  * @access private
 40:  */
 41:     var $__replace = array();
 42: /**
 43:  * Array of string that are replace with there var replace above.
 44:  * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
 45:  *
 46:  * @var array
 47:  * @access private
 48:  */
 49:     var $__match = array();
 50: /**
 51:  * cache action time
 52:  *
 53:  * @var object
 54:  * @access public
 55:  */
 56:     var $cacheAction;
 57: /**
 58:  * Counter used for counting nocache section tags.
 59:  *
 60:  * @var integer
 61:  */
 62:     var $_counter = 0;
 63: 
 64: /**
 65:  * Main method used to cache a view
 66:  *
 67:  * @param string $file File to cache
 68:  * @param string $out output to cache
 69:  * @param boolean $cache
 70:  * @return view ouput
 71:  */
 72:     function cache($file, $out, $cache = false) {
 73:         $cacheTime = 0;
 74:         $useCallbacks = false;
 75:         if (is_array($this->cacheAction)) {
 76:             $controller = Inflector::underscore($this->controllerName);
 77:             $controllerAlternate = Inflector::variable($this->controllerName);
 78: 
 79:             $check = str_replace('/', '_', $this->here);
 80:             $basePath = str_replace('/', '_', $this->base);
 81: 
 82:             $search = '/' . preg_quote($this->base, '/') . '/';
 83:             $match = preg_replace($search, '', $this->here, 1);
 84:             $match = str_replace('//', '/', $match);
 85:             $match = str_replace('/' . $controller . '/', '', $match);
 86:             $match = str_replace('/' . $controllerAlternate . '/', '', $match);
 87:             $match = str_replace('/' . $this->controllerName . '/', '', $match);
 88: 
 89:             $check = str_replace($basePath, '', $check);
 90:             $check = str_replace('_' . $controller . '_', '', $check);
 91:             $check = str_replace('_' . $this->controllerName . '_', '', $check);
 92:             $check = str_replace('_' . $controllerAlternate . '_', '', $match);
 93: 
 94:             $check = Inflector::slug($check);
 95:             $check = trim($check, '_');
 96: 
 97:             $keys = str_replace('/', '_', array_keys($this->cacheAction));
 98:             $found = array_keys($this->cacheAction);
 99:             $index = null;
100:             $count = 0;
101: 
102:             foreach ($keys as $key => $value) {
103:                 if (strpos($check, rtrim($value, '_')) === 0) {
104:                     $index = $found[$count];
105:                     break;
106:                 }
107:                 $count++;
108:             }
109: 
110:             if (isset($index)) {
111:                 $pos1 = strrpos($match, '/');
112:                 $char = strlen($match) - 1;
113: 
114:                 if ($pos1 == $char) {
115:                     $match = substr($match, 0, $char);
116:                 }
117: 
118:                 $key = $match;
119:             } elseif ($this->action == 'index') {
120:                 $index = 'index';
121:             }
122: 
123:             $options = $this->cacheAction;
124:             if (isset($this->cacheAction[$index])) {
125:                 if (is_array($this->cacheAction[$index])) {
126:                     $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
127:                 } else {
128:                     $cacheTime = $this->cacheAction[$index];
129:                 }
130:             }
131: 
132:             if (array_key_exists('duration', $options)) {
133:                 $cacheTime = $options['duration'];
134:             }
135:             if (array_key_exists('callbacks', $options)) {
136:                 $useCallbacks = $options['callbacks'];
137:             }
138: 
139:         } else {
140:             $cacheTime = $this->cacheAction;
141:         }
142: 
143:         if ($cacheTime != '' && $cacheTime > 0) {
144:             $out = preg_replace_callback('/<cake\:nocache>/', array($this, '_replaceSection'), $out);
145: 
146:             $this->__parseFile($file, $out);
147:             if ($cache === true) {
148:                 $cached = $this->__parseOutput($out);
149:                 $this->__writeFile($cached, $cacheTime, $useCallbacks);
150:                 $out = $this->_stripTags($out);
151:             }
152:             return $out;
153:         } else {
154:             return $out;
155:         }
156:     }
157: /**
158:  * Parse file searching for no cache tags
159:  *
160:  * @param string $file
161:  * @param boolean $cache
162:  * @access private
163:  */
164:     function __parseFile($file, $cache) {
165:         if (is_file($file)) {
166:             $file = file_get_contents($file);
167:         } elseif ($file = fileExistsInPath($file)) {
168:             $file = file_get_contents($file);
169:         }
170: 
171:         preg_match_all('/(<cake:nocache:\d{3}>(?<=<cake:nocache:\d{3}>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
172:         preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
173:         $fileResult = $fileResult[0];
174:         $outputResult = $outputResult[0];
175: 
176:         if (!empty($this->__replace)) {
177:             foreach ($outputResult as $i => $element) {
178:                 $index = array_search($element, $this->__match);
179:                 if ($index !== false) {
180:                     unset($outputResult[$i]);
181:                 }
182:             }
183:             $outputResult = array_values($outputResult);
184:         }
185: 
186:         if (!empty($fileResult)) {
187:             $i = 0;
188:             foreach ($fileResult as $cacheBlock) {
189:                 if (isset($outputResult[$i])) {
190:                     $this->__replace[] = $cacheBlock;
191:                     $this->__match[] = $outputResult[$i];
192:                 }
193:                 $i++;
194:             }
195:         }
196:     }
197: /**
198:  * Munges the output from a view with cache tags, and numbers the sections.
199:  * This helps solve issues with empty/duplicate content.
200:  *
201:  * @param string $content The content to munge.
202:  * @return string The content with cake:nocache tags replaced.
203:  */
204:     function _replaceSection($matches) {
205:         $this->_counter += 1;
206:         return sprintf('<cake:nocache:%03d>', $this->_counter);
207:     }
208: 
209: /**
210:  * Strip cake:nocache tags from a string. Since View::render()
211:  * only removes un-numbered nocache tags, remove all the numbered ones.
212:  * This is the complement to _replaceSection.
213:  *
214:  * @param string $content String to remove tags from.
215:  * @return string String with tags removed.
216:  */
217:     function _stripTags($content) {
218:         return preg_replace('#<\/?cake\:nocache(\:\d{3})?>#', '', $content);
219:     }
220: 
221: /**
222:  * Parse the output and replace cache tags
223:  *
224:  * @param sting $cache
225:  * @return string with all replacements made to <cake:nocache><cake:nocache>
226:  * @access private
227:  */
228:     function __parseOutput($cache) {
229:         $count = 0;
230:         if (!empty($this->__match)) {
231:             foreach ($this->__match as $found) {
232:                 $original = $cache;
233:                 $length = strlen($found);
234:                 $position = 0;
235: 
236:                 for ($i = 1; $i <= 1; $i++) {
237:                     $position = strpos($cache, $found, $position);
238: 
239:                     if ($position !== false) {
240:                         $cache = substr($original, 0, $position);
241:                         $cache .= $this->__replace[$count];
242:                         $cache .= substr($original, $position + $length);
243:                     } else {
244:                         break;
245:                     }
246:                 }
247:                 $count++;
248:             }
249:             return $cache;
250:         }
251:         return $cache;
252:     }
253: /**
254:  * Write a cached version of the file
255:  *
256:  * @param string $file
257:  * @param sting $timestamp
258:  * @return cached view
259:  * @access private
260:  */
261:     function __writeFile($content, $timestamp, $useCallbacks = false) {
262:         $now = time();
263: 
264:         if (is_numeric($timestamp)) {
265:             $cacheTime = $now + $timestamp;
266:         } else {
267:             $cacheTime = strtotime($timestamp, $now);
268:         }
269:         $path = $this->here;
270:         if ($this->here == '/') {
271:             $path = 'home';
272:         }
273:         $cache = strtolower(Inflector::slug($path));
274: 
275:         if (empty($cache)) {
276:             return;
277:         }
278:         $cache = $cache . '.php';
279:         $file = '<!--cachetime:' . $cacheTime . '--><?php';
280: 
281:         if (empty($this->plugin)) {
282:             $file .= '
283:             App::import(\'Controller\', \'' . $this->controllerName. '\');
284:             ';
285:         } else {
286:             $file .= '
287:             App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
288:             ';
289:         }
290: 
291:         $file .= '$controller =& new ' . $this->controllerName . 'Controller();
292:                 $controller->plugin = $this->plugin = \''.$this->plugin.'\';
293:                 $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
294:                 $controller->base = $this->base = \'' . $this->base . '\';
295:                 $controller->layout = $this->layout = \'' . $this->layout. '\';
296:                 $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
297:                 $controller->here = $this->here = \'' . $this->here . '\';
298:                 $controller->namedArgs  = $this->namedArgs  = \'' . $this->namedArgs . '\';
299:                 $controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
300:                 $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
301:                 $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
302:                 $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
303:                 $controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';
304:                 Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));';
305: 
306:         if ($useCallbacks == true) {
307:             $file .= '
308:                 $controller->constructClasses();
309:                 $controller->Component->initialize($controller);
310:                 $controller->beforeFilter();
311:                 $controller->Component->startup($controller);
312:                 $this->params = $controller->params;';
313:         }
314: 
315:         $file .= '
316:                 $loadedHelpers = array();
317:                 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
318:                 foreach (array_keys($loadedHelpers) as $helper) {
319:                     $camelBackedHelper = Inflector::variable($helper);
320:                     ${$camelBackedHelper} =& $loadedHelpers[$helper];
321:                     $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
322:                 }
323:         ?>';
324:         $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
325:         $file .= $content;
326:         return cache('views' . DS . $cache, $file, $timestamp);
327:     }
328: }
329: ?>
330: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs