This commit is contained in:
bzick 2014-06-28 16:38:50 +04:00
parent 7985c34c9c
commit 4773d77611
3 changed files with 24 additions and 22 deletions

View File

@ -138,13 +138,20 @@ class Compiler
$key = null; $key = null;
$before = $body = array(); $before = $body = array();
if ($tokens->is(T_VARIABLE)) { if ($tokens->is(T_VARIABLE)) {
$from = $scope->tpl->parseTerm($tokens); $from = $scope->tpl->parseTerm($tokens, $is_var);
$prepend = ""; if($is_var) {
$check = '!empty('.$from.')';
$prepend = "";
} else {
$scope["var"] = $scope->tpl->tmpVar();
$prepend = $scope["var"].' = (array)('.$from.')';
$from = $check = $scope["var"];
}
} elseif ($tokens->is('[')) { } elseif ($tokens->is('[')) {
$from = $scope->tpl->parseArray($tokens); $from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++; $scope["var"] = $scope->tpl->tmpVar();
$prepend = $uid . ' = ' . $from . ';'; $prepend = $scope["var"].' = (array)('.$from.')';
$from = $uid; $from = $check = $scope["var"];
} else { } else {
throw new UnexpectedTokenException($tokens, null, "tag {foreach}"); throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
} }
@ -189,9 +196,9 @@ class Compiler
$body = $body ? implode("; ", $body) . ";" : ""; $body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : ""; $scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
if ($key) { if ($key) {
return "$prepend if($from) { $before foreach($from as $key => $value) { $body"; return "$prepend if($check) { $before foreach($from as $key => $value) { $body";
} else { } else {
return "$prepend if($from) { $before foreach($from as $value) { $body"; return "$prepend if($check) { $before foreach($from as $value) { $body";
} }
} }

View File

@ -1263,10 +1263,11 @@ class Template extends Render
* [1, 2.3, 5+7/$var, 'string', "str {$var+3} ing", $var2, []] * [1, 2.3, 5+7/$var, 'string', "str {$var+3} ing", $var2, []]
* *
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @throws UnexpectedTokenException * @param int $count amount of elements
* @throws Error\UnexpectedTokenException
* @return string * @return string
*/ */
public function parseArray(Tokenizer $tokens) public function parseArray(Tokenizer $tokens, &$count = 0)
{ {
if ($tokens->is("[")) { if ($tokens->is("[")) {
$_arr = "array("; $_arr = "array(";
@ -1277,16 +1278,7 @@ class Template extends Render
$key = true; $key = true;
$val = false; $val = false;
$_arr .= $tokens->getAndNext() . ' '; $_arr .= $tokens->getAndNext() . ' ';
} elseif ($tokens->is( } elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(") && !$val) {
Tokenizer::MACRO_SCALAR,
T_VARIABLE,
T_STRING,
T_EMPTY,
T_ISSET,
"(",
"#"
) && !$val
) {
$_arr .= $this->parseExpr($tokens); $_arr .= $this->parseExpr($tokens);
$key = false; $key = false;
$val = true; $val = true;

View File

@ -1267,12 +1267,15 @@ class TemplateTest extends TestCase
); );
} }
public function _testSandbox() /**
* @group sb
*/
public function testSandbox()
{ {
try { try {
var_dump( var_dump(
$this->fenom->setOptions(0)->compileCode( $this->fenom->compileCode(
"{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}" '{var $a = [3, 5,6]}'
)->getBody() )->getBody()
); );
} catch (\Exception $e) { } catch (\Exception $e) {