Lazy load twig collectors (#1600)

This commit is contained in:
Barry vd. Heuvel 2024-04-01 18:27:14 +02:00 committed by GitHub
parent c3f748952b
commit 1a27ae0250
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 32 deletions

View File

@ -4,12 +4,15 @@ namespace Barryvdh\Debugbar\Twig\Extension;
use DebugBar\Bridge\Twig\DebugTwigExtension;
use Illuminate\Foundation\Application;
use Twig\Environment;
/**
* Access debugbar debug in your Twig templates.
*/
class Debug extends DebugTwigExtension
{
protected $app;
/**
* Create a new debug extension.
*
@ -17,11 +20,16 @@ class Debug extends DebugTwigExtension
*/
public function __construct(Application $app)
{
$messagesCollector = null;
if ($app->bound('debugbar') && $app['debugbar']->hasCollector('messages')) {
$messagesCollector = $app['debugbar']['messages'];
$this->app = $app;
parent::__construct(null);
}
public function debug(Environment $env, $context)
{
if ($this->app->bound('debugbar') && $this->app['debugbar']->hasCollector('messages')) {
$this->messagesCollector = $this->app['debugbar']['messages'];
}
parent::__construct($messagesCollector);
return parent::debug($env, $context);
}
}

View File

@ -1,14 +0,0 @@
<?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
{
}
}

View File

@ -3,6 +3,7 @@
namespace Barryvdh\Debugbar\Twig\Extension;
use DebugBar\Bridge\Twig\MeasureTwigExtension;
use DebugBar\Bridge\Twig\MeasureTwigTokenParser;
use Illuminate\Foundation\Application;
/**
@ -23,28 +24,46 @@ class Stopwatch extends MeasureTwigExtension
*/
public function __construct(Application $app)
{
$timeCollector = null;
if ($app->bound('debugbar')) {
$this->debugbar = $app['debugbar'];
if ($app['debugbar']->hasCollector('time')) {
$timeCollector = $app['debugbar']['time'];
}
}
parent::__construct($timeCollector, 'stopwatch');
parent::__construct(null, 'stopwatch');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
}
public function getDebugbar()
{
return $this->debugbar;
}
public function getTokenParsers()
{
return [
/*
* {% measure foo %}
* Some stuff which will be recorded on the timeline
* {% endmeasure %}
*/
new MeasureTwigTokenParser(!is_null($this->debugbar), $this->tagName, $this->getName()),
];
}
public function startMeasure(...$arg)
{
if (!$this->debugbar || !$this->debugbar->hasCollector('time')) {
return;
}
$this->debugbar->getCollector('time')->startMeasure(...$arg);
}
public function stopMeasure(...$arg)
{
if (!$this->debugbar || !$this->debugbar->hasCollector('time')) {
return;
}
$this->debugbar->getCollector('time')->stopMeasure(...$arg);
}
}