1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Templates;
13:
14: use Nette,
15: Nette\Environment,
16: Nette\Caching\Cache;
17:
18:
19:
20: 21: 22: 23: 24:
25: class CachingHelper extends Nette\Object
26: {
27:
28: private $frame;
29:
30:
31: private $key;
32:
33:
34:
35: 36: 37: 38: 39: 40: 41:
42: public static function create($key, & $parents, $args = NULL)
43: {
44: if ($args) {
45: if (array_key_exists('if', $args) && !$args['if']) {
46: return $parents[] = new self;
47: }
48: $key = array_merge(array($key), array_intersect_key($args, range(0, count($args))));
49: }
50: if ($parents) {
51: end($parents)->frame[Cache::ITEMS][] = $key;
52: }
53:
54: $cache = self::getCache();
55: if (isset($cache[$key])) {
56: echo $cache[$key];
57: return FALSE;
58:
59: } else {
60: $obj = new self;
61: $obj->key = $key;
62: $obj->frame = array(
63: Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
64: Cache::EXPIRATION => isset($args['expire']) ? $args['expire'] : '+ 7 days',
65: );
66: ob_start();
67: return $parents[] = $obj;
68: }
69: }
70:
71:
72:
73: 74: 75: 76:
77: public function save()
78: {
79: if ($this->key !== NULL) {
80: $this->getCache()->save($this->key, ob_get_flush(), $this->frame);
81: }
82: $this->key = $this->frame = NULL;
83: }
84:
85:
86:
87: 88: 89: 90: 91:
92: public function addFile($file)
93: {
94: $this->frame[Cache::FILES][] = $file;
95: }
96:
97:
98:
99:
100:
101:
102:
103: 104: 105:
106: protected static function getCache()
107: {
108: return Environment::getCache('Nette.Template.Cache');
109: }
110:
111: }