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: */
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 CachingHelper
39: * @param array
40: * @return CachingHelper
41: */
42: public static function create($key, & $parents, $args = NULL)
43: {
44: if ($args) {
45: $key .= md5(serialize($args));
46: }
47: if ($parents) {
48: end($parents)->frame[Cache::ITEMS][] = $key;
49: }
50:
51: $cache = self::getCache();
52: if (isset($cache[$key])) {
53: echo $cache[$key];
54: return FALSE;
55:
56: } else {
57: $obj = new self;
58: $obj->key = $key;
59: $obj->frame = array(
60: Cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
61: Cache::EXPIRE => isset($args['expire']) ? $args['expire'] : '+ 7 days',
62: );
63: ob_start();
64: return $parents[] = $obj;
65: }
66: }
67:
68:
69:
70: /**
71: * Stops and saves the cache.
72: * @return void
73: */
74: public function save()
75: {
76: $this->getCache()->save($this->key, ob_get_flush(), $this->frame);
77: $this->key = $this->frame = NULL;
78: }
79:
80:
81:
82: /**
83: * Adds the file dependency.
84: * @param string
85: * @return void
86: */
87: public function addFile($file)
88: {
89: $this->frame[Cache::FILES][] = $file;
90: }
91:
92:
93:
94: /********************* backend ****************d*g**/
95:
96:
97:
98: /**
99: * @return Nette\Caching\Cache
100: */
101: protected static function getCache()
102: {
103: return Environment::getCache('Nette.Template.Cache');
104: }
105:
106: }