Fix #105 + tests

This commit is contained in:
bzick 2014-08-23 11:41:21 +04:00
parent bb8d351e93
commit af7546a8ec
3 changed files with 29 additions and 11 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -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) {