Add predefined accessors Fenom::ACCESSOR_PROPERY and Fenom::ACCESSOR_METHOD

This commit is contained in:
bzick 2015-08-13 00:07:01 +03:00
parent bf44511a53
commit 4fe339ba21
4 changed files with 80 additions and 9 deletions

View File

@ -7,8 +7,8 @@ require_once __DIR__.'/../tests/tools.php';
$fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled'); $fenom = Fenom::factory(__DIR__.'/templates', __DIR__.'/compiled');
$fenom->setOptions(Fenom::AUTO_RELOAD | Fenom::FORCE_COMPILE); $fenom->setOptions(Fenom::AUTO_RELOAD | Fenom::FORCE_COMPILE);
$fenom->addAccessorSmart('g', 'App::$q->get', Fenom::ACCESSOR_CALL); $fenom->addAccessorSmart('fetch', 'fetch', Fenom::ACCESSOR_METHOD);
var_dump($fenom->compileCode('{$.g("env")}')->getBody()); var_dump($fenom->compileCode('{$.fetch("template.tpl")}')->getBody());
//var_dump($fenom->compile("bug158/main.tpl", [])->getTemplateCode()); //var_dump($fenom->compile("bug158/main.tpl", [])->getTemplateCode());
//var_dump($fenom->display("bug158/main.tpl", [])); //var_dump($fenom->display("bug158/main.tpl", []));
// $fenom->getTemplate("problem.tpl"); // $fenom->getTemplate("problem.tpl");

View File

@ -54,9 +54,11 @@ class Fenom
const MAX_MACRO_RECURSIVE = 32; const MAX_MACRO_RECURSIVE = 32;
const ACCESSOR_CUSTOM = null; const ACCESSOR_CUSTOM = null;
const ACCESSOR_VAR = 'Fenom\Accessor::parserVar'; const ACCESSOR_VAR = 'Fenom\Accessor::parserVar';
const ACCESSOR_CALL = 'Fenom\Accessor::parserCall'; const ACCESSOR_CALL = 'Fenom\Accessor::parserCall';
const ACCESSOR_PROPERTY = 'Fenom\Accessor::parserProperty';
const ACCESSOR_METHOD = 'Fenom\Accessor::parserMethod';
public static $charset = "UTF-8"; public static $charset = "UTF-8";
@ -815,7 +817,7 @@ class Fenom
/** /**
* Add global accessor ($.) * Add global accessor ($.)
* @param string $name * @param string $name
* @param callable|string $accessor * @param mixed $accessor
* @param string $parser * @param string $parser
* @return Fenom * @return Fenom
*/ */

View File

@ -28,16 +28,53 @@ class Accessor {
'env' => '$_ENV' 'env' => '$_ENV'
); );
public static function parserVar($var, Tokenizer $tokens, Template $tpl, &$is_var) { /**
* @param string $var variable expression on PHP ('App::get("storage")->user')
* @param Tokenizer $tokens
* @param Template $tpl
* @param $is_var
* @return string
*/
public static function parserVar($var, Tokenizer $tokens, Template $tpl, &$is_var)
{
$is_var = true; $is_var = true;
return $tpl->parseVariable($tokens, $var); return $tpl->parseVariable($tokens, $var);
} }
/**
public static function parserCall($call, Tokenizer $tokens, Template $tpl) { * @param string $call method name expression on PHP ('App::get("storage")->getUser')
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
*/
public static function parserCall($call, Tokenizer $tokens, Template $tpl)
{
return $call.$tpl->parseArgs($tokens); return $call.$tpl->parseArgs($tokens);
} }
/**
* @param string $prop fenom's property name
* @param Tokenizer $tokens
* @param Template $tpl
* @param $is_var
* @return string
*/
public static function parserProperty($prop, Tokenizer $tokens, Template $tpl, &$is_var)
{
return self::parserVar('$tpl->getStorage()->'.$prop, $tokens, $tpl, $is_var);
}
/**
* @param string $method fenom's method name
* @param Tokenizer $tokens
* @param Template $tpl
* @return string
*/
public static function parserMethod($method, Tokenizer $tokens, Template $tpl)
{
return self::parserCall('$tpl->getStorage()->'.$method, $tokens, $tpl);
}
/** /**
* Accessor for global variables * Accessor for global variables
* @param Tokenizer $tokens * @param Tokenizer $tokens
@ -70,6 +107,9 @@ class Accessor {
} }
} }
/**
* @return string
*/
public static function version() public static function version()
{ {
return 'Fenom::VERSION'; return 'Fenom::VERSION';

View File

@ -227,4 +227,33 @@ class AccessorTest extends TestCase
public function testFetchInvalidTpl($tpl, $exception, $message) { public function testFetchInvalidTpl($tpl, $exception, $message) {
$this->execError($tpl, $exception, $message); $this->execError($tpl, $exception, $message);
} }
public function getThree() {
return 3;
}
public static function providerSmartAccessor() {
return array(
['acc', '$tpl->getStorage()->test->values', \Fenom::ACCESSOR_VAR, '{$.acc.three}', '3'],
['acc', '$tpl->getStorage()->test->getThree', \Fenom::ACCESSOR_CALL, '{$.acc()}', '3'],
['acc', 'three', \Fenom::ACCESSOR_PROPERTY, '{$.acc}', '3'],
['acc', 'templateExists', \Fenom::ACCESSOR_METHOD, '{$.acc("persist:pipe.tpl")}', '1']
);
}
/**
* @group testSmartAccessor
* @dataProvider providerSmartAccessor
* @param $name
* @param $accessor
* @param $type
* @param $code
* @param $result
*/
public function testSmartAccessor($name, $accessor, $type, $code, $result) {
$this->fenom->test = $this;
$this->fenom->three = 3;
$this->fenom->addAccessorSmart($name, $accessor, $type);
$this->assertRender($code, $result, $this->getVars());
}
} }