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