1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class CacheMacro extends Object implements IMacro
22: {
23:
24: private $used;
25:
26:
27: 28: 29: 30:
31: public function initialize()
32: {
33: $this->used = FALSE;
34: }
35:
36:
37: 38: 39: 40:
41: public function finalize()
42: {
43: if ($this->used) {
44: return array('CacheMacro::initRuntime($template, $_g);');
45: }
46: }
47:
48:
49: 50: 51: 52:
53: public function nodeOpened(MacroNode $node)
54: {
55: $this->used = TRUE;
56: $node->isEmpty = FALSE;
57: $node->openingCode = PhpWriter::using($node)
58: ->write('<?php if (CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
59: Strings::random()
60: );
61: }
62:
63:
64: 65: 66: 67:
68: public function nodeClosed(MacroNode $node)
69: {
70: $node->closingCode = '<?php $_l->tmp = array_pop($_g->caches); if (!$_l->tmp instanceof stdClass) $_l->tmp->end(); } ?>';
71: }
72:
73:
74:
75:
76:
77: 78: 79:
80: public static function initRuntime(FileTemplate $template, stdClass $global)
81: {
82: if (!empty($global->caches)) {
83: end($global->caches)->dependencies[Cache::FILES][] = $template->getFile();
84: }
85: }
86:
87:
88: 89: 90: 91: 92: 93: 94: 95:
96: public static function createCache(ICacheStorage $cacheStorage, $key, & $parents, array $args = NULL)
97: {
98: if ($args) {
99: if (array_key_exists('if', $args) && !$args['if']) {
100: return $parents[] = new stdClass;
101: }
102: $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
103: }
104: if ($parents) {
105: end($parents)->dependencies[Cache::ITEMS][] = $key;
106: }
107:
108: $cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
109: if ($helper = $cache->start($key)) {
110: if (isset($args['expire'])) {
111: $args['expiration'] = $args['expire'];
112: }
113: $helper->dependencies = array(
114: Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
115: Cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
116: );
117: $parents[] = $helper;
118: }
119: return $helper;
120: }
121:
122: }
123: