- Added missing copyright to meneame
[haanga.git] / lib / Haanga.php
bloba5d469f30860e8eec27feb819570d88118cfd120
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 César Rodas and Menéame Comunicacions S.L. |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
39 /**
40 * Haanga Runtime class
42 * Simple class to call templates efficiently. This class aims
43 * to reduce the compilation of a template as less a possible. Also
44 * it will not load in memory the compiler, except when there is not
45 * cache (compiled template) or it is out-dated.
48 class Haanga
50 protected static $cache_dir;
51 protected static $templates_dir='.';
52 protected static $debug;
53 protected static $bootstrap = NULL;
54 protected static $check_ttl;
55 protected static $check_get;
56 protected static $check_set;
57 protected static $use_autoload = TRUE;
58 protected static $hash_filename = TRUE;
59 protected static $compiler = array();
61 public static $has_compiled;
63 private function __construct()
65 /* The class can't be instanced */
68 final public static function AutoLoad($class)
70 static $loaded = array();
71 static $path;
73 if (!isset($loaded[$class]) && substr($class, 0, 6) === 'Haanga' && !class_exists($class, false)) {
74 if ($path === NULL) {
75 $path = dirname(__FILE__);
77 $file = $path.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
78 if (is_file($file)) {
79 require $file;
81 $loaded[$class] = TRUE;
82 return;
85 return FALSE;
88 // configure(Array $opts) {{{
89 /**
90 * Configuration to load Haanga
92 * Options:
94 * - (string) cache_dir
95 * - (string) tempalte_dir
96 * - (callback) on_compile
97 * - (boolean) debug
98 * - (int) check_ttl
99 * - (callback) check_get
100 * - (callback) check_set
101 * - (boolean) autoload
102 * - (boolean) use_hash_filename
104 * @return void
106 final public static function configure(Array $opts)
108 foreach ($opts as $option => $value) {
109 switch (strtolower($option)) {
110 case 'cache_dir':
111 self::$cache_dir = $value;
112 break;
113 case 'template_dir':
114 self::$templates_dir = $value;
115 break;
116 case 'bootstrap':
117 if (is_callable($value)) {
118 self::$bootstrap = $value;
120 break;
121 case 'debug':
122 self::enableDebug((bool)$value);
123 break;
124 case 'check_ttl':
125 self::$check_ttl = (int)$value;
126 break;
127 case 'check_get':
128 if (is_callable($value)) {
129 self::$check_get = $value;
131 break;
132 case 'check_set':
133 if (is_callable($value)) {
134 self::$check_set = $value;
136 break;
137 case 'autoload':
138 self::$use_autoload = (bool)$value;
139 break;
140 case 'use_hash_filename':
141 self::$hash_filename = (bool)$value;
142 break;
143 case 'compiler':
144 if (is_array($value)) {
145 self::$compiler = $value;
147 break;
148 default:
149 continue;
153 // }}}
155 // checkCacheDir(string $dir) {{{
157 * Check the directory where the compiled templates
158 * are stored.
160 * @param string $dir
162 * @return void
164 public static function checkCacheDir()
166 $dir = self::$cache_dir;
167 if (!is_dir($dir)) {
168 $old = umask(0);
169 if (!mkdir($dir, 0777, TRUE)) {
170 throw new Haanga_Exception("{$dir} is not a valid directory");
172 umask($old);
174 if (!is_writable($dir)) {
175 throw new Haanga_Exception("{$dir} can't be written");
178 // }}}
180 // enableDebug($bool) {{{
181 public static function enableDebug($bool)
183 self::$debug = $bool;
185 // }}}
187 // load(string $file, array $vars, bool $return, array $blocks) {{{
189 * Load
191 * Load template. If the template is already compiled, just the compiled
192 * PHP file will be included an used. If the template is new, or it
193 * had changed, the Haanga compiler is loaded in memory, and the template
194 * is compiled.
197 * @param string $file
198 * @param array $vars
199 * @param bool $return
200 * @param array $blocks
202 * @return string|NULL
204 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
206 static $compiler;
207 if (empty(self::$cache_dir)) {
208 throw new Haanga_Exception("Cache dir or template dir is missing");
211 self::$has_compiled = FALSE;
213 $tpl = self::$templates_dir.'/'.$file;
214 $fnc = sha1($tpl);
215 $callback = "haanga_".$fnc;
217 if (is_callable($callback)) {
218 return $callback($vars, $return, $blocks);
221 $php = self::$hash_filename ? $fnc : $file;
222 $php = self::$cache_dir.'/'.$php.'.php';
224 $check = TRUE;
226 if (self::$check_ttl && self::$check_get && self::$check_set) {
227 /* */
228 if (call_user_func(self::$check_get, $callback)) {
229 /* disable checking for the next $check_ttl seconds */
230 $check = FALSE;
231 } else {
232 $result = call_user_func(self::$check_set, $callback, TRUE, self::$check_ttl);
236 if (!is_file($php) || ($check && filemtime($tpl) > filemtime($php))) {
238 if (!is_file($tpl)) {
239 /* There is no template nor compiled file */
240 throw new Exception("View {$file} doesn't exists");
243 /* recompile */
244 if (!$compiler) {
245 self::checkCacheDir();
247 /* Load needed files (to avoid autoload as much as possible) */
248 $dir = dirname(__FILE__);
249 require_once "{$dir}/Haanga/AST.php";
250 require_once "{$dir}/Haanga/Compiler.php";
251 require_once "{$dir}/Haanga/Compiler/Runtime.php";
252 require_once "{$dir}/Haanga/Compiler/Parser.php";
253 require_once "{$dir}/Haanga/Compiler/Lexer.php";
254 require_once "{$dir}/Haanga/Generator/PHP.php";
255 require_once "{$dir}/Haanga/Extension.php";
256 require_once "{$dir}/Haanga/Extension/Filter.php";
257 require_once "{$dir}/Haanga/Extension/Tag.php";
259 /* load compiler (done just once) */
260 if (self::$use_autoload) {
261 spl_autoload_register(array(__CLASS__, 'AutoLoad'));
264 $compiler = new Haanga_Compiler_Runtime;
266 if (self::$bootstrap) {
267 /* call bootstrap hook, just the first time */
268 call_user_func(self::$bootstrap);
271 if (count(self::$compiler) != 0) {
272 foreach (self::$compiler as $opt => $value) {
273 Haanga_Compiler::setOption($opt, $value);
278 $compiler->reset();
280 if (self::$debug) {
281 $compiler->setDebug($php.".dump");
284 if (!is_dir(dirname($php))) {
285 $old = umask(0);
286 mkdir(dirname($php), 0777, TRUE);
287 umask($old);
291 $code = $compiler->compile_file($tpl, FALSE, $vars);
293 file_put_contents($php, "<?php".$code, LOCK_EX);
294 self::$has_compiled = TRUE;
297 if (!is_callable($callback)) {
298 require $php;
301 return $callback($vars, $return, $blocks);
303 // }}}
308 * Local variables:
309 * tab-width: 4
310 * c-basic-offset: 4
311 * End:
312 * vim600: sw=4 ts=4 fdm=marker
313 * vim<600: sw=4 ts=4