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