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. |
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 +---------------------------------------------------------------------------------+
39 // Haanga_Exception {{{
41 * General exception class. It is thrown
42 * when something is not configured properly
45 class Haanga_Exception
extends Exception
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.
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) {{{
73 * Set the directory where the compiled templates
80 public static function setCacheDir($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;
92 // setTemplateDir(string $dir) {{{
94 * Set the directory where the templates are located.
100 public static function setTemplateDir($dir)
103 throw new Haanga_Exception("{$dir} is not a valid directory");
105 self
::$templates_dir = $dir;
109 // enableDebug($bool) {{{
110 public static function enableDebug($bool)
112 self
::$debug = $bool;
116 // doInclude(string $file) {{{
118 * Load a PHP file using Haanga's root dir as
121 * @param string $file File
125 public static function doInclude($file)
127 require_once dirname(__FILE__
)."/".$file;
131 // load(string $file, array $vars, bool $return, array $blocks) {{{
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
141 * @param string $file
143 * @param bool $return
144 * @param array $blocks
146 * @return string|NULL
148 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
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;
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)) {
172 require_once dirname(__FILE__
)."/haanga.php";
173 $compiler = new Haanga_Compiler_Runtime
;
177 $compiler->setDebug($php.".dump");
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)) {
193 return $callback($vars, $return, $blocks);
204 * vim600: sw=4 ts=4 fdm=marker