StopwatchTokenParser.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\TokenParser;
  11. use Symfony\Bridge\Twig\Node\StopwatchNode;
  12. use Twig\Node\Expression\AssignNameExpression;
  13. use Twig\Token;
  14. use Twig\TokenParser\AbstractTokenParser;
  15. /**
  16. * Token Parser for the stopwatch tag.
  17. *
  18. * @author Wouter J <wouter@wouterj.nl>
  19. */
  20. class StopwatchTokenParser extends AbstractTokenParser
  21. {
  22. protected $stopwatchIsAvailable;
  23. public function __construct($stopwatchIsAvailable)
  24. {
  25. $this->stopwatchIsAvailable = $stopwatchIsAvailable;
  26. }
  27. public function parse(Token $token)
  28. {
  29. $lineno = $token->getLine();
  30. $stream = $this->parser->getStream();
  31. // {% stopwatch 'bar' %}
  32. $name = $this->parser->getExpressionParser()->parseExpression();
  33. $stream->expect(Token::BLOCK_END_TYPE);
  34. // {% endstopwatch %}
  35. $body = $this->parser->subparse(array($this, 'decideStopwatchEnd'), true);
  36. $stream->expect(Token::BLOCK_END_TYPE);
  37. if ($this->stopwatchIsAvailable) {
  38. return new StopwatchNode($name, $body, new AssignNameExpression($this->parser->getVarName(), $token->getLine()), $lineno, $this->getTag());
  39. }
  40. return $body;
  41. }
  42. public function decideStopwatchEnd(Token $token)
  43. {
  44. return $token->test('endstopwatch');
  45. }
  46. public function getTag()
  47. {
  48. return 'stopwatch';
  49. }
  50. }