Skip to content

Commit

Permalink
Implemented support for Twig 3 (barryvdh#1318)
Browse files Browse the repository at this point in the history
* Implemented support for Twig 3

* Fixed v2 namespace

* Use the same notation everywhere

* Removed import

* Use clearer naming

* Fixed another Twig_Token param

* Fixed StopwatchTokenParser

* Fixed namespaces

* Renamed variable

* Refactored Twig compatibility to common base classes

* Refactor + Stopwatch fix
  • Loading branch information
tobias-kuendig authored Aug 3, 2022
1 parent 3372ed6 commit 119f0bc
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 25 deletions.
15 changes: 11 additions & 4 deletions src/Twig/Extension/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Access Laravels auth class in your Twig templates.
*/
class Debug extends Twig_Extension
class Debug extends Extension
{
/**
* @var \Barryvdh\Debugbar\LaravelDebugbar
Expand Down Expand Up @@ -44,8 +44,15 @@ public function getName()
*/
public function getFunctions()
{
// Maintain compatibility with Twig 2 and 3.
$simpleFunction = 'Twig_SimpleFunction';

if (!class_exists($simpleFunction)) {
$simpleFunction = '\Twig\TwigFunction';
}

return [
new Twig_SimpleFunction(
new $simpleFunction(
'debug',
[$this, 'debug'],
['needs_context' => true, 'needs_environment' => true]
Expand All @@ -57,10 +64,10 @@ public function getFunctions()
* Based on Twig_Extension_Debug / twig_var_dump
* (c) 2011 Fabien Potencier
*
* @param Twig_Environment $env
* @param \Twig_Environment|\Twig\Environment $env
* @param $context
*/
public function debug(Twig_Environment $env, $context)
public function debug($env, $context)
{
if (!$env->isDebug() || !$this->debugbar) {
return;
Expand Down
18 changes: 11 additions & 7 deletions src/Twig/Extension/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
namespace Barryvdh\Debugbar\Twig\Extension;

use DebugBar\DataFormatter\DataFormatterInterface;
use Twig_Environment;
use Twig_Extension;
use Twig_SimpleFunction;

/**
* Dump variables using the DataFormatter
*/
class Dump extends Twig_Extension
class Dump extends Extension
{
/**
* @var \DebugBar\DataFormatter\DataFormatter
Expand Down Expand Up @@ -40,8 +37,15 @@ public function getName()
*/
public function getFunctions()
{
// Maintain compatibility with Twig 2 and 3.
$simpleFunction = '\Twig_SimpleFunction';

if (!class_exists($simpleFunction)) {
$simpleFunction = '\Twig\TwigFunction';
}

return [
new Twig_SimpleFunction(
new $simpleFunction(
'dump',
[$this, 'dump'],
['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true]
Expand All @@ -53,12 +57,12 @@ public function getFunctions()
* Based on Twig_Extension_Debug / twig_var_dump
* (c) 2011 Fabien Potencier
*
* @param Twig_Environment $env
* @param \Twig_Environment|\Twig\Environment $env
* @param $context
*
* @return string
*/
public function dump(Twig_Environment $env, $context)
public function dump($env, $context)
{
$output = '';

Expand Down
10 changes: 10 additions & 0 deletions src/Twig/Extension/Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Barryvdh\Debugbar\Twig\Extension;

// Maintain compatibility with Twig 2 and 3.
if (class_exists('\Twig_Extension')) {
abstract class Extension extends \Twig_Extension {}
} else {
abstract class Extension extends \Twig\Extension\AbstractExtension {}
}
3 changes: 1 addition & 2 deletions src/Twig/Extension/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

use Barryvdh\Debugbar\Twig\TokenParser\StopwatchTokenParser;
use Illuminate\Foundation\Application;
use Twig_Extension;

/**
* Access Laravels auth class in your Twig templates.
* Based on Symfony\Bridge\Twig\Extension\StopwatchExtension
*/
class Stopwatch extends Twig_Extension
class Stopwatch extends Extension
{
/**
* @var \Barryvdh\Debugbar\LaravelDebugbar
Expand Down
10 changes: 10 additions & 0 deletions src/Twig/Node/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Barryvdh\Debugbar\Twig\Node;

// Maintain compatibility with Twig 2 and 3.
if (class_exists('\Twig_Node')) {
abstract class Node extends \Twig_Node {}
} else {
abstract class Node extends \Twig\Node\Node {}
}
29 changes: 23 additions & 6 deletions src/Twig/Node/StopwatchNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,49 @@
*
* @author Wouter J <[email protected]>
*/
class StopwatchNode extends \Twig_Node
class StopwatchNode extends Node
{
/**
* @param \Twig_NodeInterface|\Twig\Node\Node $name
* @param $body
* @param \Twig_Node_Expression_AssignName|\Twig\Node\Expression\AssignNameExpression $var
* @param $lineno
* @param $tag
*/
public function __construct(
\Twig_NodeInterface $name,
$name,
$body,
\Twig_Node_Expression_AssignName $var,
$var,
$lineno = 0,
$tag = null
) {
parent::__construct(['body' => $body, 'name' => $name, 'var' => $var], [], $lineno, $tag);
}

public function compile(\Twig_Compiler $compiler)
/**
* @param \Twig_Compiler|\Twig\Compiler $env
* @return void
*/
public function compile($compiler)
{
// Maintain compatibility with Twig 2 and 3.
$extension = \Barryvdh\Debugbar\Twig\Extension\Stopwatch::class;
if (class_exists('\Twig_Node')) {
$extension = 'stopwatch';
}

$compiler
->addDebugInfo($this)
->write('')
->subcompile($this->getNode('var'))
->raw(' = ')
->subcompile($this->getNode('name'))
->write(";\n")
->write("\$this->env->getExtension('stopwatch')->getDebugbar()->startMeasure(")
->write(sprintf("\$this->env->getExtension('%s')->getDebugbar()->startMeasure(", $extension))
->subcompile($this->getNode('var'))
->raw(");\n")
->subcompile($this->getNode('body'))
->write("\$this->env->getExtension('stopwatch')->getDebugbar()->stopMeasure(")
->write(sprintf("\$this->env->getExtension('%s')->getDebugbar()->stopMeasure(", $extension))
->subcompile($this->getNode('var'))
->raw(");\n");
}
Expand Down
32 changes: 26 additions & 6 deletions src/Twig/TokenParser/StopwatchTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Wouter J <[email protected]>
*/
class StopwatchTokenParser extends \Twig_TokenParser
class StopwatchTokenParser extends TokenParser
{
protected $debugbarAvailable;

Expand All @@ -18,25 +18,42 @@ public function __construct($debugbarAvailable)
$this->debugbarAvailable = $debugbarAvailable;
}

public function parse(\Twig_Token $token)
/**
* @param \Twig_Token|\Twig\Token $token
*/
public function parse($token)
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();

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

$stream->expect(\Twig_Token::BLOCK_END_TYPE);
// Maintain compatibility with Twig 2 and 3.
if (class_exists("\Twig_Token")) {
$blockEndType = \Twig_Token::BLOCK_END_TYPE;
} else {
$blockEndType = \Twig\Token::BLOCK_END_TYPE;
}

$stream->expect($blockEndType);

// {% endstopwatch %}
$body = $this->parser->subparse([$this, 'decideStopwatchEnd'], true);
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
$stream->expect($blockEndType);

// Maintain compatibility with Twig 2 and 3.
if (class_exists("\Twig_Node_Expression_AssignName")) {
$assignNameExpression = new \Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine());
} else {
$assignNameExpression = new \Twig\Node\Expression\AssignNameExpression($this->parser->getVarName(), $token->getLine());
}

if ($this->debugbarAvailable) {
return new StopwatchNode(
$name,
$body,
new \Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()),
$assignNameExpression,
$lineno,
$this->getTag()
);
Expand All @@ -50,7 +67,10 @@ public function getTag()
return 'stopwatch';
}

public function decideStopwatchEnd(\Twig_Token $token)
/**
* @param \Twig_Token|\Twig\Token $token
*/
public function decideStopwatchEnd($token)
{
return $token->test('endstopwatch');
}
Expand Down
10 changes: 10 additions & 0 deletions src/Twig/TokenParser/TokenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Barryvdh\Debugbar\Twig\TokenParser;

// Maintain compatibility with Twig 2 and 3.
if (class_exists('\Twig_TokenParser')) {
abstract class TokenParser extends \Twig_TokenParser {}
} else {
abstract class TokenParser extends \Twig\TokenParser\AbstractTokenParser {}
}

0 comments on commit 119f0bc

Please sign in to comment.