1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Templating;
13:
14: use Nette,
15: Nette\Caching;
16:
17:
18:
19: 20: 21: 22: 23:
24: class FileTemplate extends Template implements IFileTemplate
25: {
26:
27: private $file;
28:
29:
30:
31: 32: 33: 34:
35: public function __construct($file = NULL)
36: {
37: if ($file !== NULL) {
38: $this->setFile($file);
39: }
40: }
41:
42:
43:
44: 45: 46: 47: 48:
49: public function setFile($file)
50: {
51: $this->file = realpath($file);
52: if (!$this->file) {
53: throw new Nette\FileNotFoundException("Missing template file '$file'.");
54: }
55: return $this;
56: }
57:
58:
59:
60: 61: 62: 63:
64: public function getFile()
65: {
66: return $this->file;
67: }
68:
69:
70:
71: 72: 73: 74:
75: public function getSource()
76: {
77: return file_get_contents($this->file);
78: }
79:
80:
81:
82:
83:
84:
85:
86: 87: 88: 89:
90: public function render()
91: {
92: if ($this->file == NULL) {
93: throw new Nette\InvalidStateException("Template file name was not specified.");
94: }
95:
96: $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
97: if ($storage instanceof Caching\Storages\PhpFileStorage) {
98: $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
99: }
100: $cached = $compiled = $cache->load($this->file);
101:
102: if ($compiled === NULL) {
103: try {
104: $compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();
105:
106: } catch (FilterException $e) {
107: $e->setSourceFile($this->file);
108: throw $e;
109: }
110:
111: $cache->save($this->file, $compiled, array(
112: Caching\Cache::FILES => $this->file,
113: Caching\Cache::CONSTS => 'Nette\Framework::REVISION',
114: ));
115: $cached = $cache->load($this->file);
116: }
117:
118: if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) {
119: Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters());
120: } else {
121: Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters());
122: }
123: }
124:
125: }
126: