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. |
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. |
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. |
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. |
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 +---------------------------------------------------------------------------------+
38 if (!defined('HAANGA_VERSION')) {
39 /* anyone can override this value to force recompilation */
40 define('HAANGA_VERSION', '1.0.4');
45 * Haanga Runtime class
47 * Simple class to call templates efficiently. This class aims
48 * to reduce the compilation of a template as less a possible. Also
49 * it will not load in memory the compiler, except when there is not
50 * cache (compiled template) or it is out-dated.
55 protected static $cache_dir;
56 protected static $templates_dir='.';
57 protected static $debug;
58 protected static $bootstrap = NULL;
59 protected static $check_ttl;
60 protected static $check_get;
61 protected static $check_set;
62 protected static $use_autoload = TRUE;
63 protected static $hash_filename = TRUE;
64 protected static $compiler = array();
66 public static $has_compiled;
68 private function __construct()
70 /* The class can't be instanced */
73 final public static function AutoLoad($class)
75 static $loaded = array();
78 if (!isset($loaded[$class]) && substr($class, 0, 6) === 'Haanga' && !class_exists($class, false)) {
80 $path = dirname(__FILE__
);
82 $file = $path.DIRECTORY_SEPARATOR
.str_replace('_', DIRECTORY_SEPARATOR
, $class).'.php';
86 $loaded[$class] = TRUE;
93 public static function getTemplateDir()
95 return self
::$templates_dir;
98 // configure(Array $opts) {{{
100 * Configuration to load Haanga
104 * - (string) cache_dir
105 * - (string) tempalte_dir
106 * - (callback) on_compile
109 * - (callback) check_get
110 * - (callback) check_set
111 * - (boolean) autoload
112 * - (boolean) use_hash_filename
116 final public static function configure(Array $opts)
118 foreach ($opts as $option => $value) {
119 switch (strtolower($option)) {
121 self
::$cache_dir = $value;
124 self
::$templates_dir = $value;
127 if (is_callable($value)) {
128 self
::$bootstrap = $value;
132 self
::enableDebug((bool)$value);
135 self
::$check_ttl = (int)$value;
138 if (is_callable($value)) {
139 self
::$check_get = $value;
143 if (is_callable($value)) {
144 self
::$check_set = $value;
148 self
::$use_autoload = (bool)$value;
150 case 'use_hash_filename':
151 self
::$hash_filename = (bool)$value;
154 if (is_array($value)) {
155 self
::$compiler = $value;
165 // checkCacheDir(string $dir) {{{
167 * Check the directory where the compiled templates
174 public static function checkCacheDir()
176 $dir = self
::$cache_dir;
179 if (!mkdir($dir, 0777, TRUE)) {
180 throw new Haanga_Exception("{$dir} is not a valid directory");
184 if (!is_writable($dir)) {
185 throw new Haanga_Exception("{$dir} can't be written");
190 // enableDebug($bool) {{{
191 public static function enableDebug($bool)
193 self
::$debug = $bool;
197 // getCompiler($checkdir=TRUE) {{{
199 * This function is a singleton for the Haanga_Compiler_Runtime class.
200 * The instance is already set up properly and resetted.
203 * @param bool $checkdir TRUE
205 * @return Haanga_Compiler_Runtime
207 protected static function getCompiler($checkdir=TRUE)
210 static $has_checkdir = FALSE;
214 /* Load needed files (to avoid autoload as much as possible) */
215 $dir = dirname(__FILE__
);
216 require_once "{$dir}/Haanga/AST.php";
217 require_once "{$dir}/Haanga/Compiler.php";
218 require_once "{$dir}/Haanga/Compiler/Runtime.php";
219 require_once "{$dir}/Haanga/Compiler/Parser.php";
220 require_once "{$dir}/Haanga/Compiler/Tokenizer.php";
221 require_once "{$dir}/Haanga/Generator/PHP.php";
222 require_once "{$dir}/Haanga/Extension.php";
223 require_once "{$dir}/Haanga/Extension/Filter.php";
224 require_once "{$dir}/Haanga/Extension/Tag.php";
226 /* load compiler (done just once) */
227 if (self
::$use_autoload) {
228 spl_autoload_register(array(__CLASS__
, 'AutoLoad'));
231 $compiler = new Haanga_Compiler_Runtime
;
233 if (self
::$bootstrap) {
234 /* call bootstrap hook, just the first time */
235 call_user_func(self
::$bootstrap);
238 if (count(self
::$compiler) != 0) {
239 foreach (self
::$compiler as $opt => $value) {
240 Haanga_Compiler
::setOption($opt, $value);
246 if ($checkdir && !$has_checkdir) {
247 self
::checkCacheDir();
248 $has_checkdir = TRUE;
256 // callback compile(string $tpl, $context=array()) {{{
258 * Compile one template and return a PHP function
260 * @param string $tpl Template body
261 * @param array $context Context variables useful to generate efficient code (for array, objects and array)
263 * @return callback($vars=array(), $return=TRUE, $block=array())
265 public static function compile($tpl, $context=array())
267 $compiler = self
::getCompiler(FALSE);
269 foreach ($context as $var => $value) {
270 $compiler->set_context($var, $value);
273 $code = $compiler->compile($tpl);
275 return create_function('$vars=array(), $return=TRUE, $blocks=array()', $code);
279 // safe_load(string $file, array $vars, bool $return, array $blocks) {{{
280 public static function Safe_Load($file, $vars = array(), $return=FALSE, $blocks=array())
284 $tpl = self
::$templates_dir.'/'.$file;
285 if (file_exists($tpl)) {
286 /* call load if the tpl file exists */
287 return self
::Load($file, $vars, $return, $blocks);
289 } Catch (Exception
$e) {
291 /* some error but we don't care at all */
296 // load(string $file, array $vars, bool $return, array $blocks) {{{
300 * Load template. If the template is already compiled, just the compiled
301 * PHP file will be included an used. If the template is new, or it
302 * had changed, the Haanga compiler is loaded in memory, and the template
306 * @param string $file
308 * @param bool $return
309 * @param array $blocks
311 * @return string|NULL
313 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
315 if (empty(self
::$cache_dir)) {
316 throw new Haanga_Exception("Cache dir or template dir is missing");
319 self
::$has_compiled = FALSE;
321 $tpl = self
::$templates_dir.'/'.$file;
323 $callback = "haanga_".$fnc;
325 if (is_callable($callback)) {
326 return $callback($vars, $return, $blocks);
329 $php = self
::$hash_filename ?
$fnc : $file;
330 $php = self
::$cache_dir.'/'.$php.'.php';
334 if (self
::$check_ttl && self
::$check_get && self
::$check_set) {
336 if (call_user_func(self
::$check_get, $callback)) {
337 /* disable checking for the next $check_ttl seconds */
340 $result = call_user_func(self
::$check_set, $callback, TRUE, self
::$check_ttl);
344 if (!is_file($php) ||
($check && filemtime($tpl) > filemtime($php))) {
346 if (!is_file($tpl)) {
347 /* There is no template nor compiled file */
348 throw new Exception("View {$file} doesn't exists");
351 if (!is_dir(dirname($php))) {
353 mkdir(dirname($php), 0777, TRUE);
357 $fp = fopen($php, "a+");
358 /* try to block PHP file */
359 if (!flock($fp, LOCK_EX | LOCK_NB
)) {
360 /* couldn't block, another process is already compiling */
364 ** if there is an old version of the cache
368 if (is_callable($callback)) {
369 return $callback($vars, $return, $blocks);
373 ** no luck, probably the template is new
374 ** the compilation will be done, but we won't
375 ** save it (we'll use eval instead)
381 $compiler = self
::getCompiler();
384 $compiler->setDebug($php.".dump");
388 $code = $compiler->compile_file($tpl, FALSE, $vars);
389 } catch (Exception
$e) {
392 ** set the $php file as old (to force future
395 touch($php, 300, 300);
398 /* re-throw exception */
403 ftruncate($fp, 0); // truncate file
404 fwrite($fp, "<?php".$code);
405 flock($fp, LOCK_UN
); // release the lock
412 self
::$has_compiled = TRUE;
415 if (!is_callable($callback)) {
416 /* Load the cached PHP file */
418 if (!is_callable($callback)) {
420 really weird case ($php is empty, another process is compiling
421 the $tpl for the first time), so create a lambda function
424 $lambda= self
::compile(file_get_contents($tpl), $vars);
425 return $lambda($vars, $return, $blocks);
429 if (!isset($HAANGA_VERSION) ||
$HAANGA_VERSION != HAANGA_VERSION
) {
430 touch($php, 300, 300);
434 return $callback($vars, $return, $blocks);
445 * vim600: sw=4 ts=4 fdm=marker