static function generator($cmp, $args, $assign = NULL) { if (!$cmp->getOption('allow_exec')) { $cmp->Error("Tag exec is disabled for security reasons"); } $code = hcode(); if (Haanga_AST::is_var($args[0])) { $args[0] = $args[0]['var']; } else { if (Haanga_AST::is_str($args[0])) { $args[0] = $args[0]['string']; } else { $cmp->Error("invalid param"); } } $exec = hexec($args[0]); for ($i = 1; $i < count($args); $i++) { $exec->param($args[$i]); } $exec->end(); if ($assign) { $code->decl($assign, $exec); } else { $cmp->do_print($code, $exec); } return $code; }
/** * Sorted a nested array by '$sort_by' * property on each sub-array. , if you want * to see the original php file look filters/dictsort.php */ static function generator($cmp, $args, $redirected) { if (!$redirected) { $cmp->Error("dictsort must be redirected to a variable using AS <varname>"); } if (count($args) != 2) { $cmp->Error("Dictsort must have two params"); } if (!Haanga_AST::is_var($args[0])) { $cmp->Error("Dictsort: First parameter must be an array"); } $var = $cmp->get_context($args[0]['var']); $cmp->set_context($redirected, $var); $redirected = hvar($redirected); $field = hvar('field'); $key = hvar('key'); $code = hcode(); $body = hcode(); $body->decl(hvar('field', $key), hvar('item', $args[1])); $code->decl($redirected, $args[0]); $code->decl($field, array()); $code->do_foreach($redirected, 'item', $key, $body); $code->do_exec('array_multisort', $field, hconst('SORT_REGULAR'), $redirected); return $code; }
static function generator($compiler, $args) { $count = hexec('count', $args[0]); $strlen = hexec('strlen', $args[0]); $vars = hexec('count', hexec('get_object_vars', $args[0])); $guess = hexpr_cond(hexec('is_array', $args[0]), hexec('count', $args[0]), hexec('strlen', $args[0])); if (Haanga_AST::is_var($args[0])) { /* if it is a variable, best effort to detect its type at compile time */ $value = $compiler->get_context($args[0]['var']); if (is_array($value)) { return $count; } else { if (is_string($value)) { return $strlen; } else { if (is_object($value)) { return $vars; } else { return $gess; } } } } if (Haanga_AST::is_str($args[0])) { return $strlen; } return $guess; }
static function generator($cmp, $args) { foreach ($args as $arg) { if (Haanga_AST::is_var($arg)) { $cmp->set_safe($arg['var']); } } return hcode(); }
/** * firstof tag * */ static function generator($cmp, $args) { $count = count($args); $args = array_reverse($args); for ($i = 0; $i < $count; $i++) { if (isset($expr) && Haanga_AST::is_var($args[$i])) { $expr = hexpr_cond(hexpr(hexec('empty', $args[$i]), '==', FALSE), $args[$i], $expr); } else { $expr = $args[$i]; } } return $expr; }
static function generator($compiler, $args) { if (count($args) != 1) { $compiler->Error("templatetag only needs one parameter"); } if (Haanga_AST::is_var($args[0])) { $type = $args[0]['var']; if (!is_string($type)) { $compiler->Error("Invalid parameter"); } } else { if (Haanga_AST::is_str($args[0])) { $type = $args[0]['string']; } } switch ($type) { case 'openblock': $str = '{%'; break; case 'closeblock': $str = '%}'; break; case 'openbrace': $str = '{'; break; case 'closebrace': $str = '}'; break; case 'openvariable': $str = '{{'; break; case 'closevariable': $str = '}}'; break; case 'opencomment': $str = '{#'; break; case 'closecomment': $str = '#}'; break; default: $compiler->Error("Invalid parameter"); break; } $code = hcode(); $compiler->do_print($code, Haanga_AST::str($str)); return $code; }
static function generator($cmp, $args, $declared) { static $cycle = 0; if (!isset($cmp->cycle)) { $cmp->cycle = array(); } $code = hcode(); $index = 'index_' . $cycle; $def = 'def_cycle_' . $cycle; if (count($args) == 1 && Haanga_AST::is_var($args[0]) && isset($cmp->cycle[$args[0]['var']])) { $id = $cmp->cycle[$args[0]['var']]; $index = 'index_' . $id; $def = 'def_cycle_' . $id; } else { if (!$declared) { $code->do_if(hexpr(hexec('isset', hvar($def)), '==', FALSE)); } $code->decl($def, $args); if (!$declared) { $code->do_endif(); } } /* isset($var) == FALSE */ $expr = hexpr(hexec('isset', hvar($index)), '==', FALSE); $inc = hexpr(hexpr(hexpr(hvar($index), '+', 1)), '%', hexec('count', hvar($def))); if (!$declared) { if (isset($id)) { $code->decl($index, $inc); } else { $code->decl($index, hexpr_cond($expr, 0, $inc)); } $code->end(); $var = hvar($def, hvar($index)); $cmp->do_print($code, $var); } else { $code->decl($index, -1); $cmp->cycle[$declared] = $cycle; } $cycle++; return $code; }
static function generator($cmp, $args, $assign = NULL) { if (!$cmp->getOption('allow_exec')) { $cmp->Error("Tag exec is disabled for security reasons"); } $code = hcode(); if (Haanga_AST::is_var($args[0])) { $args[0] = $args[0]['var']; } else { if (Haanga_AST::is_str($args[0])) { $args[0] = $args[0]['string']; } else { $cmp->Error("invalid param"); } } // fix for static calls {{{ if (is_array($args[0])) { $end = end($args[0]); if (isset($end['class'])) { $args[0][key($args[0])]['class'] = substr($end['class'], 1); } } // }}} $exec = hexec($args[0]); for ($i = 1; $i < count($args); $i++) { $exec->param($args[$i]); } $exec->end(); if ($assign) { $code->decl($assign, $exec); // make it global $code->decl($cmp->getScopeVariable($assign), hvar($assign)); } else { $cmp->do_print($code, $exec); } return $code; }
/** * Generate needed code for custom tags (tags that aren't * handled by the compiler). * */ function generate_op_custom_tag($details, &$body) { static $tags; if (!$tags) { $tags = Haanga_Extension::getInstance('Tag'); } foreach ($details['list'] as $id => $arg) { if (Haanga_AST::is_var($arg)) { $details['list'][$id] = $this->generate_variable_name($arg['var']); } } $tag_name = $details['name']; $tagFunction = $tags->getFunctionAlias($tag_name); if (!$tagFunction && !$tags->hasGenerator($tag_name)) { $function = $this->get_custom_tag($tag_name, isset($details['as'])); } else { $function = $tagFunction; } if (isset($details['body'])) { /* if the custom tag has 'body' then it behave the same way as a filter */ $this->ob_start($body); $this->generate_op_code($details['body'], $body); $target = hvar('buffer' . $this->ob_start); if ($tags->hasGenerator($tag_name)) { $args = array_merge(array($target), $details['list']); $exec = $tags->generator($tag_name, $this, $args); if (!$exec instanceof Haanga_AST) { throw new Exception("Invalid output of custom filter {$tag_name}"); } if ($exec->stack_size() >= 2 || $exec->doesPrint) { /* The generator returned more than one statement, so we assume the output is already handled by one of those stmts. */ $body->append_ast($exec); $this->ob_start--; return; } } else { $exec = hexec($function, $target); } $this->ob_start--; $this->do_print($body, $exec); return; } $var = isset($details['as']) ? $details['as'] : NULL; $args = array_merge(array($function), $details['list']); if ($tags->hasGenerator($tag_name)) { $exec = $tags->generator($tag_name, $this, $details['list'], $var); if ($exec instanceof Haanga_AST) { if ($exec->stack_size() >= 2 || $exec->doesPrint || $var !== NULL) { /* The generator returned more than one statement, so we assume the output is already handled by one of those stmts. */ $body->append_ast($exec); return; } } else { throw new Exception("Invalid output of the custom tag {$tag_name}"); } } else { $fnc = array_shift($args); $exec = hexec($fnc); foreach ($args as $arg) { $exec->param($arg); } } if ($var) { $body->decl($var, $exec); } else { $this->do_print($body, $exec); } }
protected function generate_op_ifchanged($details, &$body) { static $ifchanged = 0; $ifchanged++; $var1 = 'ifchanged' . $ifchanged; if (!isset($details['check'])) { /* ugly */ $this->ob_start($body); $var2 = hvar('buffer' . $this->ob_start); $this->generate_op_code($details['body'], $body); $this->ob_start--; $body->do_if(hexpr(hexec('isset', hvar($var1)), '==', FALSE, '||', hvar($var1), '!=', $var2)); $this->do_print($body, $var2); $body->decl($var1, $var2); } else { /* beauty :-) */ foreach ($details['check'] as $id => $type) { if (!Haanga_AST::is_var($type)) { $this->Error("Unexpected string {$type['string']}, expected a varabile"); } $this_expr = hexpr(hexpr(hexec('isset', hvar($var1, $id)), '==', FALSE, '||', hvar($var1, $id), '!=', $type)); if (isset($expr)) { $this_expr = hexpr($expr, '&&', $this_expr); } $expr = $this_expr; } $body->do_if($expr); $this->generate_op_code($details['body'], $body); $body->decl($var1, $details['check']); } if (isset($details['else'])) { $body->do_else(); $this->generate_op_code($details['else'], $body); } $body->do_endif(); }