Skip to content

Commit

Permalink
show templates rendering in the timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 14, 2021
1 parent 5ce973d commit cc4e990
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions code/Middleware/DebugBarMiddleware.php
Expand Up @@ -3,6 +3,8 @@
namespace LeKoala\DebugBar\Middleware;

use LeKoala\DebugBar\DebugBar;
use LeKoala\DebugBar\Extension\ProxyDBExtension;
use LeKoala\DebugBar\Proxy\SSViewerProxy;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
Expand Down
53 changes: 49 additions & 4 deletions code/Proxy/SSViewerProxy.php
Expand Up @@ -32,8 +32,32 @@ class SSViewerProxy extends SSViewer
*/
public function process($item, $arguments = null, $inheritedScope = null)
{
self::trackTemplateUsed($this->chosen);
return parent::process($item, $arguments, $inheritedScope);
$templateName = self::normalizeTemplateName($this->chosen);
self::trackTemplateUsed($templateName);

DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($templateName) {
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */
$timeData = $debugBar->getCollector('time');
if (!$timeData) {
return;
}
$timeData->startMeasure($templateName, $templateName);
});

$result = parent::process($item, $arguments, $inheritedScope);

DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($templateName) {
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */
$timeData = $debugBar->getCollector('time');
if (!$timeData) {
return;
}
if ($timeData->hasStartedMeasure($templateName)) {
$timeData->stopMeasure($templateName);
}
});

return $result;
}

/**
Expand All @@ -47,12 +71,33 @@ public static function getTemplatesUsed()
}

/**
* Remove the base path from a template file path and track its use
* Reset the array
*
* @return void
*/
public static function resetTemplatesUsed()
{
static::$allTemplates = [];
}

/**
* Helps tracking the use of templates
*
* @param string $templateName
*/
protected static function trackTemplateUsed($templateName)
{
static::$allTemplates[] = str_ireplace(BASE_PATH, '', $templateName);
static::$allTemplates[] = $templateName;
}

/**
* Remove base path from template
*
* @param string $templateName
* @return string
*/
protected static function normalizeTemplateName($templateName)
{
return str_ireplace(BASE_PATH, '', $templateName);
}
}

0 comments on commit cc4e990

Please sign in to comment.