Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • CacheMacro
  • Overview
  • Namespace
  • 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 (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\CacheLatte;
  9: 
 10: use Nette;
 11: use Latte;
 12: 
 13: 
 14: /**
 15:  * Macro {cache} ... {/cache}
 16:  */
 17: class CacheMacro extends Nette\Object implements Latte\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('Nette\Bridges\CacheLatte\CacheMacro::initRuntime($template, $_g);');
 41:         }
 42:     }
 43: 
 44: 
 45:     /**
 46:      * New node is found.
 47:      * @return bool
 48:      */
 49:     public function nodeOpened(Latte\MacroNode $node)
 50:     {
 51:         if ($node->modifiers) {
 52:             trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING);
 53:         }
 54:         $this->used = TRUE;
 55:         $node->isEmpty = FALSE;
 56:         $node->openingCode = Latte\PhpWriter::using($node)
 57:             ->write('<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
 58:                 Nette\Utils\Random::generate()
 59:             );
 60:     }
 61: 
 62: 
 63:     /**
 64:      * Node is closed.
 65:      * @return void
 66:      */
 67:     public function nodeClosed(Latte\MacroNode $node)
 68:     {
 69:         $node->closingCode = '<?php $_l->tmp = array_pop($_g->caches); if (!$_l->tmp instanceof stdClass) $_l->tmp->end(); } ?>';
 70:     }
 71: 
 72: 
 73:     /********************* run-time helpers ****************d*g**/
 74: 
 75: 
 76:     /**
 77:      * @return void
 78:      */
 79:     public static function initRuntime(Latte\Template $template, \stdClass $global)
 80:     {
 81:         if (!empty($global->caches) && $template->getEngine()->getLoader() instanceof Latte\Loaders\FileLoader) {
 82:             end($global->caches)->dependencies[Nette\Caching\Cache::FILES][] = $template->getName();
 83:         }
 84:     }
 85: 
 86: 
 87:     /**
 88:      * Starts the output cache. Returns Nette\Caching\OutputHelper object if buffering was started.
 89:      * @param  Nette\Caching\IStorage
 90:      * @param  string
 91:      * @param  Nette\Caching\OutputHelper[]
 92:      * @param  array
 93:      * @return Nette\Caching\OutputHelper
 94:      */
 95:     public static function createCache(Nette\Caching\IStorage $cacheStorage, $key, & $parents, array $args = NULL)
 96:     {
 97:         if ($args) {
 98:             if (array_key_exists('if', $args) && !$args['if']) {
 99:                 return $parents[] = new \stdClass;
100:             }
101:             $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
102:         }
103:         if ($parents) {
104:             end($parents)->dependencies[Nette\Caching\Cache::ITEMS][] = $key;
105:         }
106: 
107:         $cache = new Nette\Caching\Cache($cacheStorage, 'Nette.Templating.Cache');
108:         if ($helper = $cache->start($key)) {
109:             if (isset($args['dependencies'])) {
110:                 $args += call_user_func($args['dependencies']);
111:             }
112:             if (isset($args['expire'])) {
113:                 $args['expiration'] = $args['expire']; // back compatibility
114:             }
115:             $helper->dependencies = array(
116:                 Nette\Caching\Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
117:                 Nette\Caching\Cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
118:             );
119:             $parents[] = $helper;
120:         }
121:         return $helper;
122:     }
123: 
124: }
125: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0