diff --git a/src/Twig/Extension/Debug.php b/src/Twig/Extension/Debug.php index 42bc2cb3..a70b69a4 100644 --- a/src/Twig/Extension/Debug.php +++ b/src/Twig/Extension/Debug.php @@ -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 @@ -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] @@ -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; diff --git a/src/Twig/Extension/Dump.php b/src/Twig/Extension/Dump.php index b42fafa4..edd17ba0 100644 --- a/src/Twig/Extension/Dump.php +++ b/src/Twig/Extension/Dump.php @@ -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 @@ -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] @@ -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 = ''; diff --git a/src/Twig/Extension/Extension.php b/src/Twig/Extension/Extension.php new file mode 100644 index 00000000..282ecf03 --- /dev/null +++ b/src/Twig/Extension/Extension.php @@ -0,0 +1,10 @@ + */ -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('') @@ -28,11 +45,11 @@ public function compile(\Twig_Compiler $compiler) ->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"); } diff --git a/src/Twig/TokenParser/StopwatchTokenParser.php b/src/Twig/TokenParser/StopwatchTokenParser.php index cb197a20..896af499 100644 --- a/src/Twig/TokenParser/StopwatchTokenParser.php +++ b/src/Twig/TokenParser/StopwatchTokenParser.php @@ -9,7 +9,7 @@ * * @author Wouter J */ -class StopwatchTokenParser extends \Twig_TokenParser +class StopwatchTokenParser extends TokenParser { protected $debugbarAvailable; @@ -18,7 +18,10 @@ 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(); @@ -26,17 +29,31 @@ public function parse(\Twig_Token $token) // {% 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() ); @@ -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'); } diff --git a/src/Twig/TokenParser/TokenParser.php b/src/Twig/TokenParser/TokenParser.php new file mode 100644 index 00000000..4a0b6d1c --- /dev/null +++ b/src/Twig/TokenParser/TokenParser.php @@ -0,0 +1,10 @@ +