1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NCacheMacro extends NObject 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('NCacheMacro::initRuntime($template, $_g);');
47: }
48: }
49:
50:
51:
52: 53: 54: 55:
56: public function nodeOpened(NMacroNode $node)
57: {
58: $this->used = TRUE;
59: $node->isEmpty = FALSE;
60: $node->openingCode = NPhpWriter::using($node)
61: ->write('<?php if (NCacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>',
62: NStrings::random()
63: );
64: }
65:
66:
67:
68: 69: 70: 71:
72: public function nodeClosed(NMacroNode $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: public static function initRuntime(NFileTemplate $template, stdClass $global)
87: {
88: if (!empty($global->caches)) {
89: end($global->caches)->dependencies[NCache::FILES][] = $template->getFile();
90: }
91: }
92:
93:
94:
95: 96: 97: 98: 99: 100: 101: 102:
103: public static function createCache(ICacheStorage $cacheStorage, $key, & $parents, array $args = NULL)
104: {
105: if ($args) {
106: if (array_key_exists('if', $args) && !$args['if']) {
107: return $parents[] = (object) NULL;
108: }
109: $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
110: }
111: if ($parents) {
112: end($parents)->dependencies[NCache::ITEMS][] = $key;
113: }
114:
115: $cache = new NCache($cacheStorage, 'Nette.Templating.Cache');
116: if ($helper = $cache->start($key)) {
117: if (isset($args['expire'])) {
118: $args['expiration'] = $args['expire'];
119: }
120: $helper->dependencies = array(
121: NCache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
122: NCache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
123: );
124: $parents[] = $helper;
125: }
126: return $helper;
127: }
128:
129: }
130: