From f6cc2d59938ead4c526c73deef197ff81033d336 Mon Sep 17 00:00:00 2001 From: bzick Date: Sat, 20 Jul 2013 21:25:32 +0400 Subject: [PATCH] Remove addTemplate method --- src/Fenom.php | 50 ++++++++++++++++++------------ tests/cases/Fenom/TemplateTest.php | 34 ++++++++++---------- tests/cases/FenomTest.php | 14 --------- 3 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/Fenom.php b/src/Fenom.php index e1da263..cfcfc36 100644 --- a/src/Fenom.php +++ b/src/Fenom.php @@ -18,7 +18,7 @@ use Fenom\Template, class Fenom { const VERSION = '1.0'; - /* Compiler types */ + /* Actions */ const INLINE_COMPILER = 1; const BLOCK_COMPILER = 2; const INLINE_FUNCTION = 3; @@ -26,14 +26,16 @@ class Fenom { const MODIFIER = 5; /* Options */ - const DENY_METHODS = 0x10; - const DENY_INLINE_FUNCS = 0x20; - const FORCE_INCLUDE = 0x40; - const AUTO_RELOAD = 0x80; - const FORCE_COMPILE = 0xF0; - const DISABLE_CACHE = 0x1F0; - const AUTO_ESCAPE = 0x200; - const FORCE_VALIDATE = 0x400; + const DENY_METHODS = 0x10; + const DENY_INLINE_FUNCS = 0x20; + const FORCE_INCLUDE = 0x40; + const AUTO_RELOAD = 0x80; + const FORCE_COMPILE = 0xF0; + const DISABLE_CACHE = 0x1F0; + const AUTO_ESCAPE = 0x200; + const FORCE_VERIFY = 0x400; + const AUTO_TRIM = 0x800; + const DENY_STATIC_METHODS = 0xF00; /* Default parsers */ const DEFAULT_CLOSE_COMPILER = 'Fenom\Compiler::stdClose'; @@ -54,7 +56,7 @@ class Fenom { "auto_reload" => self::AUTO_RELOAD, "force_include" => self::FORCE_INCLUDE, "auto_escape" => self::AUTO_ESCAPE, - "force_validate" => self::FORCE_VALIDATE + "force_verify" => self::FORCE_VERIFY ); /** @@ -100,7 +102,8 @@ class Fenom { "unescape" => 'Fenom\Modifier::unescape', "strip" => 'Fenom\Modifier::strip', "length" => 'Fenom\Modifier::length', - "default" => 'Fenom\Modifier::defaultValue' + "default" => 'Fenom\Modifier::defaultValue', + "iterable" => 'Fenom\Modifier::isIterable' ); /** @@ -593,15 +596,15 @@ class Fenom { * * @param string $template name of template * @param array $vars - * @param $callback + * @param callable $callback * @param float $chunk - * @return \Fenom\Render + * @return array */ public function pipe($template, array $vars, $callback, $chunk = 1e6) { ob_start($callback, $chunk, true); - $this->getTemplate($template)->display($vars); + $data = $this->getTemplate($template)->display($vars); ob_end_flush(); - + return $data; } /** @@ -629,12 +632,19 @@ class Fenom { } /** - * Add custom template into storage - * - * @param Fenom\Render $template + * Check if template exists + * @param string $template + * @return bool */ - public function addTemplate(Fenom\Render $template) { - $this->_storage[dechex($template->getOptions()).'@'. $template->getName() ] = $template; + public function templateExists($template) { + if($provider = strstr($template, ":", true)) { + if(isset($this->_providers[$provider])) { + return $this->_providers[$provider]->templateExists(substr($template, strlen($provider) + 1)); + } + } else { + return $this->_provider->templateExists($template); + } + return false; } /** diff --git a/tests/cases/Fenom/TemplateTest.php b/tests/cases/Fenom/TemplateTest.php index b0b9d32..c0c7c5f 100644 --- a/tests/cases/Fenom/TemplateTest.php +++ b/tests/cases/Fenom/TemplateTest.php @@ -11,15 +11,6 @@ use Fenom\Template, */ class TemplateTest extends TestCase { - public function setUp() { - parent::setUp(); - $this->fenom->addTemplate(new Render($this->fenom, function ($tpl) { - echo "Welcome, ".$tpl["username"]." (".$tpl["email"].")"; - }, array( - "name" => "welcome.tpl" - ))); - } - public static function providerVars() { $a = array("a" => "World"); $obj = new \stdClass; @@ -126,19 +117,20 @@ class TemplateTest extends TestCase { array('Mod: {$rescue_html|unescape}!', $b, 'Mod: Chip & Dale!'), array('Mod: {$rescue_html|unescape:"html"}!', $b, 'Mod: Chip & Dale!'), array('Mod: {$rescue_url|unescape:"url"}!', $b, 'Mod: Chip & Dale!'), - array('Mod: {$rescue|unescape:"unknown"}!', $b, 'Mod: Chip & Dale!'), + array('Mod: {$rescue|unescape:"unknown"}!', $b, 'Mod: Chip & Dale!'), array('Mod: {$time|date_format:"%Y %m %d"}!', $b, 'Mod: 2012 07 26!'), array('Mod: {$date|date_format:"%Y %m %d"}!', $b, 'Mod: 2012 07 26!'), array('Mod: {$time|date:"Y m d"}!', $b, 'Mod: 2012 07 26!'), array('Mod: {$date|date:"Y m d"}!', $b, 'Mod: 2012 07 26!'), array('Mod: {$tags|strip_tags}!', $b, 'Mod: my name is Legion!'), array('Mod: {$b.c|json_encode}!', $b, 'Mod: "Username"!'), + array('Mod: {time()|date:"Y m d"}!', $b, 'Mod: '.date("Y m d").'!'), ); } public static function providerModifiersInvalid() { return array( - array('Mod: {$lorem|}!', 'Fenom\CompileException', "Unexpected end of expression"), + array('Mod: {$lorem|}!', 'Fenom\CompileException', "Unexpected end of expression"), array('Mod: {$lorem|str_rot13}!', 'Fenom\CompileException', "Modifier str_rot13 not found", Fenom::DENY_INLINE_FUNCS), array('Mod: {$lorem|my_encode}!', 'Fenom\CompileException', "Modifier my_encode not found"), array('Mod: {$lorem|truncate:}!', 'Fenom\CompileException', "Unexpected end of expression"), @@ -188,12 +180,16 @@ class TemplateTest extends TestCase { array('If: {-"hi"} end', 'Fenom\CompileException', "Unexpected token '-'"), array('If: {($a++)++} end', 'Fenom\CompileException', "Unexpected token '++'"), array('If: {$a + * $c} end', 'Fenom\CompileException', "Unexpected token '*'"), + array('If: {$a + } end', 'Fenom\CompileException', "Unexpected token '+'"), + array('If: {$a + =} end', 'Fenom\CompileException', "Unexpected token '='"), + array('If: {$a + 1 =} end', 'Fenom\CompileException', "Unexpected token '='"), + array('If: {$a + 1 = 6} end', 'Fenom\CompileException', "Unexpected token '='"), array('If: {/$a} end', 'Fenom\CompileException', "Unexpected token '\$a'"), array('If: {$a == 5 > 4} end', 'Fenom\CompileException', "Unexpected token '>'"), array('If: {$a != 5 <= 4} end', 'Fenom\CompileException', "Unexpected token '<='"), array('If: {$a != 5 => 4} end', 'Fenom\CompileException', "Unexpected token '=>'"), array('If: {$a + (*6)} end', 'Fenom\CompileException', "Unexpected token '*'"), - array('If: {$a + ( 6} end', 'Fenom\CompileException', "Brackets don't match"), + array('If: {$a + ( 6} end', 'Fenom\CompileException', "Unexpected end of expression, expect ')'"), ); } @@ -266,6 +262,9 @@ class TemplateTest extends TestCase { array('if: {if true} block1 {else} block2 {/if} end', $a, 'if: block1 end'), array('if: {if false} block1 {else} block2 {/if} end', $a, 'if: block2 end'), array('if: {if null} block1 {else} block2 {/if} end', $a, 'if: block2 end'), + array('if: {if ($val1 || $val0) && $x} block1 {else} block2 {/if} end', + $a, 'if: block1 end'), + array('if: {if $unexist} block1 {else} block2 {/if} end', $a, 'if: block2 end', Fenom::FORCE_VERIFY), ); } @@ -317,7 +316,7 @@ class TemplateTest extends TestCase { array('Create: {var $v = 1++} Result: {$v} end', 'Fenom\CompileException', "Unexpected token '++'"), array('Create: {var $v = c} Result: {$v} end', 'Fenom\CompileException', "Unexpected token 'c'"), array('Create: {var $v = ($a)++} Result: {$v} end', 'Fenom\CompileException', "Unexpected token '++'"), - array('Create: {var $v = --$a++} Result: {$v} end', 'Fenom\CompileException', "Unexpected token '++'"), + array('Create: {var $v = --$a++} Result: {$v} end', 'Fenom\CompileException', "Can not use two increments and decrements for one variable"), array('Create: {var $v = $a|upper++} Result: {$v} end', 'Fenom\CompileException', "Unexpected token '++'"), array('Create: {var $v = max($a,2)++} Result: {$v} end', 'Fenom\CompileException', "Unexpected token '++'"), array('Create: {var $v = max($a,2)} Result: {$v} end', 'Fenom\CompileException', "Modifier max not found", Fenom::DENY_INLINE_FUNCS), @@ -388,6 +387,8 @@ class TemplateTest extends TestCase { array('{if $nonempty.string!} right {/if}', $a), array('{if $nonempty.double!} right {/if}', $a), array('{if $nonempty.bool!} right {/if}', $a), + // ! ... : ... + // !: ... ); } @@ -614,7 +615,7 @@ class TemplateTest extends TestCase { * @group include * @dataProvider providerInclude */ - public function testInclude($code, $vars, $result) { + public function _testInclude($code, $vars, $result) { // fixme, addTemplate removed $this->exec($code, $vars, $result); } @@ -627,9 +628,10 @@ class TemplateTest extends TestCase { /** * @dataProvider providerIf + * @group test-if */ - public function testIf($code, $vars, $result) { - $this->exec($code, $vars, $result); + public function testIf($code, $vars, $result, $options = 0) { + $this->exec($code, $vars, $result, $options); } /** diff --git a/tests/cases/FenomTest.php b/tests/cases/FenomTest.php index 956625d..b4b61e6 100644 --- a/tests/cases/FenomTest.php +++ b/tests/cases/FenomTest.php @@ -5,20 +5,6 @@ use Fenom\Render, class FenomTest extends \Fenom\TestCase { - public function testAddRender() { - $test = $this; - $this->fenom->addTemplate(new Render($this->fenom, function($tpl) use ($test) { - /** @var \PHPUnit_Framework_TestCase $test */ - $test->assertInstanceOf('Fenom\Render', $tpl); - echo "Inline render"; - }, array( - "name" => 'render.tpl', - "scm" => false - ))); - - $this->assertSame("Inline render", $this->fenom->fetch('render.tpl', array())); - } - public function testCompileFile() { $a = array( "a" => "a",