- Added gettext support
[haanga.git] / lib / Haanga.php
blobcfa89758b1bb4b228e60a9cc1f8f8f5bbf315450
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 /**
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 public static $has_compiled;
55 private function __construct()
57 /* The class can't be instanced */
60 final public static function RegisterAutoLoad()
62 set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));
63 spl_autoload_register(array(__CLASS__, 'AutoLoad'));
66 final public static function AutoLoad($class)
68 static $loaded = array();
70 if (!isset($loaded[$class]) && substr($class, 0, 6) === 'Haanga' && !class_exists($class, false)) {
71 $file = str_replace('_', '/', $class);
72 @include $file.'.php';
73 $loaded[$class] = TRUE;
77 // setCacheDir(string $dir) {{{
78 /**
79 * Set the directory where the compiled templates
80 * are stored.
82 * @param string $dir
84 * @return void
86 public static function setCacheDir($dir)
88 if (!is_dir($dir)) {
89 throw new Haanga_Exception("{$dir} is not a valid directory");
91 if (!is_writable($dir)) {
92 throw new Haanga_Exception("{$dir} can't be written");
94 self::$cache_dir = $dir;
96 // }}}
98 // setTemplateDir(string $dir) {{{
99 /**
100 * Set the directory where the templates are located.
102 * @param string $dir
104 * @return void
106 public static function setTemplateDir($dir)
108 if (!is_dir($dir)) {
109 throw new Haanga_Exception("{$dir} is not a valid directory");
111 self::$templates_dir = $dir;
113 // }}}
115 // enableDebug($bool) {{{
116 public static function enableDebug($bool)
118 self::$debug = $bool;
120 // }}}
122 // load(string $file, array $vars, bool $return, array $blocks) {{{
124 * Load
126 * Load template. If the template is already compiled, just the compiled
127 * PHP file will be included an used. If the template is new, or it
128 * had changed, the Haanga compiler is loaded in memory, and the template
129 * is compiled.
132 * @param string $file
133 * @param array $vars
134 * @param bool $return
135 * @param array $blocks
137 * @return string|NULL
139 public static function Load($file, $vars = array(), $return=FALSE, $blocks=array())
141 static $compiler;
142 if (empty(self::$cache_dir) || empty(self::$templates_dir)) {
143 throw new Haanga_Exception("Cache dir or template dir is missing");
146 self::$has_compiled = FALSE;
148 $tpl = self::$templates_dir.'/'.$file;
149 $fnc = sha1($tpl);
150 $php = self::$cache_dir.'/'.$fnc.'.php';
151 $callback = "haanga_".$fnc;
153 if (is_callable($callback)) {
154 return $callback($vars, $return, $blocks);
157 if (!is_file($php) && !is_file($tpl)) {
158 throw new Exception("View {$file} doesn't exists");
161 if (!is_file($php) || filemtime($tpl) > filemtime($php)) {
162 if (!$compiler) {
163 $compiler = new Haanga_Compiler_Runtime;
165 $compiler->reset();
166 if (self::$debug) {
167 $compiler->setDebug($php.".dump");
170 //try {
171 $code = $compiler->compile_file($tpl, FALSE);
172 //} catch (Exception $e) {
173 // throw new Haanga_Compiler_Exception($tpl.' :'.$e->getMessage().' on '.$e->getfile(). ':'.$e->Getline());
176 file_put_contents($php, "<?php".$code);
177 self::$has_compiled = TRUE;
179 if (!is_callable($callback)) {
180 require_once $php;
183 return $callback($vars, $return, $blocks);
185 // }}}
190 * Local variables:
191 * tab-width: 4
192 * c-basic-offset: 4
193 * End:
194 * vim600: sw=4 ts=4 fdm=marker
195 * vim<600: sw=4 ts=4