Done :ignore option, improve ignore mechanism

This commit is contained in:
bzick
2014-05-12 00:36:50 +04:00
parent 58d118496c
commit d8c4ea6994
5 changed files with 52 additions and 40 deletions

View File

@ -277,6 +277,11 @@ class Fenom
'type' => self::BLOCK_COMPILER, 'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::stripOpen', 'open' => 'Fenom\Compiler::stripOpen',
'close' => 'Fenom\Compiler::nope' 'close' => 'Fenom\Compiler::nope'
),
'ignore' => array(
'type' => self::BLOCK_COMPILER,
'open' => 'Fenom\Compiler::ignoreOpen',
'close' => 'Fenom\Compiler::nope'
) )
); );

View File

@ -986,4 +986,14 @@ class Compiler
$tokens->next(); $tokens->next();
$tag->setOption(\Fenom::AUTO_STRIP, $expected); $tag->setOption(\Fenom::AUTO_STRIP, $expected);
} }
/**
* Tag {ignore}
* @param Tokenizer $tokens
* @param Tag $tag
*/
public static function ignoreOpen(Tokenizer $tokens, Tag $tag)
{
$tag->tpl->ignore('ignore');
}
} }

View File

@ -286,4 +286,14 @@ class Tag extends \ArrayObject
{ {
$this->setOption(\Fenom::AUTO_STRIP, true); $this->setOption(\Fenom::AUTO_STRIP, true);
} }
/**
* Enable ignore for body of the tag
*/
public function optIgnore()
{
if(!$this->isClosed()) {
$this->tpl->ignore($this->name);
}
}
} }

View File

@ -96,7 +96,7 @@ class Template extends Render
private $_line = 1; private $_line = 1;
private $_post = array(); private $_post = array();
/** /**
* @var bool * @var bool|string
*/ */
private $_ignore = false; private $_ignore = false;
@ -243,24 +243,22 @@ class Template extends Render
$this->_ignore = false; $this->_ignore = false;
} else { // still ignore } else { // still ignore
$this->_appendText($tag); $this->_appendText($tag);
continue;
} }
}
if ($this->_tag_filters) {
foreach ($this->_tag_filters as $filter) {
$_tag = call_user_func($filter, $_tag, $this);
}
}
$tokens = new Tokenizer($_tag); // tokenize the tag
if ($tokens->isIncomplete()) { // all strings finished?
$need_more = true;
} else { } else {
if ($this->_tag_filters) { $this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer
foreach ($this->_tag_filters as $filter) { if ($tokens->key()) { // if tokenizer have tokens - throws exceptions
$_tag = call_user_func($filter, $_tag, $this); throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line);
}
}
$tokens = new Tokenizer($_tag); // tokenize the tag
if ($tokens->isIncomplete()) { // all strings finished?
$need_more = true;
} else {
$this->_appendCode($this->parseTag($tokens), $tag); // start the tag lexer
if ($tokens->key()) { // if tokenizer have tokens - throws exceptions
throw new CompileException("Unexpected token '" . $tokens->current() . "' in {$this} line {$this->_line}, near '{" . $tokens->getSnippetAsString(
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line);
}
} }
} }
} while ($need_more); } while ($need_more);
@ -381,6 +379,10 @@ class Template extends Render
} }
} }
public function ignore($tag_name) {
$this->_ignore = $tag_name;
}
/** /**
* @param callable[] $cb * @param callable[] $cb
*/ */
@ -552,13 +554,7 @@ class Template extends Render
{ {
try { try {
if ($tokens->is(Tokenizer::MACRO_STRING)) { if ($tokens->is(Tokenizer::MACRO_STRING)) {
if ($tokens->current() === "ignore") { return $this->parseAct($tokens);
$this->_ignore = "ignore";
$tokens->next();
return '';
} else {
return $this->parseAct($tokens);
}
} elseif ($tokens->is('/')) { } elseif ($tokens->is('/')) {
return $this->parseEndTag($tokens); return $this->parseEndTag($tokens);
} else { } else {
@ -567,15 +563,9 @@ class Template extends Render
} catch (InvalidUsageException $e) { } catch (InvalidUsageException $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e); throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\LogicException $e) { } catch (\LogicException $e) {
throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString( throw new SecurityException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString( throw new CompileException($e->getMessage() . " in {$this->_name} line {$this->_line}, near '{" . $tokens->getSnippetAsString(0, 0) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
0,
0
) . "' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
} }
} }
@ -934,10 +924,7 @@ class Template extends Render
switch ($key) { switch ($key) {
case 'const': case 'const':
$tokens->need('.')->next(); $tokens->need('.')->next();
$var = $this->parseName($tokens); $var = '@constant(' . var_export($this->parseName($tokens), true) . ')';
if (!defined($var)) {
$var = '@constant(' . var_export($var, true) . ')';
}
break; break;
case 'version': case 'version':
$var = '\Fenom::VERSION'; $var = '\Fenom::VERSION';
@ -1156,9 +1143,6 @@ class Template extends Render
case '"': case '"':
return $this->parseQuote($tokens); return $this->parseQuote($tokens);
break; break;
case '$':
$tokens->next()->need('.')->next()->need(T_CONST)->next();
return @constant($this->parseName($tokens));
default: default:
throw new UnexpectedTokenException($tokens); throw new UnexpectedTokenException($tokens);
} }

View File

@ -62,6 +62,7 @@ class TemplateTest extends TestCase
array('hello, {$b."$c"}!', $b, 'hello, Username!'), array('hello, {$b."$c"}!', $b, 'hello, Username!'),
array('hello, {$b."{$c}"}!', $b, 'hello, Username!'), array('hello, {$b."{$c}"}!', $b, 'hello, Username!'),
array('hello, {$b[ "{$c}" ]}!', $b, 'hello, Username!'), array('hello, {$b[ "{$c}" ]}!', $b, 'hello, Username!'),
array('hello, {$b[ ("{$c}") ]}!', $b, 'hello, Username!'),
array('hello, {$b[ "mcp" ]}!', $b, 'hello, Master!'), array('hello, {$b[ "mcp" ]}!', $b, 'hello, Master!'),
array('hello, {$b[ "m{$c}p" ]}!', $b, 'hello, Master!'), array('hello, {$b[ "m{$c}p" ]}!', $b, 'hello, Master!'),
array('hello, {$b."m{$c}p"}!', $b, 'hello, Master!'), array('hello, {$b."m{$c}p"}!', $b, 'hello, Master!'),
@ -850,7 +851,8 @@ class TemplateTest extends TestCase
$a, $a,
'literal: { $a} end' 'literal: { $a} end'
), ),
array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end') array('{if 0}none{/if}literal: function () { return 1; } end', $a, 'literal: function () { return 1; } end'),
array('{if:ignore 1}literal: {$a} end{/if}', $a, 'literal: {$a} end'),
); );
} }
@ -1454,6 +1456,7 @@ class TemplateTest extends TestCase
} }
/** /**
* @group testIgnores
* @dataProvider providerIgnores * @dataProvider providerIgnores
*/ */
public function testIgnores($code, $vars, $result) public function testIgnores($code, $vars, $result)