1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
10: * @package Nette\Templates
11: */
12:
13:
14:
15: /**
16: * Caching template helper.
17: *
18: * @author David Grudl
19: */
20: class CachingHelper extends Object
21: {
22: /** @var array */
23: private $frame;
24:
25: /** @var string */
26: private $key;
27:
28:
29:
30: /**
31: * Starts the output cache. Returns CachingHelper object if buffering was started.
32: * @param string
33: * @param CachingHelper
34: * @param array
35: * @return CachingHelper
36: */
37: public static function create($key, & $parents, $args = NULL)
38: {
39: if ($args) {
40: $key .= md5(serialize($args));
41: }
42: if ($parents) {
43: end($parents)->frame[Cache::ITEMS][] = $key;
44: }
45:
46: $cache = self::getCache();
47: if (isset($cache[$key])) {
48: echo $cache[$key];
49: return FALSE;
50:
51: } else {
52: $obj = new self;
53: $obj->key = $key;
54: $obj->frame = array(
55: Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
56: Cache::EXPIRE => isset($args['expire']) ? $args['expire'] : '+ 7 days',
57: );
58: ob_start();
59: return $parents[] = $obj;
60: }
61: }
62:
63:
64:
65: /**
66: * Stops and saves the cache.
67: * @return void
68: */
69: public function save()
70: {
71: $this->getCache()->save($this->key, ob_get_flush(), $this->frame);
72: $this->key = $this->frame = NULL;
73: }
74:
75:
76:
77: /**
78: * Adds the file dependency.
79: * @param string
80: * @return void
81: */
82: public function addFile($file)
83: {
84: $this->frame[Cache::FILES][] = $file;
85: }
86:
87:
88:
89: /********************* backend ****************d*g**/
90:
91:
92:
93: /**
94: * @return Cache
95: */
96: protected static function getCache()
97: {
98: return Environment::getCache('Nette.Template.Cache');
99: }
100:
101: }