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