Namespaces

  • 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
  • PHP

Classes

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