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

64 lines
1.6 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;
2017-05-30 11:15:38 +01:00
use Twig\Node\Expression\AssignNameExpression;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
2013-05-06 20:11:53 +01:00
/**
* Token Parser for the stopwatch tag.
*
* @author Wouter J <wouter@wouterj.nl>
*/
2017-05-30 11:15:38 +01:00
class StopwatchTokenParser extends AbstractTokenParser
2013-05-06 20:11:53 +01:00
{
protected $stopwatchIsAvailable;
public function __construct($stopwatchIsAvailable)
{
$this->stopwatchIsAvailable = $stopwatchIsAvailable;
}
2017-05-30 11:15:38 +01:00
public function parse(Token $token)
2013-05-06 20:11:53 +01:00
{
$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
2017-05-30 11:15:38 +01:00
$stream->expect(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);
2017-05-30 11:15:38 +01:00
$stream->expect(Token::BLOCK_END_TYPE);
2013-08-11 19:06:26 +01:00
2013-05-06 20:11:53 +01:00
if ($this->stopwatchIsAvailable) {
2017-05-30 11:15:38 +01:00
return new StopwatchNode($name, $body, new AssignNameExpression($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
2017-05-30 11:15:38 +01:00
public function decideStopwatchEnd(Token $token)
2013-05-06 20:11:53 +01:00
{
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';
}
}