Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  * @package Nette\Latte\Macros
  7:  */
  8: 
  9: 
 10: 
 11: /**
 12:  * Macro {cache} ... {/cache}
 13:  *
 14:  * @author     David Grudl
 15:  * @package Nette\Latte\Macros
 16:  */
 17: class CacheMacro extends Object implements IMacro
 18: {
 19:     /** @var bool */
 20:     private $used;
 21: 
 22: 
 23:     /**
 24:      * Initializes before template parsing.
 25:      * @return void
 26:      */
 27:     public function initialize()
 28:     {
 29:         $this->used = FALSE;
 30:     }
 31: 
 32: 
 33:     /**
 34:      * Finishes template parsing.
 35:      * @return array(prolog, epilog)
 36:      */
 37:     public function finalize()
 38:     {
 39:         if ($this->used) {
 40:             return array('CacheMacro::initRuntime($template, $_g);');
 41:         }
 42:     }
 43: 
 44: 
 45:     /**
 46:      * New node is found.
 47:      * @return bool
 48:      */
 49:     public function nodeOpened(MacroNode $node)
 50:     {
 51:         $this->used = TRUE;
 52:         $node->isEmpty = FALSE;
 53:         $node->openingCode = PhpWriter::using($node)
 54:             ->write('<?php if (CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
 55:                 Strings::random()
 56:             );
 57:     }
 58: 
 59: 
 60:     /**
 61:      * Node is closed.
 62:      * @return void
 63:      */
 64:     public function nodeClosed(MacroNode $node)
 65:     {
 66:         $node->closingCode = '<?php $_l->tmp = array_pop($_g->caches); if (!$_l->tmp instanceof stdClass) $_l->tmp->end(); } ?>';
 67:     }
 68: 
 69: 
 70:     /********************* run-time helpers ****************d*g**/
 71: 
 72: 
 73:     /**
 74:      * @return void
 75:      */
 76:     public static function initRuntime(FileTemplate $template, stdClass $global)
 77:     {
 78:         if (!empty($global->caches)) {
 79:             end($global->caches)->dependencies[Cache::FILES][] = $template->getFile();
 80:         }
 81:     }
 82: 
 83: 
 84:     /**
 85:      * Starts the output cache. Returns CachingHelper object if buffering was started.
 86:      * @param  ICacheStorage
 87:      * @param  string
 88:      * @param  CachingHelper[]
 89:      * @param  array
 90:      * @return CachingHelper
 91:      */
 92:     public static function createCache(ICacheStorage $cacheStorage, $key, & $parents, array $args = NULL)
 93:     {
 94:         if ($args) {
 95:             if (array_key_exists('if', $args) && !$args['if']) {
 96:                 return $parents[] = new stdClass;
 97:             }
 98:             $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
 99:         }
100:         if ($parents) {
101:             end($parents)->dependencies[Cache::ITEMS][] = $key;
102:         }
103: 
104:         $cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
105:         if ($helper = $cache->start($key)) {
106:             if (isset($args['expire'])) {
107:                 $args['expiration'] = $args['expire']; // back compatibility
108:             }
109:             $helper->dependencies = array(
110:                 Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
111:                 Cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
112:             );
113:             $parents[] = $helper;
114:         }
115:         return $helper;
116:     }
117: 
118: }
119: 
Nette Framework 2.0.18 (for PHP 5.2, un-prefixed) API documentation generated by ApiGen 2.8.0