Add WIP TemplateLoader
[hoplite.git] / views / template_loader.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\views;
18
19 /*!
20 This class knows how to load and cache templates to the file system.
21 */
22 class TemplateLoader
23 {
24 /*! @var TemplateLoader Singleton instance */
25 static private $instance = NULL;
26
27 /*! @var string Base path for loading the template file. Use %s to indicate
28 where the name (passed to the constructor) should be
29 substituted.
30 */
31 protected $template_path = '%s.tpl';
32
33 /*! @var string The cache path for templates. Unlike |$template_path|, this
34 should only be a path, to which the cached template name will
35 be appended. This should not end with a trailing slash.
36 */
37 protected $cache_path = '/tmp/phalanx_views';
38
39 /*! @var string A header to put at the beginning of each cached template file,
40 common for setting include paths or cache debug information.
41 */
42 protected $cache_prefix = '';
43
44 /*! @var array An array of Template objects, keyed by the template name. */
45 protected $cache = array();
46
47 /*! Gets the singleton instance. */
48 static public function GetInstance()
49 {
50 if (!self::$instance)
51 self::$instance = new __class__();
52 return self::$instance();
53 }
54 /*! Sets the singleton instance. */
55 static public function SetInstance($instance) { self::$instance = $instance; }
56
57 /*! Accessors */
58 public function set_template_path($path) { $this->$template_path = $path; }
59 function template_path() { return $this->$template_path; }
60
61 public function set_cache_path($path) { $this->$cache_path = $path; }
62 public function cache_path() { return $this->$cache_path; }
63
64 /*!
65 Loads a template from a file, creates a Template object, and returns a copy
66 of that object.
67
68 @param string Template name, with wich the template plath is formatted.
69
70 @return Template Clone of the cached template.
71 */
72 public function Load($name)
73 {
74 // First check the memory cache.
75 if (isset($this->cache[$name]))
76 return clone $this->cache[$name];
77
78 // Then check the filesystem cache.
79 $template = $this->_LoadIfCached($name);
80 if ($template) {
81 $this->cache[$name] = $template;
82 return clone $template;
83 }
84
85 // Finally, parse and cache the template.
86 $template = $this->_Cache($name);
87 $this->cache[$name] = $template;
88 return clone $template;
89 }
90
91 /*!
92 Loads a cached filesystem template if it is up-to-date.
93
94 @param string Template name
95
96 @return Template|NULL
97 */
98 protected function _LoadIfCached($name)
99 {
100 $cache_path = $this->_CachePath($name);
101 $tpl_path = $this->_TemplatePath($name);
102
103 // Make sure the cached file exists and hasn't gotten out-of-date.
104 if (!file_exists($cache_path) || filemtime($cache_path) < filemtime($tpl_path)) {
105 return NULL;
106
107 // Load the contents of the cache.
108 $data = @file_get_contents($cache_path);
109 if ($data === FALSE)
110 return NULL;
111
112 return Template::NewWithData($data);
113 }
114
115 /*!
116 Loads a raw template from the file system, stores the compiled template in
117 the file system, and returns a new template object with that data.
118
119 @param string Template name.
120
121 @return Template
122 */
123 protected function _Cache($name)
124 {
125 $cache_path = $this->_CachePath($name);
126 $tpl_path = $this->_TemplatePath($name);
127
128 $data = @file_get_contents($tpl_path);
129 if ($data === FALSE)
130 throw new TemplateLoaderException('Could not load template ' . $this->template_name);
131
132 $template = Template::NewWithCompiledData($data);
133
134 // Cache the file.
135 if (!file_put_contents($cache_path, $this->cache_prefix . $template->template()))
136 throw new TemplateLoaderException('Could not cache ' . $this->template_name . ' to ' . $cache_path);
137 }
138
139 /*! Returns the template path for a given template name. */
140 protected function _TemplatePath($name)
141 {
142 return sprintf($this->template_path, $name);
143 }
144
145 /*! Returns the cache path for a given template name. */
146 protected function _CachePath($name)
147 {
148 return self::$cache_path . '/' . $name . '.phpi';
149 }
150 }
151
152 class TemplateLoaderException extends \Exception {}