Merge pull request #91 from bzick/develop

2.1.2
This commit is contained in:
Ivan Shalganov 2014-07-03 12:18:51 +04:00
commit f74ee26d95
4 changed files with 43 additions and 9 deletions

View File

@ -635,7 +635,6 @@ class Compiler
/**
* Standard close tag {/...}
*
* @static
* @return string
*/
public static function stdClose()
@ -646,20 +645,23 @@ class Compiler
/**
* Standard function parser
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
*/
public static function stdFuncParser(Tokenizer $tokens, Tag $tag)
{
return $tag->out($tag->callback . "(" . self::toArray($tag->tpl->parseParams($tokens)) . ', $tpl)');
if(is_string($tag->callback)) {
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
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
@ -689,7 +691,6 @@ class Compiler
/**
* Standard function open tag parser
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
@ -704,7 +705,6 @@ class Compiler
/**
* Standard function close tag parser
*
* @static
* @param Tokenizer $tokens
* @param Tag $tag
* @return string
@ -712,7 +712,12 @@ class Compiler
public static function stdFuncClose($tokens, Tag $tag)
{
$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

@ -296,4 +296,8 @@ class Helper
{
return $this->word;
}
public function getArray() {
return array(1,2,3);
}
}

View File

@ -688,7 +688,8 @@ class TemplateTest extends TestCase
{
$a = array(
"list" => array(1 => "one", 2 => "two", 3 => "three"),
"empty" => array()
"empty" => array(),
"obj" => new Helper("testing")
);
return array(
array('Foreach: {foreach $list as $e} {$e}, {/foreach} end', $a, 'Foreach: one, two, three, end'),
@ -715,6 +716,7 @@ class TemplateTest extends TestCase
),
array('Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach [] as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array('Foreach: {foreach $obj->getArray() as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: 0 => 1, 1 => 2, 2 => 3, end'),
array('Foreach: {foreach $unexists as $k => $e} {$k} => {$e}, {/foreach} end', $a, 'Foreach: end'),
array(
'Foreach: {foreach $empty as $k => $e} {$k} => {$e}, {foreachelse} empty {/foreach} end',

View File

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