Skip to content

Commit

Permalink
Lazy load twig collectors (#1600)
Browse files Browse the repository at this point in the history
barryvdh authored Apr 1, 2024
1 parent c3f7489 commit 1a27ae0
Showing 3 changed files with 45 additions and 32 deletions.
16 changes: 12 additions & 4 deletions src/Twig/Extension/Debug.php
Original file line number Diff line number Diff line change
@@ -4,24 +4,32 @@

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.
*
* @param \Illuminate\Foundation\Application $app
*/
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);
}
}
14 changes: 0 additions & 14 deletions src/Twig/Extension/Extension.php

This file was deleted.

47 changes: 33 additions & 14 deletions src/Twig/Extension/Stopwatch.php
Original file line number Diff line number Diff line change
@@ -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');
}

/**
* {@inheritDoc}
*/
public function getName()
{
return static::class;
parent::__construct(null, 'stopwatch');
}


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);
}
}

0 comments on commit 1a27ae0

Please sign in to comment.