From 8f3c94a7a10186a4c0deffe0e293d0e56d474f7a Mon Sep 17 00:00:00 2001 From: bzick Date: Thu, 19 Feb 2015 17:04:15 +0300 Subject: [PATCH] Fix #147 --- src/Fenom/Template.php | 11 ++++++++--- tests/cases/Fenom/SandboxTest.php | 3 ++- tests/cases/FenomTest.php | 28 +++++++++++++++++++++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index e5f7bde..956a457 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -819,11 +819,16 @@ class Template extends Render if ($tokens->isSpecialVal()) { return $unary . $tokens->getAndNext(); } elseif ($tokens->isNext("(") && !$tokens->getWhitespace()) { - $func = $this->_fenom->getModifier($tokens->current(), $this); + $func = $this->_fenom->getModifier($modifier = $tokens->current(), $this); if (!$func) { throw new \Exception("Function " . $tokens->getAndNext() . " not found"); } - return $unary . $this->parseChain($tokens, $func . $this->parseArgs($tokens->next())); + if (!is_string($func)) { // dynamic modifier + $call = 'call_user_func_array($tpl->getStorage()->getModifier("' . $modifier . '"), array'.$this->parseArgs($tokens->next()).')'; // @todo optimize + } else { + $call = $func . $this->parseArgs($tokens->next()); + } + return $unary . $this->parseChain($tokens, $call); } elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) { $method = $this->parseStatic($tokens); $args = $this->parseArgs($tokens); @@ -1389,7 +1394,7 @@ class Template extends Render * (1 + 2.3, 'string', $var, [2,4]) * * @param Tokenizer $tokens - * @throws TokenizeException + * @param bool $as_string * @return string */ public function parseArgs(Tokenizer $tokens) diff --git a/tests/cases/Fenom/SandboxTest.php b/tests/cases/Fenom/SandboxTest.php index 811efca..a5ea86b 100644 --- a/tests/cases/Fenom/SandboxTest.php +++ b/tests/cases/Fenom/SandboxTest.php @@ -13,8 +13,9 @@ class SandboxTest extends TestCase { // return 'cutContent(); // }); // $this->tpl('welcome.tpl', '{$a}'); +// $this->fenom->addModifier('min', function () {}); // try { -// var_dump($this->fenom->compileCode('{for $i=0 to 3} {/for}')->getBody()); +// var_dump($this->fenom->compileCode('{time() + min(1, 10)}')->getBody()); // } catch (\Exception $e) { // print_r($e->getMessage() . "\n" . $e->getTraceAsString()); // while ($e->getPrevious()) { diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index b4076c7..937cb40 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -119,10 +119,32 @@ class FenomTest extends \Fenom\TestCase public function testSetModifier() { $this->fenom->addModifier("mymod", "myMod"); - $this->tpl('custom.tpl', 'Custom modifier {$a|mymod}'); - $this->assertSame( + $this->assertRender( + 'Custom modifier {$a|mymod}', "Custom modifier (myMod)Custom(/myMod)", - $this->fenom->fetch('custom.tpl', array("a" => "Custom")) + array("a" => "Custom") + ); + $this->assertRender( + 'Custom modifier {mymod($a)}', + "Custom modifier (myMod)Custom(/myMod)", + array("a" => "Custom") + ); + } + + public function testSetModifierClosure() + { + $this->fenom->addModifier("mymod", function ($value) { + return "(myMod)$value(/myMod)"; + }); + $this->assertRender( + 'Custom modifier {$a|mymod}', + "Custom modifier (myMod)Custom(/myMod)", + array("a" => "Custom") + ); + $this->assertRender( + 'Custom modifier {mymod($a)}', + "Custom modifier (myMod)Custom(/myMod)", + array("a" => "Custom") ); }