diff --git a/src/Fenom.php b/src/Fenom.php index 9cd3e09..3626bd1 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -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; } diff --git a/src/Fenom/Accessor.php b/src/Fenom/Accessor.php index 2330537..3afa5ea 100644 --- a/src/Fenom/Accessor.php +++ b/src/Fenom/Accessor.php @@ -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) { diff --git a/src/Fenom/Template.php b/src/Fenom/Template.php index 316b170..31e961f 100644 --- a/src/Fenom/Template.php +++ b/src/Fenom/Template.php @@ -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) { diff --git a/tests/cases/Fenom/SandboxTest.php b/tests/cases/Fenom/SandboxTest.php index b2f3172..7890108 100644 --- a/tests/cases/Fenom/SandboxTest.php +++ b/tests/cases/Fenom/SandboxTest.php @@ -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 '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; } } \ No newline at end of file