From d307c808a0fb4bcf8aa3aa9bd1bea9369649ce74 Mon Sep 17 00:00:00 2001 From: klkvsk Date: Thu, 11 Jul 2013 14:09:16 +0400 Subject: [PATCH 1/6] fix parsing of array param in function (was falling in endless loop) --- src/Fenom/Template.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index b27b822..f67cb78 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -1126,7 +1126,11 @@ class Template extends Render { } if($tokens->is("=")) { $tokens->next(); - $params[ $key ] = $this->parseExp($tokens); + if ($tokens->is('[')) { + $params[ $key ] = $this->parseArray($tokens); + } else { + $params[ $key ] = $this->parseExp($tokens); + } } else { $params[ $key ] = 'true'; } From 803fd670cd4b8b3c4fd324725692e51a1e4fe2b0 Mon Sep 17 00:00:00 2001 From: klkvsk Date: Thu, 11 Jul 2013 14:13:40 +0400 Subject: [PATCH 2/6] fix use var_export for default params in function (nulls and empty arrays broke templates) --- src/Fenom/Compiler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fenom/Compiler.php b/src/Fenom/Compiler.php index d27361b..4fa8d9e 100644 --- a/src/Fenom/Compiler.php +++ b/src/Fenom/Compiler.php @@ -615,7 +615,7 @@ class Compiler { } elseif(isset($params[ $param->getPosition() ])) { $args[] = $params[ $param->getPosition() ]; } elseif($param->isOptional()) { - $args[] = $param->getDefaultValue(); + $args[] = var_export($param->getDefaultValue(), true); } } return "$function(".implode(", ", $args).')'; From eb1fa831743d2dca729c2d297237761f702bdcde Mon Sep 17 00:00:00 2001 From: klkvsk Date: Thu, 11 Jul 2013 14:14:34 +0400 Subject: [PATCH 3/6] add tests for custom functions in template --- tests/cases/Fenom/FunctionsTest.php | 64 +++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/cases/Fenom/FunctionsTest.php diff --git a/tests/cases/Fenom/FunctionsTest.php b/tests/cases/Fenom/FunctionsTest.php new file mode 100644 index 0000000..888a40b --- /dev/null +++ b/tests/cases/Fenom/FunctionsTest.php @@ -0,0 +1,64 @@ +fenom->addFunctionSmart('sum', __CLASS__ . '::functionSum'); + $this->fenom->addFunctionSmart('pow', __CLASS__ . '::functionPow'); + $this->fenom->addFunctionSmart('inc', __CLASS__ . '::functionInc'); + + $this->tpl('function_params_scalar.tpl', '{pow a=2 n=3}'); + $this->tpl('function_params_dynamic.tpl', '{pow a=$a n=$n}'); + $this->tpl('function_default_param_scalar.tpl', '{pow a=2}'); + $this->tpl('function_default_param_empty_array.tpl', '{sum}'); + $this->tpl('function_default_param_const.tpl', '{inc a=1}'); + $this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}'); + } + + public function testFunctionWithParams() { + $output = $this->fenom->fetch('function_params_scalar.tpl'); + $this->assertEquals('8', $output); + } + + public function testFunctionWithDynamicParams() { + $output = $this->fenom->fetch('function_params_dynamic.tpl', array('a' => 3, 'n' => 4)); + $this->assertEquals('81', $output); + } + + public function testFunctionWithDefaultParamScalar() { + $output = $this->fenom->fetch('function_default_param_scalar.tpl'); + $this->assertEquals('4', $output); + } + + public function testFunctionWithDefaultParamArray() { + $output = $this->fenom->fetch('function_default_param_empty_array.tpl'); + $this->assertEquals('0', $output); + } + + public function testFunctionWithDefaultParamConst() { + $output = $this->fenom->fetch('function_default_param_const.tpl'); + $this->assertEquals('2', $output); + } + + public function testFunctionWithArrayParam() { + $output = $this->fenom->fetch('function_array_param.tpl'); + $this->assertEquals('15', $output); + } + +} From a2b5b43cb122878c478e54a42de76ba649d355c9 Mon Sep 17 00:00:00 2001 From: klkvsk Date: Thu, 11 Jul 2013 16:13:11 +0400 Subject: [PATCH 4/6] proper fix for array params (now works with both positional and named params), tests --- src/Fenom/Template.php | 12 ++++++------ tests/cases/Fenom/FunctionsTest.php | 10 ++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index f67cb78..c606773 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -644,7 +644,11 @@ class Template extends Render { } else { break; } - } else { + + } elseif($tokens->is('[')) { + $_exp .= $this->parseArray($tokens); + + } else { break; } } @@ -1126,11 +1130,7 @@ class Template extends Render { } if($tokens->is("=")) { $tokens->next(); - if ($tokens->is('[')) { - $params[ $key ] = $this->parseArray($tokens); - } else { - $params[ $key ] = $this->parseExp($tokens); - } + $params[ $key ] = $this->parseExp($tokens); } else { $params[ $key ] = 'true'; } diff --git a/tests/cases/Fenom/FunctionsTest.php b/tests/cases/Fenom/FunctionsTest.php index 888a40b..755a1e0 100644 --- a/tests/cases/Fenom/FunctionsTest.php +++ b/tests/cases/Fenom/FunctionsTest.php @@ -29,7 +29,8 @@ class FunctionsTest extends TestCase { $this->tpl('function_default_param_empty_array.tpl', '{sum}'); $this->tpl('function_default_param_const.tpl', '{inc a=1}'); $this->tpl('function_array_param.tpl', '{sum of=[1, 2, 3, 4, 5]}'); - } + $this->tpl('function_array_param_pos.tpl', '{sum [1, 2, 3, 4, 5]}'); + } public function testFunctionWithParams() { $output = $this->fenom->fetch('function_params_scalar.tpl'); @@ -56,9 +57,14 @@ class FunctionsTest extends TestCase { $this->assertEquals('2', $output); } - public function testFunctionWithArrayParam() { + public function testFunctionWithArrayNamedParam() { $output = $this->fenom->fetch('function_array_param.tpl'); $this->assertEquals('15', $output); } + public function testFunctionWithArrayPositionalParam() { + $output = $this->fenom->fetch('function_array_param_pos.tpl'); + $this->assertEquals('15', $output); + } + } From ceedbd3372a4620be030b105249b607982eb3254 Mon Sep 17 00:00:00 2001 From: klkvsk Date: Fri, 12 Jul 2013 12:05:37 +0400 Subject: [PATCH 5/6] fix for expressions like "{if (!($a || $b))}" (were failing to compile), tests added --- src/Fenom/Template.php | 2 +- tests/cases/Fenom/TemplateTest.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index b27b822..00eb69d 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -608,7 +608,7 @@ class Template extends Render { } $term = 1; } elseif(!$term && $tokens->is(Tokenizer::MACRO_UNARY)) { - if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY)) { + if(!$tokens->isNext(T_VARIABLE, T_DNUMBER, T_LNUMBER, T_STRING, T_ISSET, T_EMPTY, '(')) { break; } $_exp .= $tokens->getAndNext(); diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index b0b9d32..0c1c80e 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -166,8 +166,9 @@ class TemplateTest extends TestCase { array('Exp: {$y-$x} result', $b, 'Exp: 18 result'), array('Exp: {$y*$x} result', $b, 'Exp: 243 result'), array('Exp: {$y^$x} result', $b, 'Exp: 18 result'), - array('Exp: {-$x} result', $b, 'Exp: -9 result'), + array('Exp: {-($x)} result', $b, 'Exp: -9 result'), array('Exp: {!$x} result', $b, 'Exp: result'), + array('Exp: {!($x)} result', $b, 'Exp: result'), array('Exp: {!5} result', $b, 'Exp: result'), array('Exp: {-1} result', $b, 'Exp: -1 result'), array('Exp: {$z = 5} {$z} result', $b, 'Exp: 5 5 result'), @@ -266,6 +267,8 @@ class TemplateTest extends TestCase { array('if: {if true} block1 {else} block2 {/if} end', $a, 'if: block1 end'), array('if: {if false} block1 {else} block2 {/if} end', $a, 'if: block2 end'), array('if: {if null} block1 {else} block2 {/if} end', $a, 'if: block2 end'), + array('if: {if !($val0 || $val1)} block1 {else} block2 {/if} end', + $a, 'if: block2 end'), ); } From 7e557df6ea485729f63dbc58a73a32bb20598cd2 Mon Sep 17 00:00:00 2001 From: litvinenkow Date: Thu, 18 Jul 2013 01:46:57 -0700 Subject: [PATCH 6/6] Update Fenom.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit по-моему со строчки 733 нужно исправить вот так, то когда параметры задаешь массивом, ошибка вываливается да и с маской тоже не особо работает --- src/Fenom.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Fenom.php b/src/Fenom.php index 55f09ed..bcaf31d 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -731,12 +731,12 @@ class Fenom { * @throws \RuntimeException if key from custom assoc doesn't exists into possible values */ private static function _makeMask(array $values, array $options, $mask = 0) { - foreach($values as $value) { - if(isset($options[$value])) { - if($options[$value]) { - $mask |= $options[$value]; + foreach ($values as $key=>$value) { + if (isset($options[$key])) { + if ($options[$key]) { + $mask |= $options[$key]; } else { - $mask &= ~$options[$value]; + $mask &= ~$options[$key]; } } else { throw new \RuntimeException("Undefined parameter $value");