winter/modules/cms/twig/ComponentTokenParser.php

71 lines
1.9 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Cms\Twig;
use Twig_Node;
2014-05-14 23:24:20 +10:00
use Twig_Token;
use Twig_TokenParser;
use Twig_Error_Syntax;
2014-05-14 23:24:20 +10:00
/**
* Parser for the `{% component %}` Twig tag.
2014-05-14 23:24:20 +10:00
*
* {% component "pluginComponent" %}
2014-05-14 23:24:20 +10:00
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class ComponentTokenParser extends Twig_TokenParser
{
/**
* Parses a token and returns a node.
*
* @param Twig_Token $token A Twig_Token instance
2017-05-13 09:34:20 +10:00
* @return Twig_Node A Twig_Node instance
2014-05-14 23:24:20 +10:00
*/
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
2014-05-14 23:24:20 +10:00
$stream = $this->parser->getStream();
2014-05-14 23:24:20 +10:00
$name = $this->parser->getExpressionParser()->parseExpression();
$paramNames = [];
$nodes = [$name];
$end = false;
while (!$end) {
$current = $stream->next();
switch ($current->getType()) {
case Twig_Token::NAME_TYPE:
$paramNames[] = $current->getValue();
$stream->expect(Twig_Token::OPERATOR_TYPE, '=');
$nodes[] = $this->parser->getExpressionParser()->parseExpression();
break;
case Twig_Token::BLOCK_END_TYPE:
$end = true;
break;
default:
2014-10-11 01:42:04 +02:00
throw new Twig_Error_Syntax(
sprintf('Invalid syntax in the component tag. Line %s', $lineno),
2014-10-11 01:42:04 +02:00
$stream->getCurrent()->getLine(),
2017-07-26 17:48:00 +10:00
$stream->getSourceContext()
2014-10-11 01:42:04 +02:00
);
break;
}
}
return new ComponentNode(new Twig_Node($nodes), $paramNames, $token->getLine(), $this->getTag());
2014-05-14 23:24:20 +10:00
}
/**
* Gets the tag name associated with this token parser.
*
* @return string The tag name
*/
public function getTag()
{
return 'component';
}
2014-10-11 01:42:04 +02:00
}