Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active September 28, 2020 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/2bd944a4a9797ce902853a33f61687a7 to your computer and use it in GitHub Desktop.
Save hissy/2bd944a4a9797ce902853a33f61687a7 to your computer and use it in GitHub Desktop.
#concrete5 Visualize rendering time of each blocks with DebugBar
<?php
/**
* First, install the debug bar package. @link: https://github.com/concrete5cojp/concrete5_debugbar
* Then, add these code in your application/bootstrap/app.php
*/
if ($app->isInstalled()) {
$director = $app->make(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class);
$debugBar = $app->make(\Concrete\Core\Package\PackageService::class)->getByHandle('concrete5_debugbar');
if (is_object($debugBar) && $debugBar->isPackageInstalled()) {
$director->addListener('on_block_load', static function ($event) use ($app) {
$bID = $event->getArgument('bID');
$btHandle = $event->getArgument('btHandle');
$app->make('debugbar/time')->startMeasure(sprintf('block_%d', $bID), sprintf('Rendering %s block (bID: %d)', $btHandle, $bID));
});
$director->addListener('on_block_output', static function ($event) use ($app) {
/** @var \Concrete\Core\Block\Block $b */
$b = $event->getBlock();
$bID = $b->getBlockID();
$app->make('debugbar/time')->stopMeasure(sprintf('block_%d', $bID), [
'arHandle' => $b->getAreaHandle(),
'cacheBlockOutput' => $b->cacheBlockOutput(),
]);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment