- Added django-yui-layout-templates project with few modifications
[haanga.git] / lib / runtime.php
blob8bf93abb171a0acaa2b09a68d2dbfa2a0196fbe5
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 Haanga |
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 // Haanga_Exception {{{
40 /**
41 * General exception class. It is thrown
42 * when something is not configured properly
45 class Haanga_Exception extends Exception
48 // }}}
50 /**
51 * Haanga Runtime class
53 * Simple class to call templates efficiently. This class aims
54 * to reduce the compilation of a template as less a possible. Also
55 * it will not load in memory the compiler, except when there is not
56 * cache (compiled template) or it is out-dated.
59 class Haanga
61 protected static $cache_dir;
62 protected static $templates_dir='.';
63 protected static $debug;
64 public static $has_compiled;
66 private function __construct()
68 /* The class can't be instanced */
71 // setCacheDir(string $dir) {{{
72 /**
73 * Set the directory where the compiled templates
74 * are stored.
76 * @param string $dir
78 * @return void
80 public static function setCacheDir($dir)
82 if (!is_dir($dir)) {
83 throw new Haanga_Exception("{$dir} is not a valid directory");
85 if (!is_writable($dir)) {
86 throw new Haanga_Exception("{$dir} can't be written");
88 self::$cache_dir = $dir;
90 // }}}
92 // setTemplateDir(string $dir) {{{
93 /**
94 * Set the directory where the templates are located.
96 * @param string $dir
98 * @return void
100 public static function setTemplateDir($dir)
102 if (!is_dir($dir)) {
103 throw new Haanga_Exception("{$dir} is not a valid directory");
105 self::$templates_dir = $dir;
107 // }}}
109 // enableDebug($bool) {{{
110 public static function enableDebug($bool)
112 self::$debug = $bool;
114 // }}}
116 // doInclude(string $file) {{{
118 * Load a PHP file using Haanga's root dir as
119 * base dir.
121 * @param string $file File
123 * @return void
125 public static function doInclude($file)
127 require_once dirname(__FILE__)."/".$file;
129 // }}}
131 // load(string $file, array $vars, bool $return, array $blocks) {{{
133 * Load
135 * Load template. If the template is already compiled, just the compiled
136 * PHP file will be included an used. If the template is new, or it
137 * had changed, the Haanga compiler is loaded in memory, and the template
138 * is compiled.
141 * @param string $file
142 * @param array $vars
143 * @param bool $return
144 * @param array $blocks
146 * @return string|NULL
148 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
150 static $compiler;
151 if (empty(self::$cache_dir) || empty(self::$templates_dir)) {
152 throw new Haanga_Exception("Cache dir or template dir is missing");
155 self::$has_compiled = FALSE;
157 $tpl = self::$templates_dir.'/'.$file;
158 $fnc = sha1($tpl);
159 $php = self::$cache_dir.'/'.$fnc.'.php';
160 $callback = "haanga_".$fnc;
162 if (is_callable($callback)) {
163 return $callback($vars, $return, $blocks);
166 if (!is_file($php) && !is_file($tpl)) {
167 throw new Exception("View {$file} doesn't exists");
170 if (!is_file($php) || filemtime($tpl) > filemtime($php)) {
171 if (!$compiler) {
172 require_once dirname(__FILE__)."/haanga.php";
173 $compiler = new Haanga_Compiler_Runtime;
175 $compiler->reset();
176 if (self::$debug) {
177 $compiler->setDebug($php.".dump");
180 try {
181 $code = $compiler->compile_file($tpl, FALSE);
182 } catch (Exception $e) {
183 throw new Haanga_CompilerException($tpl.' :'.$e->getMessage());
186 file_put_contents($php, "<?php".$code);
187 self::$has_compiled = TRUE;
189 if (!is_callable($callback)) {
190 require_once $php;
193 return $callback($vars, $return, $blocks);
195 // }}}
200 * Local variables:
201 * tab-width: 4
202 * c-basic-offset: 4
203 * End:
204 * vim600: sw=4 ts=4 fdm=marker
205 * vim<600: sw=4 ts=4