forked from barryvdh/laravel-debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented support for Twig 3 (barryvdh#1318)
* 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
1 parent
3372ed6
commit 119f0bc
Showing
8 changed files
with
102 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
* | ||
* @author Wouter J <[email protected]> | ||
*/ | ||
class StopwatchTokenParser extends \Twig_TokenParser | ||
class StopwatchTokenParser extends TokenParser | ||
{ | ||
protected $debugbarAvailable; | ||
|
||
|
@@ -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() | ||
); | ||
|
@@ -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'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |