From af7546a8ec426d99a46f57217d8f1a08bcad2573 Mon Sep 17 00:00:00 2001 From: bzick Date: Sat, 23 Aug 2014 11:41:21 +0400 Subject: [PATCH] Fix #105 + tests --- src/Fenom/Template.php | 32 ++++++++++++++++++++---------- tests/TestCase.php | 5 +++++ tests/cases/Fenom/TemplateTest.php | 3 ++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index af772eb..09bf5e6 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -780,14 +780,7 @@ class Template extends Render if ($this->_options & Fenom::DENY_METHODS) { throw new \LogicException("Forbidden to call methods"); } - do { // parse call-chunks: $var->func()->func()->prop->func()->... - if ($tokens->is('(')) { - $code .= $this->parseArgs($tokens); - } - if ($tokens->is(T_OBJECT_OPERATOR) && $tokens->isNext(T_STRING)) { - $code .= '->' . $tokens->next()->getAndNext(); - } - } while ($tokens->is('(', T_OBJECT_OPERATOR)); + $code = $this->parseChain($tokens, $code); } elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) { $code .= $tokens->getAndNext(); } else { @@ -812,11 +805,11 @@ class Template extends Render if (!$func) { throw new \Exception("Function " . $tokens->getAndNext() . " not found"); } - return $unary . $func . $this->parseArgs($tokens->next()); + return $unary . $this->parseChain($tokens, $func . $this->parseArgs($tokens->next())); } elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) { $method = $this->parseStatic($tokens); $args = $this->parseArgs($tokens); - return $unary . $method . $args; + return $unary . $this->parseChain($tokens, $method . $args); } else { return false; } @@ -841,6 +834,25 @@ class Template extends Render } } + /** + * Parse call-chunks: $var->func()->func()->prop->func()->... + * @param Tokenizer $tokens + * @param string $code start point (it is $var) + * @return string + */ + public function parseChain(Tokenizer $tokens, $code) { + do { + if ($tokens->is('(')) { + $code .= $this->parseArgs($tokens); + } + if ($tokens->is(T_OBJECT_OPERATOR) && $tokens->isNext(T_STRING)) { + $code .= '->' . $tokens->next()->getAndNext(); + } + } while ($tokens->is('(', T_OBJECT_OPERATOR)); + + return $code; + } + /** * Parse variable name: $a, $a.b, $a.b['c'] * @param Tokenizer $tokens diff --git a/tests/TestCase.php b/tests/TestCase.php index e0ca435..f1b40a4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -287,6 +287,10 @@ class Helper $this->self = $this; } + public static function method() { + return new \ArrayObject(array("page" => new \ArrayObject(array("title" => "test page"), \ArrayObject::ARRAY_AS_PROPS)), \ArrayObject::ARRAY_AS_PROPS); + } + public function chunk() { return $this; @@ -301,3 +305,4 @@ class Helper return array(1,2,3); } } + diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index dfd8daf..42db34a 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -114,6 +114,7 @@ class TemplateTest extends TestCase array('hello, {"World"}!', $a, 'hello, World!'), array('hello, {"W{$a}d"}!', $a, 'hello, WWorldd!'), array('hello, {$world->chunk(1)->self->chunk("new")}!', $b, 'hello, world!'), + array(':: {Fenom\Helper::method()->page->title} ::', $b, ':: test page ::'), ); } @@ -1318,7 +1319,7 @@ class TemplateTest extends TestCase try { var_dump( $this->fenom->compileCode( - '{add $a[] = 5}' + '{Fenom\Helper::method()->page->title}' )->getBody() ); } catch (\Exception $e) {