mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
commit
b6f363eaff
@ -780,14 +780,7 @@ class Template extends Render
|
|||||||
if ($this->_options & Fenom::DENY_METHODS) {
|
if ($this->_options & Fenom::DENY_METHODS) {
|
||||||
throw new \LogicException("Forbidden to call methods");
|
throw new \LogicException("Forbidden to call methods");
|
||||||
}
|
}
|
||||||
do { // parse call-chunks: $var->func()->func()->prop->func()->...
|
$code = $this->parseChain($tokens, $code);
|
||||||
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));
|
|
||||||
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
|
} elseif ($tokens->is(Tokenizer::MACRO_INCDEC)) {
|
||||||
$code .= $tokens->getAndNext();
|
$code .= $tokens->getAndNext();
|
||||||
} else {
|
} else {
|
||||||
@ -812,11 +805,11 @@ class Template extends Render
|
|||||||
if (!$func) {
|
if (!$func) {
|
||||||
throw new \Exception("Function " . $tokens->getAndNext() . " not found");
|
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)) {
|
} elseif ($tokens->isNext(T_NS_SEPARATOR, T_DOUBLE_COLON)) {
|
||||||
$method = $this->parseStatic($tokens);
|
$method = $this->parseStatic($tokens);
|
||||||
$args = $this->parseArgs($tokens);
|
$args = $this->parseArgs($tokens);
|
||||||
return $unary . $method . $args;
|
return $unary . $this->parseChain($tokens, $method . $args);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
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']
|
* Parse variable name: $a, $a.b, $a.b['c']
|
||||||
* @param Tokenizer $tokens
|
* @param Tokenizer $tokens
|
||||||
|
@ -287,6 +287,10 @@ class Helper
|
|||||||
$this->self = $this;
|
$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()
|
public function chunk()
|
||||||
{
|
{
|
||||||
return $this;
|
return $this;
|
||||||
@ -301,3 +305,4 @@ class Helper
|
|||||||
return array(1,2,3);
|
return array(1,2,3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +114,7 @@ class TemplateTest extends TestCase
|
|||||||
array('hello, {"World"}!', $a, 'hello, World!'),
|
array('hello, {"World"}!', $a, 'hello, World!'),
|
||||||
array('hello, {"W{$a}d"}!', $a, 'hello, WWorldd!'),
|
array('hello, {"W{$a}d"}!', $a, 'hello, WWorldd!'),
|
||||||
array('hello, {$world->chunk(1)->self->chunk("new")}!', $b, 'hello, world!'),
|
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 {
|
try {
|
||||||
var_dump(
|
var_dump(
|
||||||
$this->fenom->compileCode(
|
$this->fenom->compileCode(
|
||||||
'{add $a[] = 5}'
|
'{Fenom\Helper::method()->page->title}'
|
||||||
)->getBody()
|
)->getBody()
|
||||||
);
|
);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user