- Optimized forloop.* variables
[haanga.git] / haanga / runtime.php
blob8751b0f5c0ac9f360844441c2d5d983169087f41
1 <?php
4 /**
5 * Haanga Runtime class
7 * Load a template, if the template does not exists or it
8 * has changed, this class will compile it and update the cache
9 */
10 class Haanga
12 protected static $cache_dir;
13 protected static $templates_dir;
15 private function __construct()
17 /* The class can't be instanced */
20 public static function setCacheDir($dir)
22 if (!is_dir($dir)) {
23 throw new Exception("{$dir} is not a valid directory");
25 if (!is_writable($dir)) {
26 throw new Exception("{$dir} can't be written");
28 self::$cache_dir = $dir;
31 public static function setTemplateDir($dir)
33 if (!is_dir($dir)) {
34 throw new Exception("{$dir} is not a valid directory");
36 self::$templates_dir = $dir;
39 // doInclude(string $file) {{{
40 /**
41 * Load a PHP file using Haanga's root dir as
42 * base dir.
44 * @param string $file File
46 * @return void
48 public static function doInclude($file)
50 require_once dirname(__FILE__)."/".$file;
52 // }}}
54 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
56 static $compiler;
57 if (empty(self::$cache_dir) || empty(self::$templates_dir)) {
58 throw new Exception("Cache dir or template dir is missing");
61 $tpl = self::$templates_dir.'/'.$file;
62 $fnc = sha1($tpl);
63 $php = self::$cache_dir.'/'.$fnc.'.php';
65 if (!is_file($php) && !is_file($tpl)) {
66 throw new Exception("View {$file} doesn't exists");
69 if (!is_file($php) || filemtime($tpl) > filemtime($php)) {
70 if (!$compiler) {
71 require_once dirname(__FILE__)."/haanga.php";
72 $compiler = new Haanga_Main_Runtime;
74 $compiler->reset();
75 //$compiler->setDebug($php.".dump");
76 $code = $compiler->compile_file($tpl, $tpl);
77 file_put_contents($php, "<?php".$code);
79 $callback = "haanga_".$fnc;
80 if (!is_callable($callback)) {
81 require_once $php;
84 return $callback($vars, $return, $blocks);
89 * Local variables:
90 * tab-width: 4
91 * c-basic-offset: 4
92 * End:
93 * vim600: sw=4 ts=4 fdm=marker
94 * vim<600: sw=4 ts=4