3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite
\views
;
19 use \hoplite
\base\Profiling
;
21 require_once HOPLITE_ROOT
. '/base/profiling.php';
22 require_once HOPLITE_ROOT
. '/views/template.php';
25 This class knows how to load and cache templates to the file system.
29 /*! @var TemplateLoader Singleton instance */
30 static private $instance = NULL
;
32 /*! @var string Base path for loading the template file. Use %s to indicate
33 where the name (passed to the constructor) should be
36 protected $template_path = '%s.tpl';
38 /*! @var string The cache path for templates. Unlike |$template_path|, this
39 should only be a path, to which the cached template name will
40 be appended. This should not end with a trailing slash.
42 protected $cache_path = '/tmp/phalanx_views';
44 /*! @var string A header to put at the beginning of each cached template file,
45 common for setting include paths or cache debug information.
47 protected $cache_prefix = '';
49 /*! @var array An array of Template objects, keyed by the template name. */
50 protected $cache = array();
52 /*! @var array Array of template usage counts, keyed by name. Only used
55 protected $usage = array();
57 /*! Gets the singleton instance. */
58 static public function GetInstance()
60 if (!self
::$instance) {
61 $class = get_called_class();
62 self
::$instance = new $class();
64 return self
::$instance;
66 /*! Sets the singleton instance. */
67 static public function SetInstance($instance) { self
::$instance = $instance; }
70 public function set_template_path($path) { $this->template_path
= $path; }
71 function template_path() { return $this->template_path
; }
73 public function set_cache_path($path) { $this->cache_path
= $path; }
74 public function cache_path() { return $this->cache_path
; }
77 Loads a template from a file, creates a Template object, and returns a copy
80 @param string Template name, with wich the template plath is formatted.
82 @return Template Clone of the cached template.
84 public function Load($name)
86 if (Profiling
::IsProfilingEnabled() && !isset($this->usage
[$name]))
87 $this->usage
[$name] = 0;
89 // First check the memory cache.
90 if (isset($this->cache
[$name]))
91 return clone $this->cache
[$name];
93 // Then check the filesystem cache.
94 $template = $this->_LoadIfCached($name);
96 $this->cache
[$name] = $template;
97 return clone $template;
100 // Finally, parse and cache the template.
101 $template = $this->_Cache($name);
102 $this->cache
[$name] = $template;
103 return clone $template;
106 /*! Convenience function for loading templates. */
107 static public function Fetch($name)
109 return self
::GetInstance()->Load($name);
112 /*! Marks a template as having been used. */
113 public function MarkTemplateRendered($name)
115 if (!isset($this->usage
[$name]))
116 throw new \
InvalidArgumentException("Template $name has not been loaded through this instance");
118 $this->usage
[$name]++
;
122 Loads a cached filesystem template if it is up-to-date.
124 @param string Template name
126 @return Template|NULL
128 protected function _LoadIfCached($name)
130 $cache_path = $this->_CachePath($name);
131 $tpl_path = $this->_TemplatePath($name);
133 // Make sure the cached file exists and hasn't gotten out-of-date.
134 if (!file_exists($cache_path) ||
filemtime($cache_path) < filemtime($tpl_path))
137 // Load the contents of the cache.
138 $data = @file_get_contents($cache_path);
142 return Template
::NewWithCompiledData($name, $data);
146 Loads a raw template from the file system, stores the compiled template in
147 the file system, and returns a new template object with that data.
149 @param string Template name.
153 protected function _Cache($name)
155 $cache_path = $this->_CachePath($name);
156 $tpl_path = $this->_TemplatePath($name);
158 $data = @file_get_contents($tpl_path);
160 throw new TemplateLoaderException('Could not load template ' . $name);
162 $template = Template
::NewWithData($name, $data);
165 if (file_put_contents($cache_path, $this->cache_prefix
. $template->template()) === FALSE
)
166 throw new TemplateLoaderException('Could not cache ' . $name . ' to ' . $cache_path);
171 /*! Returns the template path for a given template name. */
172 protected function _TemplatePath($name)
174 return sprintf($this->template_path
, $name);
177 /*! Returns the cache path for a given template name. */
178 protected function _CachePath($name)
180 return $this->cache_path
. $name . '.phpi';
184 class TemplateLoaderException
extends \Exception
{}