Add custom parsers for accessors

This commit is contained in:
bzick 2015-06-01 23:42:47 +03:00
parent 3b56906ec8
commit ef4458ca10
4 changed files with 44 additions and 18 deletions

View File

@ -53,6 +53,10 @@ class Fenom
const MAX_MACRO_RECURSIVE = 32;
const ACCESSOR_CUSTOM = null;
const ACCESSOR_VAR = 'Fenom\Accessor::parserVar';
const ACCESSOR_CALL = 'Fenom\Accessor::parserCall';
/**
* @var int[] of possible options, as associative array
* @see setOptions
@ -796,12 +800,16 @@ class Fenom
/**
* Add global accessor ($.)
* @param string $name
* @param callable $parser
* @param callable|string $accessor
* @param string $parser
* @return Fenom
*/
public function addAccessor($name, $parser)
public function addAccessor($name, $accessor, $parser = self::ACCESSOR_CUSTOM)
{
$this->_accessors[$name] = $parser;
$this->_accessors[$name] = array(
"accessor" => $accessor,
"parser" => $parser
);
return $this;
}

View File

@ -28,10 +28,20 @@ class Accessor {
'env' => '$_ENV'
);
public static function parserVar($var, Tokenizer $tokens, Template $tpl, &$is_var) {
$is_var = true;
return $tpl->parseVariable($tokens, $var);
}
public static function parserCall($accessor, Tokenizer $tokens, Template $tpl) {
// return $tpl->parseVariable($tokens, $var);
}
/**
* Accessor for global variables
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
*/
public static function getVar(Tokenizer $tokens, Template $tpl)
{
@ -47,6 +57,7 @@ class Accessor {
/**
* Accessor for template information
* @param Tokenizer $tokens
* @return string
*/
public static function tpl(Tokenizer $tokens)
{

View File

@ -795,8 +795,16 @@ class Template extends Render
}
$code = $this->parseScalar($tokens, true);
break;
case '$':
$code = $this->parseAccessor($tokens, $is_var);
if(!$is_var) {
$code = $unary . $code;
break;
}
case T_VARIABLE:
$code = $this->parseVariable($tokens);
if(!isset($code)) {
$code = $this->parseVariable($tokens);
}
if ($tokens->is("(") && $tokens->hasBackList(T_STRING, T_OBJECT_OPERATOR)) {
if ($this->_options & Fenom::DENY_METHODS) {
throw new \LogicException("Forbidden to call methods");
@ -817,9 +825,6 @@ class Template extends Render
}
}
break;
case '$':
$code = $unary . $this->parseAccessor($tokens, $is_var);
break;
case T_DEC:
case T_INC:
if($this->_options & Fenom::FORCE_VERIFY) {

View File

@ -10,23 +10,25 @@ class SandboxTest extends TestCase {
*/
public function test()
{
return;
$this->fenom->setOptions(\Fenom::FORCE_VERIFY);
$this->fenom->addAccessor('q', 'Navi::$q', \Fenom::ACCESSOR_VAR);
// $this->assertEquals([1, 2, 4, "as" => 767, "df" => ["qert"]], [1, 2, 4, "as" => 767, "df" => ["qet"]]);
// $this->fenom->addBlockCompiler('php', 'Fenom\Compiler::nope', function ($tokens, Tag $tag) {
// return '<?php ' . $tag->cutContent();
// });
// $this->tpl('welcome.tpl', '{$a}');
// var_dump($this->fenom->compileCode('{set $a=$one|min:0..$three|max:4}')->getBody());
// try {
// var_dump($this->fenom->compileCode('{foreach $a as $k => $v} {/foreach}')->getBody());
// } catch (\Exception $e) {
// print_r($e->getMessage() . "\n" . $e->getTraceAsString());
// while ($e->getPrevious()) {
// $e = $e->getPrevious();
// print_r("\n\n" . $e->getMessage() . " in {$e->getFile()}:{$e->getLine()}\n" . $e->getTraceAsString());
// }
// }
// exit;
try {
var_dump($this->fenom->compileCode('{$.q.ddqd->d() + 333}')->getBody());
} catch (\Exception $e) {
print_r($e->getMessage() . "\n" . $e->getTraceAsString());
while ($e->getPrevious()) {
$e = $e->getPrevious();
print_r("\n\n" . $e->getMessage() . " in {$e->getFile()}:{$e->getLine()}\n" . $e->getTraceAsString());
}
}
exit;
}
}