CoreFiles by mySites.guru
MeasureTwigTokenParser.php78 lines, 1,705 bytes, from Joomla_5.2.0-alpha2-Alpha-Full_Package.zip
Raw History Compare with 5.2.0-alpha1
md5
279c800ac0351287666491c26eebdd27
sha1
55e2fff0b05d11a4366ee69d8214bc782f1e16c8
sha256
91dab70ba44a8c5d91c7727746cdb5244bb70c0dd8094b8cdadc3078fed9b6bf
<?php

namespace DebugBar\Bridge\Twig;

use Twig\Node\Expression\AssignNameExpression;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

/**
 * Token Parser for the measure tag.
 * Based on Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
 */
class MeasureTwigTokenParser extends AbstractTokenParser
{
    /**
     * @var string
     */
    private $extName;

    /**
     * @var string
     */
    private $tagName;

    /**
     * @var bool
     */
    private $enabled;

    /**
     *
     * @param string $tagName
     */
    public function __construct($enabled, $tagName, $extName = null)
    {
        $this->enabled = $enabled;
        $this->tagName = $tagName;
        $this->extName = $extName;
    }

    public function parse(Token $token)
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();

        // {% measure 'bar' %}
        $name = $this->parser->getExpressionParser()->parseExpression();

        $stream->expect(Token::BLOCK_END_TYPE);

        // {% endmeasure %}
        $body = $this->parser->subparse([$this, 'decideMeasureEnd'], true);
        $stream->expect(Token::BLOCK_END_TYPE);

        if ($this->enabled) {
            return new MeasureTwigNode(
                $name,
                $body,
                new AssignNameExpression($this->parser->getVarName(), $token->getLine()),
                $lineno,
                $this->getTag(),
                $this->extName
            );
        }

        return $body;
    }

    public function getTag()
    {
        return $this->tagName;
    }

    public function decideMeasureEnd(Token $token)
    {
        return $token->test('end'.$this->getTag());
    }
}

The same content is in 22 releases, Joomla 5.2.0-alpha2 to 5.3.0-alpha3. See them all, or every version of this file.