Created
August 28, 2017 09:16
-
-
Save ahilles107/efdbc1b471a73a180ae54023ef24cbb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SWP\Parser; | |
use SWP\Node\GimmeNode; | |
/** | |
* Parser for gimme/endgimme blocks. | |
*/ | |
class GimmeTokenParser extends \Twig_TokenParser | |
{ | |
/** | |
* Gets the tag name associated with this token parser. | |
* | |
* @return string The tag name | |
*/ | |
public function getTag() | |
{ | |
return 'gimme'; | |
} | |
/** | |
* @param \Twig_Token $token | |
* | |
* @return bool | |
*/ | |
public function decideCacheEnd(\Twig_Token $token) | |
{ | |
return $token->test('endgimme'); | |
} | |
/** | |
* Parses a token and returns a node. | |
* | |
* @return \Twig_Node A Twig_Node instance | |
* | |
* @throws \Twig_Error_Syntax | |
*/ | |
public function parse(\Twig_Token $token) | |
{ | |
$lineNumber = $token->getLine(); | |
$stream = $this->parser->getStream(); | |
// assign passed string (ex. article) to variable | |
$annotation = $this->parser->getExpressionParser()->parseAssignmentExpression(); | |
$parameters = null; | |
// parse optional data passed after `with` keyword | |
if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) { | |
$parameters = $this->parser->getExpressionParser()->parseExpression(); | |
} | |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | |
$body = $this->parser->subparse([$this, 'decideCacheEnd'], true); | |
$stream->expect(\Twig_Token::BLOCK_END_TYPE); | |
// pass all parsed data to Node class. | |
return new GimmeNode($annotation, $parameters, $body, $lineNumber, $this->getTag()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment