1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://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: * @package Nette\Templates
11: */
12:
13:
14:
15: /**
16: * Caching template helper.
17: *
18: * @author David Grudl
19: * @package Nette\Templates
20: */
21: class NCachingHelper extends NObject
22: {
23: /** @var array */
24: private $frame;
25:
26: /** @var string */
27: private $key;
28:
29:
30:
31: /**
32: * Starts the output cache. Returns CachingHelper object if buffering was started.
33: * @param string
34: * @param string
35: * @param array
36: * @return NCachingHelper
37: */
38: public static function create($key, $file, $tags)
39: {
40: $cache = self::getCache();
41: if (isset($cache[$key])) {
42: echo $cache[$key];
43: return FALSE;
44:
45: } else {
46: $obj = new self;
47: $obj->key = $key;
48: $obj->frame = array(
49: NCache::FILES => array($file),
50: NCache::TAGS => $tags,
51: NCache::EXPIRATION => rand(86400 * 4, 86400 * 7),
52: );
53: ob_start();
54: return $obj;
55: }
56: }
57:
58:
59:
60: /**
61: * Stops and saves the cache.
62: * @return void
63: */
64: public function save()
65: {
66: $this->getCache()->save($this->key, ob_get_flush(), $this->frame);
67: $this->key = $this->frame = NULL;
68: }
69:
70:
71:
72: /**
73: * Adds the file dependency.
74: * @param string
75: * @return void
76: */
77: public function addFile($file)
78: {
79: $this->frame[NCache::FILES][] = $file;
80: }
81:
82:
83:
84: /**
85: * Adds the cached item dependency.
86: * @param string
87: * @return void
88: */
89: public function addItem($item)
90: {
91: $this->frame[NCache::ITEMS][] = $item;
92: }
93:
94:
95:
96: /********************* backend ****************d*g**/
97:
98:
99:
100: /**
101: * @return NCache
102: */
103: protected static function getCache()
104: {
105: return NEnvironment::getCache('Nette.Template.Curly');
106: }
107:
108: }
109: