From c3f748952b1b26fa6178d3bd5e0b9c66b704e2bf Mon Sep 17 00:00:00 2001 From: erikn69 Date: Mon, 1 Apr 2024 11:01:26 -0500 Subject: [PATCH] Upstream twig extensions (#1599) --- src/Twig/Extension/Debug.php | 88 ++----------------- src/Twig/Extension/Dump.php | 84 +----------------- src/Twig/Extension/Stopwatch.php | 31 +++---- src/Twig/Node/Node.php | 14 --- src/Twig/Node/StopwatchNode.php | 56 ------------ src/Twig/TokenParser/StopwatchTokenParser.php | 77 ---------------- src/Twig/TokenParser/TokenParser.php | 14 --- 7 files changed, 22 insertions(+), 342 deletions(-) delete mode 100644 src/Twig/Node/Node.php delete mode 100644 src/Twig/Node/StopwatchNode.php delete mode 100644 src/Twig/TokenParser/StopwatchTokenParser.php delete mode 100644 src/Twig/TokenParser/TokenParser.php diff --git a/src/Twig/Extension/Debug.php b/src/Twig/Extension/Debug.php index a70b69a4f..678a807a5 100644 --- a/src/Twig/Extension/Debug.php +++ b/src/Twig/Extension/Debug.php @@ -2,98 +2,26 @@ namespace Barryvdh\Debugbar\Twig\Extension; +use DebugBar\Bridge\Twig\DebugTwigExtension; use Illuminate\Foundation\Application; -use Twig_Environment; -use Twig_Extension; -use Twig_SimpleFunction; /** - * Access Laravels auth class in your Twig templates. + * Access debugbar debug in your Twig templates. */ -class Debug extends Extension +class Debug extends DebugTwigExtension { /** - * @var \Barryvdh\Debugbar\LaravelDebugbar - */ - protected $debugbar; - - /** - * Create a new auth extension. + * Create a new debug extension. * * @param \Illuminate\Foundation\Application $app */ public function __construct(Application $app) { - if ($app->bound('debugbar')) { - $this->debugbar = $app['debugbar']; - } else { - $this->debugbar = null; - } - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'Laravel_Debugbar_Debug'; - } - - /** - * {@inheritDoc} - */ - public function getFunctions() - { - // Maintain compatibility with Twig 2 and 3. - $simpleFunction = 'Twig_SimpleFunction'; - - if (!class_exists($simpleFunction)) { - $simpleFunction = '\Twig\TwigFunction'; - } - - return [ - new $simpleFunction( - 'debug', - [$this, 'debug'], - ['needs_context' => true, 'needs_environment' => true] - ), - ]; - } - - /** - * Based on Twig_Extension_Debug / twig_var_dump - * (c) 2011 Fabien Potencier - * - * @param \Twig_Environment|\Twig\Environment $env - * @param $context - */ - public function debug($env, $context) - { - if (!$env->isDebug() || !$this->debugbar) { - return; - } - - $count = func_num_args(); - if (2 === $count) { - $data = []; - foreach ($context as $key => $value) { - if (is_object($value)) { - if (method_exists($value, 'toArray')) { - $data[$key] = $value->toArray(); - } else { - $data[$key] = "Object (" . get_class($value) . ")"; - } - } else { - $data[$key] = $value; - } - } - $this->debugbar->addMessage($data); - } else { - for ($i = 2; $i < $count; $i++) { - $this->debugbar->addMessage(func_get_arg($i)); - } + $messagesCollector = null; + if ($app->bound('debugbar') && $app['debugbar']->hasCollector('messages')) { + $messagesCollector = $app['debugbar']['messages']; } - return; + parent::__construct($messagesCollector); } } diff --git a/src/Twig/Extension/Dump.php b/src/Twig/Extension/Dump.php index edd17ba03..0a3be7b71 100644 --- a/src/Twig/Extension/Dump.php +++ b/src/Twig/Extension/Dump.php @@ -2,91 +2,11 @@ namespace Barryvdh\Debugbar\Twig\Extension; -use DebugBar\DataFormatter\DataFormatterInterface; +use DebugBar\Bridge\Twig\DumpTwigExtension; /** * Dump variables using the DataFormatter */ -class Dump extends Extension +class Dump extends DumpTwigExtension { - /** - * @var \DebugBar\DataFormatter\DataFormatter - */ - protected $formatter; - - /** - * Create a new auth extension. - * - * @param \DebugBar\DataFormatter\DataFormatterInterface $formatter - */ - public function __construct(DataFormatterInterface $formatter) - { - $this->formatter = $formatter; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'Laravel_Debugbar_Dump'; - } - - /** - * {@inheritDoc} - */ - public function getFunctions() - { - // Maintain compatibility with Twig 2 and 3. - $simpleFunction = '\Twig_SimpleFunction'; - - if (!class_exists($simpleFunction)) { - $simpleFunction = '\Twig\TwigFunction'; - } - - return [ - new $simpleFunction( - 'dump', - [$this, 'dump'], - ['is_safe' => ['html'], 'needs_context' => true, 'needs_environment' => true] - ), - ]; - } - - /** - * Based on Twig_Extension_Debug / twig_var_dump - * (c) 2011 Fabien Potencier - * - * @param \Twig_Environment|\Twig\Environment $env - * @param $context - * - * @return string - */ - public function dump($env, $context) - { - $output = ''; - - $count = func_num_args(); - if (2 === $count) { - $data = []; - foreach ($context as $key => $value) { - if (is_object($value)) { - if (method_exists($value, 'toArray')) { - $data[$key] = $value->toArray(); - } else { - $data[$key] = "Object (" . get_class($value) . ")"; - } - } else { - $data[$key] = $value; - } - } - $output .= $this->formatter->formatVar($data); - } else { - for ($i = 2; $i < $count; $i++) { - $output .= $this->formatter->formatVar(func_get_arg($i)); - } - } - - return '
' . $output . '
'; - } } diff --git a/src/Twig/Extension/Stopwatch.php b/src/Twig/Extension/Stopwatch.php index f99401672..4b0d24db8 100644 --- a/src/Twig/Extension/Stopwatch.php +++ b/src/Twig/Extension/Stopwatch.php @@ -2,14 +2,14 @@ namespace Barryvdh\Debugbar\Twig\Extension; -use Barryvdh\Debugbar\Twig\TokenParser\StopwatchTokenParser; +use DebugBar\Bridge\Twig\MeasureTwigExtension; use Illuminate\Foundation\Application; /** - * Access Laravels auth class in your Twig templates. + * Access debugbar time measures in your Twig templates. * Based on Symfony\Bridge\Twig\Extension\StopwatchExtension */ -class Stopwatch extends Extension +class Stopwatch extends MeasureTwigExtension { /** * @var \Barryvdh\Debugbar\LaravelDebugbar @@ -17,17 +17,22 @@ class Stopwatch extends Extension protected $debugbar; /** - * Create a new auth extension. + * Create a new time measure extension. * * @param \Illuminate\Foundation\Application $app */ public function __construct(Application $app) { + $timeCollector = null; if ($app->bound('debugbar')) { $this->debugbar = $app['debugbar']; - } else { - $this->debugbar = null; + + if ($app['debugbar']->hasCollector('time')) { + $timeCollector = $app['debugbar']['time']; + } } + + parent::__construct($timeCollector, 'stopwatch'); } /** @@ -35,19 +40,7 @@ public function __construct(Application $app) */ public function getName() { - return 'stopwatch'; - } - - public function getTokenParsers() - { - return [ - /* - * {% stopwatch foo %} - * Some stuff which will be recorded on the timeline - * {% endstopwatch %} - */ - new StopwatchTokenParser($this->debugbar !== null), - ]; + return static::class; } public function getDebugbar() diff --git a/src/Twig/Node/Node.php b/src/Twig/Node/Node.php deleted file mode 100644 index f35ea7b7a..000000000 --- a/src/Twig/Node/Node.php +++ /dev/null @@ -1,14 +0,0 @@ - - */ -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( - $name, - $body, - $var, - $lineno = 0, - $tag = null - ) { - parent::__construct(['body' => $body, 'name' => $name, 'var' => $var], [], $lineno, $tag); - } - - /** - * @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(sprintf("\$this->env->getExtension('%s')->getDebugbar()->startMeasure(", $extension)) - ->subcompile($this->getNode('var')) - ->raw(");\n") - ->subcompile($this->getNode('body')) - ->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 deleted file mode 100644 index 896af4992..000000000 --- a/src/Twig/TokenParser/StopwatchTokenParser.php +++ /dev/null @@ -1,77 +0,0 @@ - - */ -class StopwatchTokenParser extends TokenParser -{ - protected $debugbarAvailable; - - public function __construct($debugbarAvailable) - { - $this->debugbarAvailable = $debugbarAvailable; - } - - /** - * @param \Twig_Token|\Twig\Token $token - */ - public function parse($token) - { - $lineno = $token->getLine(); - $stream = $this->parser->getStream(); - - // {% stopwatch 'bar' %} - $name = $this->parser->getExpressionParser()->parseExpression(); - - // 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($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, - $assignNameExpression, - $lineno, - $this->getTag() - ); - } - - return $body; - } - - public function getTag() - { - return 'stopwatch'; - } - - /** - * @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 deleted file mode 100644 index 2ff179628..000000000 --- a/src/Twig/TokenParser/TokenParser.php +++ /dev/null @@ -1,14 +0,0 @@ -