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 The TemplateLoader manages the reading of templates from the file system.
27 It can also work with a CacheBackend to store the compiled template data to
30 Internally it also maintains a cache that it will consult after a template is
31 loaded for the first time.
35 /*! @var TemplateLoader Singleton instance */
36 static private $instance = NULL
;
38 /*! @var string Base path for loading the template file. Use %s to indicate
39 where the name (passed to the constructor) should be
42 protected $template_path = '%s.tpl';
44 /*! @var CacheBackend A cache for compiled template data. */
45 protected $cache_backend = NULL
;
47 /*! @var array An array of Template objects, keyed by the template name. */
48 protected $cache = array();
50 /*! @var array Array of template usage counts, keyed by name. Only used
53 protected $usage = array();
55 /*! Gets the singleton instance. */
56 static public function GetInstance()
58 if (!self
::$instance) {
59 $class = get_called_class();
60 self
::$instance = new $class();
62 return self
::$instance;
64 /*! Sets the singleton instance. */
65 static public function SetInstance($instance) { self
::$instance = $instance; }
68 public function set_template_path($path) { $this->template_path
= $path; }
69 public function template_path() { return $this->template_path
; }
71 public function set_cache_backend(CacheBackend
$backend) { $this->cache_backend
= $backend; }
72 public function cache_backend() { return $this->cache_backend
; }
75 Loads a template from a file, creates a Template object, and returns a copy
78 @param string Template name, with wich the template plath is formatted.
80 @return Template Clone of the cached template.
82 public function Load($name)
84 if (Profiling
::IsProfilingEnabled() && !isset($this->usage
[$name]))
85 $this->usage
[$name] = 0;
87 // First check the memory cache.
88 if (isset($this->cache
[$name]))
89 return clone $this->cache
[$name];
91 // Then check if the cache backend has it.
92 $template = $this->_QueryCache($name);
94 $this->cache
[$name] = $template;
95 return clone $template;
98 // Finally, parse and cache the template.
99 $template = $this->_Load($name);
100 $this->cache
[$name] = $template;
101 return clone $template;
104 /*! Convenience function for loading templates. */
105 static public function Fetch($name)
107 return self
::GetInstance()->Load($name);
110 /*! Marks a template as having been used. */
111 public function MarkTemplateRendered($name)
113 if (!isset($this->usage
[$name]))
114 throw new \
InvalidArgumentException("Template $name has not been loaded through this instance");
116 $this->usage
[$name]++
;
120 Queries the optional CacheBackend for a template.
122 @param string Template name
124 @return Template|NULL
126 protected function _QueryCache($name)
128 if (!$this->cache_backend
)
131 $tpl_path = $this->_TemplatePath($name);
132 $data = $this->cache_backend
->GetTemplateDataForName($name, filemtime($tpl_path));
136 return Template
::NewWithCompiledData($name, $data);
140 Loads a raw template from the file system and compiles it. If the optional
141 CacheBackend is present, it will cache the compiled data.
143 @param string Template name.
147 protected function _Load($name)
149 $tpl_path = $this->_TemplatePath($name);
151 $data = @file_get_contents($tpl_path);
153 throw new TemplateLoaderException('Could not load template ' . $name);
155 $template = Template
::NewWithData($name, $data);
157 if ($this->cache_backend
) {
158 $this->cache_backend
->StoreCompiledTemplate(
159 $name, filemtime($tpl_path), $template->template());
165 /*! Returns the template path for a given template name. */
166 protected function _TemplatePath($name)
168 return sprintf($this->template_path
, $name);
172 class TemplateLoaderException
extends \Exception
{}