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 +---------------------------------------------------------------------------------+
42 protected static $block_var=NULL;
44 protected $forloop = array();
46 protected $sub_template = FALSE;
48 protected $check_function = FALSE;
49 protected $blocks=array();
53 * number of blocks :-)
55 protected $in_block=0;
59 protected $ob_start=0;
61 protected $prepend_op;
63 * Context at compile time
67 * Table which contains all variables
68 * aliases defined in the template
72 * Flag the current variable as safe. This means
73 * that escape won't be called if autoescape is
74 * activated (which is activated by default)
76 public $var_is_safe=FALSE;
79 /* compiler options */
80 static protected $autoescape = TRUE;
81 static protected $if_empty = TRUE;
82 static protected $dot_as_object = TRUE;
83 static protected $strip_whitespace = FALSE;
84 static protected $is_exec_enabled = FALSE;
85 static protected $global_context = array();
86 static protected $echo_concat = '.';
87 static protected $enable_load = TRUE;
95 function __construct()
97 $this->generator
= new Haanga_Generator_PHP
;
98 if (self
::$block_var===NULL) {
99 self
::$block_var = '{{block.'.md5('super').'}}';
103 // getOption($option) {{{
104 public static function getOption($option)
107 switch (strtolower($option)) {
109 $value = self
::$enable_load;
112 $value = self
::$if_empty;
115 $value = self
::$autoescape;
117 case 'dot_as_object':
118 $value = self
::$dot_as_object;
121 $value = self
::$echo_concat;
123 case 'strip_whitespace':
124 $value = self
::$strip_whitespace;
126 case 'is_exec_enabled':
128 $value = self
::$is_exec_enabled;
131 $value = self
::$global_context;
138 // setOption($option, $value) {{{
140 * Set Compiler option.
144 public static function setOption($option, $value)
146 switch (strtolower($option)) {
148 self
::$if_empty = (bool)$value;
151 self
::$enable_load = (bool)$value;
153 if ($value == '.' ||
$value == ',') {
154 self
::$echo_concat = $value;
159 self
::$autoescape = (bool)$value;
161 case 'dot_as_object':
162 self
::$dot_as_object = (bool)$value;
164 case 'strip_whitespace':
165 self
::$strip_whitespace = (bool)$value;
167 case 'is_exec_enabled':
169 self
::$is_exec_enabled = (bool)$value;
172 if (!is_array($value)) {
173 $value = array($value);
175 self
::$global_context = $value;
181 // setDebug($file) {{{
182 function setDebug($file)
184 $this->debug
= $file;
191 foreach (array_keys(get_object_vars($this)) as $key) {
192 if (isset($avoid_cleaning[$key])) {
197 $this->generator
= new Haanga_Generator_PHP
;
198 $this->blocks
= array();
199 $this->cycle
= array();
203 // get_template_name() {{{
204 final function get_template_name()
210 // Set template name {{{
211 function set_template_name($path)
213 $file = basename($path);
214 $pos = strpos($file,'.');
215 return ($this->name
= substr($file, 0, $pos));
219 // get_function_name(string $name) {{{
220 function get_function_name($name)
222 return "{$name}_template";
226 // Compile ($code, $name=NULL) {{{
227 final function compile($code, $name=NULL, $file=NULL)
231 if (count(self
::$global_context) > 0) {
232 /* add global variables (if any) to the current context */
233 foreach (self
::$global_context as $var) {
234 $this->set_context($var, $GLOBALS[$var]);
238 $parsed = Haanga_Compiler_Tokenizer
::init($code, $this, $file);
240 $this->subtemplate
= FALSE;
242 $body = new Haanga_AST
;
243 $this->prepend_op
= hcode();
245 if (isset($parsed[0]) && $parsed[0]['operation'] == 'base') {
246 /* {% base ... %} found */
247 $base = $parsed[0][0];
248 $code .= $this->get_base_template($base);
252 if (defined('HAANGA_VERSION')) {
253 $body->decl('HAANGA_VERSION', HAANGA_VERSION
);
257 $func_name = $this->get_function_name($name);
258 if ($this->check_function
) {
259 $body->do_if(hexpr(hexec('function_exists', $func_name), '===', FALSE));
261 if (!empty($this->file
)) {
262 $body->comment("Generated from ".$this->file
);
265 $body->declare_function($func_name);
267 if (count(self
::$global_context) > 0) {
268 $body->do_global(self
::$global_context);
272 $body->do_exec('extract', hvar('vars'));
273 $body->do_if(hexpr(hvar('return'), '==', TRUE));
274 $body->do_exec('ob_start');
278 $this->generate_op_code($parsed, $body);
279 if ($this->subtemplate
) {
280 $expr = $this->expr_call_base_template();
281 $this->do_print($body, $expr);
284 $body->do_if(hexpr(hvar('return'), '==', TRUE));
285 $body->do_return(hexec('ob_get_clean'));
289 $body->do_endfunction();
290 if ($this->check_function
) {
295 if ($this->prepend_op
->stack_size() > 0) {
296 $this->prepend_op
->append_ast($body);
297 $body = $this->prepend_op
;
300 $op_code = $body->getArray(TRUE);
303 $code .= $this->generator
->getCode($op_code);
304 if (!empty($this->append
)) {
305 $code .= $this->append
;
308 if (!empty($this->debug
)) {
309 $op_code['php'] = $code;
310 file_put_contents($this->debug
, print_r($op_code, TRUE), LOCK_EX
);
316 // compile_file($file) {{{
320 * @param string $file File path
321 * @param bool $safe Whether or not add check if the function is already defined
323 * @return Generated PHP code
325 final function compile_file($file, $safe=FALSE, $context=array())
327 if (!is_readable($file)) {
328 throw new Haanga_Compiler_Exception("$file is not a file");
331 $this->_base_dir
= dirname($file);
332 $this->file
= realpath($file);
334 $this->check_function
= $safe;
335 $this->context
= $context;
336 $name = $this->set_template_name($file);
337 return $this->compile(file_get_contents($file), $name, $file);
341 // getOpCodes($code, $file='') {{{
343 * Compile the $code and return the "opcodes"
344 * (the Abstract syntax tree).
346 * @param string $code Template content
347 * @param string $file File path (used for erro reporting)
352 public function getOpCodes($code, $file)
354 $oldfile = $this->file
;
356 $parsed = Haanga_Compiler_Tokenizer
::init($code, $this, $file);
357 $body = new Haanga_AST
;
358 if (isset($parsed[0]) && $parsed[0]['operation'] == 'base') {
359 $this->Error("{% base is not supported on inlines %}");
361 $body = new Haanga_AST
;
362 $this->generate_op_code($parsed, $body);
363 $this->file
= $oldfile;
368 // Error($errtxt) {{{
370 * Throw an exception and appends information about the template (the path and
371 * the last processed line).
375 public function Error($err)
377 throw new Haanga_Compiler_Exception("{$err} in {$this->file}:$this->line");
381 // is_expr methods {{{
382 function is_var_filter($cmd)
384 return isset($cmd['var_filter']);
389 // expr_call_base_template() {{{
391 * Generate code to call base template
394 function expr_call_base_template()
397 $this->get_function_name($this->subtemplate
),
404 // get_base_template($base) {{{
406 * Handle {% base "" %} definition. By default only
407 * static (string) are supported, but this can be overrided
410 * This method load the base class, compile it and return
411 * the generated code.
413 * @param array $base Base structure
415 * @return string Generated source code
417 function get_base_template($base)
419 if (!Haanga_AST
::is_str($base)) {
420 $this->Error("Dynamic inheritance is not supported for compilated templates");
422 $file = $base['string'];
423 list($this->subtemplate
, $new_code) = $this->compile_required_template($file);
424 return $new_code."\n\n";
428 // {% base "foo.html" %} {{{
429 protected function generate_op_base()
431 $this->Error("{% base %} can be only as first statement");
436 protected function generate_op_code($parsed, &$body)
438 if (!is_array($parsed)) {
439 $this->Error("Invalid \$parsed array");
441 foreach ($parsed as $op) {
442 if (!is_array($op)) {
445 if (!isset($op['operation'])) {
446 $this->Error("Malformed array:".print_r($op, TRUE));
448 if (isset($op['line'])) {
449 $this->line
= $op['line'];
452 if ($this->subtemplate
&& $this->in_block
== 0 && $op['operation'] != 'block') {
453 /* ignore most of tokens in subtemplates */
457 $method = "generate_op_".$op['operation'];
458 if (!is_callable(array($this, $method))) {
459 $this->Error("Compiler: Missing method $method");
461 $this->$method($op, $body);
466 // Check the current expr {{{
467 protected function check_expr(&$expr)
469 if (Haanga_AST
::is_expr($expr)) {
470 if ($expr['op_expr'] == 'in') {
471 for ($id=0; $id < 2; $id++
) {
472 if ($this->is_var_filter($expr[$id])) {
473 $expr[$id] = $this->get_filtered_var($expr[$id]['var_filter'], $var);
476 if (Haanga_AST
::is_str($expr[1])) {
477 $expr = hexpr(hexec('strpos', $expr[1], $expr[0]), '!==', FALSE);
481 hexec('is_array', $expr[1]),
482 hexec('array_search', $expr[0], $expr[1]),
483 hexec('strpos', $expr[1], $expr[0])
489 if (is_object($expr)) {
490 $expr = $expr->getArray();
492 $this->check_expr($expr[0]);
493 $this->check_expr($expr[1]);
494 } else if (is_array($expr)) {
495 if ($this->is_var_filter($expr)) {
496 $expr = $this->get_filtered_var($expr['var_filter'], $var);
497 } else if (isset($expr['args'])) {
498 /* check every arguments */
499 foreach ($expr['args'] as &$v) {
500 $this->check_expr($v);
503 } else if (isset($expr['expr_cond'])) {
504 /* Check expr conditions */
505 $this->check_expr($expr['expr_cond']);
506 $this->check_expr($expr['true']);
507 $this->check_expr($expr['false']);
513 // ifequal|ifnot equal <var_filtered|string|number> <var_fitlered|string|number> ... else ... {{{
514 protected function generate_op_ifequal($details, &$body)
516 $if['expr'] = hexpr($details[1], $details['cmp'], $details[2])->getArray();
517 $if['body'] = $details['body'];
518 if (isset($details['else'])) {
519 $if['else'] = $details['else'];
521 $this->generate_op_if($if, $body);
525 // {% if <expr> %} HTML {% else %} TWO {% endif $} {{{
526 protected function generate_op_if($details, &$body)
528 if (self
::$if_empty && $this->is_var_filter($details['expr']) && count($details['expr']['var_filter']) == 1) {
529 /* if we are doing if <Variable> it should check
530 if it exists without throw any warning */
531 $expr = $details['expr'];
532 $expr['var_filter'][] = 'empty';
534 $variable = $this->get_filtered_var($expr['var_filter'], $var);
536 $details['expr'] = hexpr($variable, '===', FALSE)->getArray();
538 $this->check_expr($details['expr']);
539 $expr = Haanga_AST
::fromArrayGetAST($details['expr']);
541 $this->generate_op_code($details['body'], $body);
542 if (isset($details['else'])) {
544 $this->generate_op_code($details['else'], $body);
550 // Override template {{{
551 protected function compile_required_template($file)
553 if (!is_file($file)) {
554 if (isset($this->_base_dir
)) {
555 $file = $this->_base_dir
.'/'.$file;
558 if (!is_file($file)) {
559 $this->Error("can't find {$file} file template");
561 $class = get_class($this);
564 $code = $comp->compile_file($file, $this->check_function
);
565 return array($comp->get_template_name(), $code);
569 // include "file.html" | include <var1> {{{
570 protected function generate_op_include($details, &$body)
572 if (!$details[0]['string']) {
573 $this->Error("Dynamic inheritance is not supported for compilated templates");
575 list($name,$code) = $this->compile_required_template($details[0]['string']);
576 $this->append
.= "\n\n{$code}";
577 $this->do_print($body,
578 hexec($this->get_function_name($name),
579 hvar('vars'), TRUE, hvar('blocks'))
584 // Handle HTML code {{{
585 protected function generate_op_html($details, &$body)
587 $string = Haanga_AST
::str($details['html']);
588 $this->do_print($body, $string);
592 // get_var_filtered {{{
594 * This method handles all the filtered variable (piped_list(X)'s
595 * output in the parser.
598 * @param array $variable (Output of piped_list(B) (parser))
599 * @param array &$varname Variable name
600 * @param bool $accept_string TRUE is string output are OK (ie: block.parent)
605 function get_filtered_var($variable, &$varname, $accept_string=FALSE)
607 $this->var_is_safe
= FALSE;
609 if (count($variable) > 1) {
610 $count = count($variable);
611 $target = $this->generate_variable_name($variable[0]);
613 if (!Haanga_AST
::is_var($target)) {
614 /* block.super can't have any filter */
615 $this->Error("This variable can't have any filter");
618 for ($i=1; $i < $count; $i++
) {
619 $func_name = $variable[$i];
620 if ($func_name == 'escape') {
621 /* to avoid double cleaning */
622 $this->var_is_safe
= TRUE;
624 $args = array(isset($exec) ?
$exec : $target);
625 $exec = $this->do_filtering($func_name, $args);
631 $details = $this->generate_variable_name($variable[0]);
632 $varname = $variable[0];
634 if (!Haanga_AST
::is_var($details) && !$accept_string) {
635 /* generate_variable_name didn't replied a variable, weird case
636 currently just used for {{block.super}}.
638 $this->Error("Invalid variable name {$variable[0]}");
646 // generate_op_print_var {{{
648 * Generate code to print a variable with its filters, if there is any.
650 * All variable (except those flagged as |safe) are automatically
651 * escaped if autoescape is "on".
654 protected function generate_op_print_var($details, &$body)
657 $details = $this->get_filtered_var($details['variable'], $variable, TRUE);
659 if (!Haanga_AST
::is_var($details) && !Haanga_AST
::is_exec($details)) {
660 /* generate_variable_name didn't replied a variable, weird case
661 currently just used for {{block.super}}.
663 $this->do_print($body, $details);
667 if (!$this->is_safe($details) && self
::$autoescape) {
668 $args = array($details);
669 $details = $this->do_filtering('escape', $args);
673 if (is_array($details)) {
674 $details = Haanga_AST
::fromArrayGetAST($details);
676 $this->do_print($body, $details);
680 // {# something #} {{{
681 protected function generate_op_comment($details, &$body)
683 /* comments are annoying */
684 //$body->comment($details['comment']);
688 // {% block 'name' %} ... {% endblock %} {{{
689 protected function generate_op_block($details, &$body)
691 if (is_array($details['name'])) {
693 foreach ($details['name'] as $part) {
694 if (is_string($part)) {
696 } else if (is_array($part)) {
697 if (Haanga_AST
::is_str($part)) {
698 $name .= "{$part['string']}";
699 } elseif (isset($part['object'])) {
700 $name .= "{$part['object']}";
702 $this->Error("Invalid blockname");
707 $details['name'] = substr($name, 0, -1);
710 $this->blocks
[] = $details['name'];
711 $block_name = hvar('blocks', $details['name']);
713 $this->ob_start($body);
714 $buffer_var = 'buffer'.$this->ob_start
;
717 $this->generate_op_code($details['body'], $content);
719 $body->append_ast($content);
722 $buffer = hvar($buffer_var);
726 * isset previous block (parent block)?
728 * has reference to self::$block_var ?
730 * replace self::$block_var for current block value (buffer)
734 * print current block
737 $declare = hexpr_cond(
738 hexec('isset', $block_name),
740 hexpr(hexec('strpos', $block_name, self
::$block_var), '===', FALSE),
742 hexec('str_replace', self
::$block_var, $buffer, $block_name)
746 if (!$this->subtemplate
) {
747 $this->do_print($body, $declare);
749 $body->decl($block_name, $declare);
750 if ($this->in_block
> 1) {
751 $this->do_print($body, $block_name);
754 array_pop($this->blocks
);
760 // regroup <var1> by <field> as <foo> {{{
761 protected function generate_op_regroup($details, &$body)
763 $body->comment("Temporary sorting");
765 $array = $this->get_filtered_var($details['array'], $varname);
767 if (Haanga_AST
::is_exec($array)) {
768 $varname = hvar($details['as']);
769 $body->decl($varname, $array);
771 $var = hvar('item', $details['row']);
773 $body->decl('temp_group', array());
775 $body->do_foreach($varname, 'item', NULL,
776 hcode()->decl(hvar('temp_group', $var, NULL), hvar('item'))
779 $body->comment("Proper format");
780 $body->decl($details['as'], array());
781 $body->do_foreach('temp_group', 'item', 'group',
783 hvar($details['as'], NULL),
784 array("grouper" => hvar('group'), "list" => hvar('item'))
787 $body->comment("Sorting done");
791 // variable context {{{
795 * These two functions are useful to detect if a variable
796 * separated by dot (foo.bar) is an array or object. To avoid
797 * overhead we decide it at compile time, rather than
798 * ask over and over at rendering time.
801 * + If foo exists at compile time,
802 * and it is an array, it would be foo['bar']
803 * otherwise it'd be foo->bar.
804 * + If foo don't exists at compile time,
805 * it would be foo->bar if the compiler option
806 * dot_as_object is TRUE (by default) otherwise
810 * @author gallir (ideas)
813 function set_context($varname, $value)
815 $this->context
[$varname] = $value;
818 function get_context($variable)
820 if (!is_array($variable)) {
821 $variable = array($variable);
823 $varname = $variable[0];
824 if (isset($this->context
[$varname])) {
825 if (count($variable) == 1) {
826 return $this->context
[$varname];
828 $var = & $this->context
[$varname];
829 foreach ($variable as $id => $part) {
831 if (is_array($part) && isset($part['object'])) {
832 if (is_array($part['object']) && isset($part['object']['var'])) {
833 /* object $foo->$bar */
834 $name = $part['object']['var'];
835 $name = $this->get_context($name);
836 if (!isset($var->$name)) {
841 if (!isset($var->$part['object'])) {
844 $var = &$var->$part['object'];
846 } else if (is_object($var)) {
847 if (!isset($var->$part)) {
852 if (!is_scalar($part) ||
empty($part) ||
!isset($var[$part])) {
867 function var_is_object(Array $variable, $default=NULL)
869 $varname = $variable[0];
882 return FALSE; /* these are arrays */
885 $variable = $this->get_context($variable);
886 if (is_array($variable) ||
is_object($variable)) {
887 return $default ?
is_object($variable) : is_object($variable) && !$variable InstanceOf Iterator
&& !$variable Instanceof ArrayAccess
;
890 return $default===NULL ? self
::$dot_as_object : $default;
894 // Get variable name {{{
895 function generate_variable_name($variable, $special=true)
897 if (is_array($variable)) {
898 switch ($variable[0]) {
901 return array('var' => $variable);
904 $this->Error("Invalid forloop reference outside of a loop");
907 switch ($variable[1]['object']) {
909 $this->forloop
[$this->forid
]['counter'] = TRUE;
910 $variable = 'forcounter1_'.$this->forid
;
913 $this->forloop
[$this->forid
]['counter0'] = TRUE;
914 $variable = 'forcounter0_'.$this->forid
;
917 $this->forloop
[$this->forid
]['counter'] = TRUE;
918 $this->forloop
[$this->forid
]['last'] = TRUE;
919 $variable = 'islast_'.$this->forid
;
922 $this->forloop
[$this->forid
]['first'] = TRUE;
923 $variable = 'isfirst_'.$this->forid
;
926 $this->forloop
[$this->forid
]['revcounter'] = TRUE;
927 $variable = 'revcount_'.$this->forid
;
930 $this->forloop
[$this->forid
]['revcounter0'] = TRUE;
931 $variable = 'revcount0_'.$this->forid
;
936 $variable = $this->generate_variable_name(array_values($variable));
937 $variable = $variable['var'];
941 $this->Error("Unexpected forloop.{$variable[1]}");
943 /* no need to escape it */
944 $this->var_is_safe
= TRUE;
948 return array('var' => $variable);
950 if ($this->in_block
== 0) {
951 $this->Error("Can't use block.super outside a block");
953 if (!$this->subtemplate
) {
954 $this->Error("Only subtemplates can call block.super");
956 /* no need to escape it */
957 $this->var_is_safe
= TRUE;
958 return Haanga_AST
::str(self
::$block_var);
961 /* choose array or objects */
964 // this section is resolved on the parser.y
965 return array('var' => $variable);
968 for ($i=1; $i < count($variable); $i++
) {
969 $var_part = array_slice($variable, 0, $i);
972 if (is_array($variable[$i])) {
973 if (isset($variable[$i]['class'])) {
974 // no type guess for static properties
977 if (isset($variable[$i]['object'])) {
980 if (!Haanga_AST
::is_var($variable[$i])) {
981 $variable[$i] = current($variable[$i]);
983 $variable[$i] = $this->generate_variable_name($variable[$i]['var']);
987 $is_obj = $this->var_is_object($var_part, 'unknown');
989 if ( $is_obj === TRUE ||
($is_obj == 'unknown' && !$def_arr)) {
990 $variable[$i] = array('object' => $variable[$i]);
997 } else if (isset($this->var_alias
[$variable])) {
998 $variable = $this->var_alias
[$variable]['var'];
1001 return hvar($variable)->getArray();
1006 public function do_print(Haanga_AST
$code, $stmt)
1008 /* Flag this object as a printing one */
1009 $code->doesPrint
= TRUE;
1011 if (self
::$strip_whitespace && Haanga_AST
::is_str($stmt)) {
1012 $stmt['string'] = preg_replace('/\s+/', ' ', $stmt['string']);
1015 if ($this->ob_start
== 0) {
1016 $code->do_echo($stmt);
1020 $buffer = hvar('buffer'.$this->ob_start
);
1021 $code->append($buffer, $stmt);
1027 // for [<key>,]<val> in <array> {{{
1028 protected function generate_op_loop($details, &$body)
1030 if (isset($details['empty'])) {
1031 $body->do_if(hexpr(hexec('count', hvar($details['array'])), '==', 0));
1032 $this->generate_op_code($details['empty'], $body);
1037 $oldid = $this->forid
;
1038 $this->forid
= $oldid+
1;
1039 $this->forloop
[$this->forid
] = array();
1041 if (isset($details['range'])) {
1042 $this->set_safe($details['variable']);
1044 /* check variable context */
1046 /* Check if the array to iterate is an object */
1047 $var = &$details['array'][0];
1048 if (is_string($var) && $this->var_is_object(array($var), FALSE)) {
1049 /* It is an object, call to get_object_vars */
1050 $body->decl($var.'_arr', hexec('get_object_vars', hvar($var)));
1055 $array = $this->get_filtered_var($details['array'], $varname);
1058 if ($this->is_safe(hvar($varname))) {
1059 $this->set_safe(hvar($details['variable']));
1062 if ($array Instanceof Haanga_AST
) {
1064 $tmp = hvar('tmp'.($oldid+
1));
1065 $body->decl($tmp, $array);
1068 $details['array'] = $array;
1072 $for_body = hcode();
1073 $this->generate_op_code($details['body'], $for_body);
1076 $oid = $this->forid
;
1077 $size = hvar('psize_'.$oid);
1080 if (isset($this->forloop
[$oid]['counter'])) {
1081 $var = hvar('forcounter1_'.$oid);
1082 $body->decl($var, 1);
1083 $for_body->decl($var, hexpr($var, '+', 1));
1088 if (isset($this->forloop
[$oid]['counter0'])) {
1089 $var = hvar('forcounter0_'.$oid);
1090 $body->decl($var, 0);
1091 $for_body->decl($var, hexpr($var, '+', 1));
1096 if (isset($this->forloop
[$oid]['last'])) {
1098 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1101 $var = 'islast_'.$oid;
1102 $body->decl($var, hexpr(hvar('forcounter1_'.$oid), '==', $size));
1103 $for_body->decl($var, hexpr(hvar('forcounter1_'.$oid), '==', $size));
1108 if (isset($this->forloop
[$oid]['first'])) {
1109 $var = hvar('isfirst_'.$oid);
1110 $body->decl($var, TRUE);
1111 $for_body->decl($var, FALSE);
1116 if (isset($this->forloop
[$oid]['revcounter'])) {
1118 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1121 $var = hvar('revcount_'.$oid);
1122 $body->decl($var, $size);
1123 $for_body->decl($var, hexpr($var, '-', 1));
1128 if (isset($this->forloop
[$oid]['revcounter0'])) {
1130 $body->decl('psize_'.$oid, hexec('count', hvar_ex($details['array'])));
1133 $var = hvar('revcount0_'.$oid);
1134 $body->decl($var, hexpr($size, "-", 1));
1135 $for_body->decl($var, hexpr($var, '-', 1));
1141 /* Restore old ForID */
1142 $this->forid
= $oldid;
1144 /* Merge loop body */
1145 if (!isset($details['range'])) {
1146 $body->do_foreach($array, $details['variable'], $details['index'], $for_body);
1148 if ($this->is_safe(hvar($varname))) {
1149 $this->set_unsafe($details['variable']);
1152 for ($i=0; $i < 2; $i++
) {
1153 if (Haanga_AST
::is_var($details['range'][$i])) {
1154 $details['range'][$i] = $this->generate_variable_name($details['range'][$i]['var']);
1158 if (Haanga_AST
::is_var($details['step'])) {
1159 $details['step'] = $this->generate_variable_name($details['step']['var']);
1161 $body->do_for($details['variable'], $details['range'][0], $details['range'][1], $details['step'], $for_body);
1162 $this->set_unsafe(hvar($details['variable']));
1165 if (isset($details['empty'])) {
1171 function generate_op_set($details, &$body)
1173 $var = $this->generate_variable_name($details['var']);
1174 $this->check_expr($details['expr']);
1175 $body->decl_raw($var, $details['expr']);
1176 $body->decl(hvar('vars', $var['var']), $var);
1180 // ifchanged [<var1> <var2] {{{
1181 protected function generate_op_ifchanged($details, &$body)
1183 static $ifchanged = 0;
1186 $var1 = 'ifchanged'.$ifchanged;
1187 if (!isset($details['check'])) {
1189 $this->ob_start($body);
1190 $var2 = hvar('buffer'.$this->ob_start
);
1193 $this->generate_op_code($details['body'], $body);
1195 $body->do_if(hexpr(hexec('isset', hvar($var1)), '==', FALSE, '||', hvar($var1), '!=', $var2));
1196 $this->do_print($body, $var2);
1197 $body->decl($var1, $var2);
1200 foreach ($details['check'] as $id=>$type) {
1201 if (!Haanga_AST
::is_var($type)) {
1202 $this->Error("Unexpected string {$type['string']}, expected a varabile");
1205 $this_expr = hexpr(hexpr(
1206 hexec('isset', hvar($var1, $id)), '==', FALSE,
1207 '||', hvar($var1, $id), '!=', $type
1211 $this_expr = hexpr($expr, '&&', $this_expr);
1217 $body->do_if($expr);
1218 $this->generate_op_code($details['body'], $body);
1219 $body->decl($var1, $details['check']);
1222 if (isset($details['else'])) {
1224 $this->generate_op_code($details['else'], $body);
1230 // autoescape ON|OFF {{{
1231 function generate_op_autoescape($details, &$body)
1233 $old_autoescape = self
::$autoescape;
1234 self
::$autoescape = strtolower($details['value']) == 'on';
1235 $this->generate_op_code($details['body'], $body);
1236 self
::$autoescape = $old_autoescape;
1240 // {% spacefull %} Set to OFF strip_whitespace for a block (the compiler option) {{{
1241 function generate_op_spacefull($details, &$body)
1243 $old_strip_whitespace = self
::$strip_whitespace;
1244 self
::$strip_whitespace = FALSE;
1245 $this->generate_op_code($details['body'], $body);
1246 self
::$strip_whitespace = $old_strip_whitespace;
1250 // ob_Start(array &$body) {{{
1252 * Start a new buffering
1255 function ob_start(&$body)
1258 $body->decl('buffer'.$this->ob_start
, "");
1263 function get_custom_tag($name)
1265 $function = $this->get_function_name($this->name
).'_tag_'.$name;
1266 $this->append
.= "\n\n".Haanga_Extension
::getInstance('Tag')->getFunctionBody($name, $function);
1271 * Generate needed code for custom tags (tags that aren't
1272 * handled by the compiler).
1275 function generate_op_custom_tag($details, &$body)
1279 $tags = Haanga_Extension
::getInstance('Tag');
1282 foreach ($details['list'] as $id => $arg) {
1283 if (Haanga_AST
::is_var($arg)) {
1284 $details['list'][$id] = $this->generate_variable_name($arg['var']);
1289 $tag_name = $details['name'];
1290 $tagFunction = $tags->getFunctionAlias($tag_name);
1292 if (!$tagFunction && !$tags->hasGenerator($tag_name)) {
1293 $function = $this->get_custom_tag($tag_name, isset($details['as']));
1295 $function = $tagFunction;
1297 if (isset($details['body'])) {
1299 if the custom tag has 'body'
1300 then it behave the same way as a filter
1302 $this->ob_start($body);
1303 $this->generate_op_code($details['body'], $body);
1304 $target = hvar('buffer'.$this->ob_start
);
1305 if ($tags->hasGenerator($tag_name)) {
1306 $args = array_merge(array($target), $details['list']);
1307 $exec = $tags->generator($tag_name, $this, $args);
1308 if (!$exec InstanceOf Haanga_AST
) {
1309 $this->Error("Invalid output of custom filter {$tag_name}");
1311 if ($exec->stack_size() >= 2 ||
$exec->doesPrint
) {
1313 The generator returned more than one statement,
1314 so we assume the output is already handled
1315 by one of those stmts.
1317 $body->append_ast($exec);
1322 $exec = hexec($function, $target);
1325 $this->do_print($body, $exec);
1329 $var = isset($details['as']) ?
$details['as'] : NULL;
1330 $args = array_merge(array($function), $details['list']);
1332 if ($tags->hasGenerator($tag_name)) {
1333 $exec = $tags->generator($tag_name, $this, $details['list'], $var);
1334 if ($exec InstanceOf Haanga_AST
) {
1335 if ($exec->stack_size() >= 2 ||
$exec->doesPrint ||
$var !== NULL) {
1337 The generator returned more than one statement,
1338 so we assume the output is already handled
1339 by one of those stmts.
1341 $body->append_ast($exec);
1345 $this->Error("Invalid output of the custom tag {$tag_name}");
1348 $fnc = array_shift($args);
1349 $exec = hexec($fnc);
1350 foreach ($args as $arg) {
1356 $body->decl($var, $exec);
1358 $this->do_print($body, $exec);
1363 // with <variable> as <var> {{{
1368 function generate_op_alias($details, &$body)
1370 $this->var_alias
[ $details['as'] ] = $this->generate_variable_name($details['var']);
1371 $this->generate_op_code($details['body'], $body);
1372 unset($this->var_alias
[ $details['as'] ] );
1376 // Custom Filters {{{
1377 function get_custom_filter($name)
1379 $function = $this->get_function_name($this->name
).'_filter_'.$name;
1380 $this->append
.= "\n\n".Haanga_Extension
::getInstance('Filter')->getFunctionBody($name, $function);
1385 function do_filtering($name, $args)
1389 $filter = Haanga_Extension
::getInstance('Filter');
1392 if (is_array($name)) {
1394 prepare array for ($func_name, $arg1, $arg2 ... )
1395 where $arg1 = last expression and $arg2.. $argX is
1396 defined in the template
1398 $args = array_merge($args, $name['args']);
1402 if (!$filter->isValid($name)) {
1403 $this->Error("{$name} is an invalid filter");
1406 if ($filter->isSafe($name)) {
1407 /* check if the filter is return HTML-safe data (to avoid double scape) */
1408 $this->var_is_safe
= TRUE;
1412 if ($filter->hasGenerator($name)) {
1413 return $filter->generator($name, $this, $args);
1415 $fnc = $filter->getFunctionAlias($name);
1417 $fnc = $this->get_custom_filter($name);
1420 $args = array_merge(array($fnc), $args);
1421 $exec = call_user_func_array('hexec', $args);
1426 function generate_op_filter($details, &$body)
1428 $this->ob_start($body);
1429 $this->generate_op_code($details['body'], $body);
1430 $target = hvar('buffer'.$this->ob_start
);
1431 foreach ($details['functions'] as $f) {
1432 $param = (isset($exec) ?
$exec : $target);
1433 $exec = $this->do_filtering($f, array($param));
1436 $this->do_print($body, $exec);
1440 /* variable safety {{{ */
1441 function set_safe($name)
1443 if (!Haanga_AST
::is_Var($name)) {
1444 $name = hvar($name)->getArray();
1446 $this->safes
[serialize($name)] = TRUE;
1449 function set_unsafe($name)
1451 if (!Haanga_AST
::is_Var($name)) {
1452 $name = hvar($name)->getArray();
1454 unset($this->safes
[serialize($name)]);
1457 function is_safe($name)
1459 if ($this->var_is_safe
) {
1462 if (isset($this->safes
[serialize($name)])) {
1469 final static function main_cli()
1471 $argv = $GLOBALS['argv'];
1472 $haanga = new Haanga_Compiler
;
1473 $code = $haanga->compile_file($argv[1], TRUE);
1474 if (!isset($argv[2]) ||
$argv[2] != '--notags') {
1475 $code = "<?php\n\n$code";
1487 * vim600: sw=4 ts=4 fdm=marker
1488 * vim<600: sw=4 ts=4