This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bridge/Twig/TokenParser/StopwatchTokenParser.php

61 lines
1.5 KiB
PHP
Raw Normal View History

2013-05-06 20:11:53 +01:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\TokenParser;
use Symfony\Bridge\Twig\Node\StopwatchNode;
/**
* Token Parser for the stopwatch tag.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class StopwatchTokenParser extends \Twig_TokenParser
{
protected $stopwatchIsAvailable;
public function __construct($stopwatchIsAvailable)
{
$this->stopwatchIsAvailable = $stopwatchIsAvailable;
}
public function parse(\Twig_Token $token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
2013-08-11 19:06:26 +01:00
// {% stopwatch 'bar' %}
$name = $this->parser->getExpressionParser()->parseExpression();
2013-05-06 20:11:53 +01:00
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
2013-08-11 19:06:26 +01:00
// {% endstopwatch %}
2013-05-06 20:11:53 +01:00
$body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
2013-08-11 19:06:26 +01:00
2013-05-06 20:11:53 +01:00
if ($this->stopwatchIsAvailable) {
return new StopwatchNode($name, $body, new \Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $lineno, $this->getTag());
2013-05-06 20:11:53 +01:00
}
return $body;
}
2013-08-11 19:06:26 +01:00
2013-05-06 20:11:53 +01:00
public function decideStopwatchEnd(\Twig_Token $token)
{
return $token->test('endstopwatch');
}
2013-08-11 19:06:26 +01:00
2013-05-06 20:11:53 +01:00
public function getTag()
{
return 'stopwatch';
}
}