This commit is contained in:
bzick 2014-07-03 12:07:20 +04:00
parent eb49861ab8
commit a4883d75e5
2 changed files with 36 additions and 8 deletions

View File

@ -635,7 +635,6 @@ class Compiler
/** /**
* Standard close tag {/...} * Standard close tag {/...}
* *
* @static
* @return string * @return string
*/ */
public static function stdClose() public static function stdClose()
@ -646,20 +645,23 @@ class Compiler
/** /**
* Standard function parser * Standard function parser
* *
* @static
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @param Tag $tag * @param Tag $tag
* @return string * @return string
*/ */
public static function stdFuncParser(Tokenizer $tokens, Tag $tag) public static function stdFuncParser(Tokenizer $tokens, Tag $tag)
{ {
if(is_string($tag->callback)) {
return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)'); return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)');
} else {
return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL.
$tag->out('call_user_func($info["function"], '.self::toArray($tag->tpl->parseParams($tokens)).', $tpl)');
}
} }
/** /**
* Smart function parser * Smart function parser
* *
* @static
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @param Tag $tag * @param Tag $tag
* @return string * @return string
@ -689,7 +691,6 @@ class Compiler
/** /**
* Standard function open tag parser * Standard function open tag parser
* *
* @static
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @param Tag $tag * @param Tag $tag
* @return string * @return string
@ -704,7 +705,6 @@ class Compiler
/** /**
* Standard function close tag parser * Standard function close tag parser
* *
* @static
* @param Tokenizer $tokens * @param Tokenizer $tokens
* @param Tag $tag * @param Tag $tag
* @return string * @return string
@ -712,7 +712,12 @@ class Compiler
public static function stdFuncClose($tokens, Tag $tag) public static function stdFuncClose($tokens, Tag $tag)
{ {
$tag->restore(\Fenom::AUTO_ESCAPE); $tag->restore(\Fenom::AUTO_ESCAPE);
return $tag->out($tag->callback . '(' . $tag["params"] . ', ob_get_clean(), $tpl)'); if(is_string($tag->callback)) {
return $tag->out($tag->callback . "(" . $tag["params"] . ', ob_get_clean(), $tpl)');
} else {
return '$info = $tpl->getStorage()->getTag('.var_export($tag->name, true).');'.PHP_EOL.
$tag->out('call_user_func($info["function"], ' . $tag["params"] . ', ob_get_clean(), $tpl)');
}
} }
/** /**

View File

@ -130,13 +130,33 @@ class FenomTest extends \Fenom\TestCase
*/ */
public function testSetFunctions() public function testSetFunctions()
{ {
$test = $this;
$this->fenom->setOptions(Fenom::FORCE_COMPILE); $this->fenom->setOptions(Fenom::FORCE_COMPILE);
$this->fenom->addFunction("myfunc", "myFunc"); $this->fenom->addFunction("myfunc", "myFunc");
$this->fenom->addFunction("myfunc2", function ($args, $tpl) use ($test) {
$test->assertInstanceOf('Fenom\Render', $tpl);
$test->assertSame(array(
"name" => "foo"
), $args);
return "MyFunc2:".$args['name'];
});
$this->fenom->addBlockFunction("myblockfunc", "myBlockFunc"); $this->fenom->addBlockFunction("myblockfunc", "myBlockFunc");
$this->fenom->addBlockFunction("myblockfunc2", function ($args, $content, $tpl) use ($test) {
$test->assertInstanceOf('Fenom\Render', $tpl);
$test->assertSame(array(
"name" => "foo"
), $args);
$this->assertSame(' this block1 ', $content);
return "Block2:" . $args["name"] . ':' . trim($content) . ':Block';
});
$this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}'); $this->tpl('custom.tpl', 'Custom function {myfunc name="foo"}');
$this->assertSame("Custom function MyFunc:foo", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom function MyFunc:foo", $this->fenom->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom function {myfunc2 name="foo"}');
$this->assertSame("Custom function MyFunc2:foo", $this->fenom->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}'); $this->tpl('custom.tpl', 'Custom function {myblockfunc name="foo"} this block1 {/myblockfunc}');
$this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array())); $this->assertSame("Custom function Block:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array()));
$this->tpl('custom.tpl', 'Custom function {myblockfunc2 name="foo"} this block1 {/myblockfunc2}');
$this->assertSame("Custom function Block2:foo:this block1:Block", $this->fenom->fetch('custom.tpl', array()));
} }
public function testSetCompilers() public function testSetCompilers()
@ -253,7 +273,7 @@ class FenomTest extends \Fenom\TestCase
); );
} }
public function testAddFunctions() public function testAddAllowedFunctions()
{ {
$this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS); $this->fenom->setOptions(Fenom::DENY_NATIVE_FUNCS);
$this->assertFalse($this->fenom->isAllowedFunction('substr')); $this->assertFalse($this->fenom->isAllowedFunction('substr'));
@ -261,6 +281,9 @@ class FenomTest extends \Fenom\TestCase
$this->assertTrue($this->fenom->isAllowedFunction('substr')); $this->assertTrue($this->fenom->isAllowedFunction('substr'));
} }
/** /**
* @requires function php_gte_54 * @requires function php_gte_54
* @group pipe * @group pipe