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:
32: public function initialize()
33: {
34: $this->used = FALSE;
35: }
36:
37:
38:
39: 40: 41: 42:
43: public function finalize()
44: {
45: if ($this->used) {
46: return array('CacheMacro::initRuntime($template, $_g);');
47: }
48: }
49:
50:
51:
52: 53: 54: 55:
56: public function nodeOpened(MacroNode $node)
57: {
58: $this->used = TRUE;
59: $node->isEmpty = FALSE;
60: $node->openingCode = PhpWriter::using($node)
61: ->write('<?php if (CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
62: Strings::random()
63: );
64: }
65:
66:
67:
68: 69: 70: 71:
72: public function nodeClosed(MacroNode $node)
73: {
74: $node->closingCode = '<?php $_l->tmp = array_pop($_g->caches); if (!$_l->tmp instanceof stdClass) $_l->tmp->end(); } ?>';
75: }
76:
77:
78:
79:
80:
81:
82:
83: 84: 85: 86:
87: public static function initRuntime($template, $global)
88: {
89: if (!empty($global->caches)) {
90: end($global->caches)->dependencies[Cache::FILES][] = $template->getFile();
91: }
92: }
93:
94:
95:
96: 97: 98: 99: 100: 101: 102: 103:
104: public static function createCache(ICacheStorage $cacheStorage, $key, & $parents, $args = NULL)
105: {
106: if ($args) {
107: if (array_key_exists('if', $args) && !$args['if']) {
108: return $parents[] = (object) NULL;
109: }
110: $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
111: }
112: if ($parents) {
113: end($parents)->dependencies[Cache::ITEMS][] = $key;
114: }
115:
116: $cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
117: if ($helper = $cache->start($key)) {
118: if (isset($args['expire'])) {
119: $args['expiration'] = $args['expire'];
120: }
121: $helper->dependencies = array(
122: Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
123: Cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
124: );
125: $parents[] = $helper;
126: }
127: return $helper;
128: }
129:
130: }
131: