Skip to content

Commit

Permalink
Removed ugly if($reject)
Browse files Browse the repository at this point in the history
boekkooi committed Jul 24, 2014
1 parent 2078bf5 commit 28add35
Showing 2 changed files with 56 additions and 32 deletions.
8 changes: 8 additions & 0 deletions src/Twig/Node/DeferReference.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,14 @@
*/
class DeferReference extends Twig_Node_BlockReference
{
/**
* @param string $name
* @param string $variable
* @param boolean $unique
* @param boolean $reference
* @param integer $lineno The line number
* @param string $tag The tag name associated with the Node
*/
public function __construct($name, $variable, $unique, $reference, $lineno, $tag = null)
{
parent::__construct($name, $lineno, $tag);
80 changes: 48 additions & 32 deletions src/Twig/TokenParser/Defer.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
use Twig_Token;
use Twig_TokenParser;
use Boekkooi\Bundle\TwigJackBundle\Twig\Node;
use Twig_TokenStream;

/**
* Marks a section of a template as being usable in a later stage.
@@ -25,6 +26,9 @@ class Defer extends Twig_TokenParser
{
protected $blockPrefix;

/**
* @param string $blockPrefix
*/
public function __construct($blockPrefix)
{
$this->blockPrefix = $blockPrefix;
@@ -34,8 +38,7 @@ public function __construct($blockPrefix)
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
*
* @return Twig_Token A Twig_NodeInterface instance
* @return null|Twig_Token A Twig_NodeInterface instance
*/
public function parse(Twig_Token $token)
{
@@ -47,52 +50,33 @@ public function parse(Twig_Token $token)
$name = $name !== null ? $name->getValue() : false;

$unique = $name !== false;
$reject = false;

$variableName = $stream->nextIf(\Twig_Token::NAME_TYPE);
$variableName = $variableName !== null ? $variableName->getValue() : false;

if ($name) {
$name = $this->blockPrefix . $reference . $name;
$reject = $this->parser->hasBlock($name);
if ($this->parser->hasBlock($name)) {
$this->bodyParse($stream, $name, $lineno);
return null;
}
} else {
$i = 0;
do {
$name = $this->blockPrefix . $reference . ($i++);
} while ($this->parser->hasBlock($name));
}

if (!$reject) {
$this->parser->setBlock($name, $block = new Node\Defer($name, new Twig_Node(array()), $lineno));
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);
}

if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) {
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
$value = $token->getValue();
$this->parser->setBlock($name, $block = new Node\Defer($name, new Twig_Node(array()), $lineno));
$this->parser->pushLocalScope();
$this->parser->pushBlockStack($name);

if ($value != $name) {
throw new Twig_Error_Syntax(sprintf("Expected enddefer for defer '$name' (but %s given)", $value), $stream->getCurrent()->getLine(), $stream->getFilename());
}
}
} else {
$body = new Twig_Node(array(
new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
));
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->bodyParse($stream, $name, $lineno);

if (!$reject) {
$block->setNode('body', $body);
$this->parser->popBlockStack();
$this->parser->popLocalScope();
}
$block->setNode('body', $body);
$this->parser->popBlockStack();
$this->parser->popLocalScope();

if ($reject) {
return null;
}
return new Node\DeferReference($name, $variableName, $unique, $reference, $lineno, $this->getTag());
}

@@ -108,4 +92,36 @@ public function getTag()
{
return 'defer';
}

/**
* @param Twig_TokenStream $stream
* @param string $name
* @param integer $lineno
* @return Twig_Node
*/
public function bodyParse(Twig_TokenStream $stream, $name, $lineno)
{
if ($stream->nextIf(Twig_Token::BLOCK_END_TYPE)) {
$body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
if ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) {
$value = $token->getValue();

if ($value != $name) {
throw new Twig_Error_Syntax(
sprintf("Expected enddefer for defer '$name' (but %s given)", $value),
$stream->getCurrent()->getLine(),
$stream->getFilename()
);
}
}
} else {
$body = new Twig_Node(
array(
new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
)
);
}
$stream->expect(Twig_Token::BLOCK_END_TYPE);
return $body;
}
}

0 comments on commit 28add35

Please sign in to comment.