From 4773d77611389a7afb7f366fa7eef5eab5cf8097 Mon Sep 17 00:00:00 2001 From: bzick Date: Sat, 28 Jun 2014 16:38:50 +0400 Subject: [PATCH] dev #83 --- src/Fenom/Compiler.php | 21 ++++++++++++++------- src/Fenom/Template.php | 16 ++++------------ tests/cases/Fenom/TemplateTest.php | 9 ++++++--- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index c81caef..026ab2b 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -138,13 +138,20 @@ class Compiler $key = null; $before = $body = array(); if ($tokens->is(T_VARIABLE)) { - $from = $scope->tpl->parseTerm($tokens); - $prepend = ""; + $from = $scope->tpl->parseTerm($tokens, $is_var); + 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('[')) { $from = $scope->tpl->parseArray($tokens); - $uid = '$v' . $scope->tpl->i++; - $prepend = $uid . ' = ' . $from . ';'; - $from = $uid; + $scope["var"] = $scope->tpl->tmpVar(); + $prepend = $scope["var"].' = (array)('.$from.')'; + $from = $check = $scope["var"]; } else { throw new UnexpectedTokenException($tokens, null, "tag {foreach}"); } @@ -189,9 +196,9 @@ class Compiler $body = $body ? implode("; ", $body) . ";" : ""; $scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : ""; 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 { - return "$prepend if($from) { $before foreach($from as $value) { $body"; + return "$prepend if($check) { $before foreach($from as $value) { $body"; } } diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index a2aca54..a4dfa71 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -1263,10 +1263,11 @@ class Template extends Render * [1, 2.3, 5+7/$var, 'string', "str {$var+3} ing", $var2, []] * * @param Tokenizer $tokens - * @throws UnexpectedTokenException + * @param int $count amount of elements + * @throws Error\UnexpectedTokenException * @return string */ - public function parseArray(Tokenizer $tokens) + public function parseArray(Tokenizer $tokens, &$count = 0) { if ($tokens->is("[")) { $_arr = "array("; @@ -1277,16 +1278,7 @@ class Template extends Render $key = true; $val = false; $_arr .= $tokens->getAndNext() . ' '; - } elseif ($tokens->is( - Tokenizer::MACRO_SCALAR, - T_VARIABLE, - T_STRING, - T_EMPTY, - T_ISSET, - "(", - "#" - ) && !$val - ) { + } elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(") && !$val) { $_arr .= $this->parseExpr($tokens); $key = false; $val = true; diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index b9ff1d2..8d36ea5 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -1267,12 +1267,15 @@ class TemplateTest extends TestCase ); } - public function _testSandbox() + /** + * @group sb + */ + public function testSandbox() { try { var_dump( - $this->fenom->setOptions(0)->compileCode( - "{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}" + $this->fenom->compileCode( + '{var $a = [3, 5,6]}' )->getBody() ); } catch (\Exception $e) {