NEW: Hooks tab in debugbar (#24992)

Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
This commit is contained in:
jyhere
2024-04-09 03:09:10 +02:00
committed by GitHub
parent 58e0837a7a
commit 4880ec1b77
6 changed files with 173 additions and 2 deletions

View File

@@ -50,6 +50,11 @@ class HookManager
// Array with instantiated classes
public $hooks = array();
/**
* @var array List of hooks called during this request
*/
public $hooksHistory = [];
// Array result
public $resArray = array();
// Printable result
@@ -155,6 +160,26 @@ class HookManager
*/
public function executeHooks($method, $parameters = array(), &$object = null, &$action = '')
{
global $debugbar;
if (is_object($debugbar) && get_class($debugbar) === 'DolibarrDebugBar') {
$trace = debug_backtrace();
if (isset($trace[0])) {
$hookInformations = [
'name' => $method,
'contexts' => $this->contextarray,
'file' => $trace[0]['file'],
'line' => $trace[0]['line'],
];
$hash = md5(serialize($hookInformations));
if (!empty($this->hooksHistory[$hash])) {
$this->hooksHistory[$hash]['count']++;
} else {
$hookInformations['count'] = 1;
$this->hooksHistory[$hash] = $hookInformations;
}
}
}
if (!is_array($this->hooks) || empty($this->hooks)) {
return 0; // No hook available, do nothing.
}

View File

@@ -0,0 +1,73 @@
<?php
use \DebugBar\DataCollector\RequestDataCollector;
/**
* DolRequestDataCollector class
*/
class DolHooksCollector extends RequestDataCollector
{
/**
* Collects the data from the collectors
*
* @return array
*/
public function collect()
{
/**
* @global $hookmanager HookManager
*/
global $hookmanager;
$data = ['hooks' => []];
if (empty($hookmanager->hooksHistory)) {
return $data;
}
$i = 0;
foreach ($hookmanager->hooksHistory as $key => $hookHistory) {
$i++;
$hookHistory['contexts'] = implode(', ', $hookHistory['contexts']);
$data['hooks']["[$i] {$hookHistory['name']}"] = $hookHistory;
// $data["[$key] {$hookHistory['name']}"] = "{$hookHistory['file']} (L{$hookHistory['line']}). Contexts: "
// . implode(', ', $hookHistory['contexts']);
}
$data['nb_of_hooks'] = count($data['hooks']);
return $data;
}
/**
* Return widget settings
*
* @return string[][]
*/
public function getWidgets()
{
global $langs;
$langs->load("other");
return [
$langs->transnoentities('Hooks') => [
"icon" => "tags",
"widget" => "PhpDebugBar.Widgets.HookListWidget",
"map" => "hooks.hooks",
"default" => "{}"
],
"{$langs->transnoentities('Hooks')}:badge" => [
"map" => "hooks.nb_of_hooks",
"default" => 0
]
];
}
/**
* @return string
*/
public function getName()
{
return 'hooks';
}
}

View File

@@ -172,7 +172,8 @@ class DolibarrCollector extends DataCollector implements Renderable, AssetProvid
{
return array(
'base_url' => dol_buildpath('/debugbar', 1),
'js' => 'js/widgets.js'
'js' => 'js/widgets.js',
'css' => 'css/widgets.css'
);
}
}

View File

@@ -35,6 +35,7 @@ dol_include_once('/debugbar/class/DataCollector/DolExceptionsCollector.php');
dol_include_once('/debugbar/class/DataCollector/DolQueryCollector.php');
dol_include_once('/debugbar/class/DataCollector/DolibarrCollector.php');
dol_include_once('/debugbar/class/DataCollector/DolLogsCollector.php');
dol_include_once('/debugbar/class/DataCollector/DolHooksCollector.php');
/**
* DolibarrDebugBar class
@@ -62,6 +63,7 @@ class DolibarrDebugBar extends DebugBar
//$this->addCollector(new DolExceptionsCollector());
$this->addCollector(new DolQueryCollector());
$this->addCollector(new DolibarrCollector());
$this->addCollector(new DolHooksCollector());
if (isModEnabled('syslog')) {
$this->addCollector(new DolLogsCollector());
}

View File

@@ -0,0 +1,45 @@
dl.phpdebugbar-widgets-kvlist.phpdebugbar-widgets-hooklist dt {
float: left;
width: 230px;
padding: 5px;
border-top: 1px solid #eee;
font-weight: bold;
clear: both;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
dl.phpdebugbar-widgets-kvlist.phpdebugbar-widgets-hooklist dt span {
font-weight: bold;
}
dl.phpdebugbar-widgets-kvlist dd {
margin-left: 240px;
padding: 5px;
border-top: 1px solid #eee;
cursor: pointer;
min-height: 17px;
}
dl.phpdebugbar-widgets-kvlist dd span {
display: inline-block;
}
dl.phpdebugbar-widgets-kvlist dd span strong{
font-weight: bold;
}
dl.phpdebugbar-widgets-kvlist dd span:nth-of-type(1) {
min-width: 550px;
margin-right: 20px;
}
dl.phpdebugbar-widgets-kvlist dd span:nth-of-type(2) {
min-width: 90px;
margin-right: 20px;
}
dl.phpdebugbar-widgets-kvlist dd span:nth-of-type(3) {
min-width: 80px;
margin-right: 20px;
}

View File

@@ -72,4 +72,29 @@
});
/**
* An extension of KVListWidget where the data represents a list
* of variables
*
* Options:
* - data
*/
var HookListWidget = PhpDebugBar.Widgets.HookListWidget = PhpDebugBar.Widgets.KVListWidget.extend({
className: csscls('widgets-kvlist widgets-hooklist'),
itemRenderer: function(dt, dd, key, object) {
$('<span />').attr('title', key).text(key).appendTo(dt);
dd.html('<span><strong>File: </strong> ' + object.file
+ '</span><span><strong>Line: </strong>' + object.line
+ '</span><span><strong>Count: </strong>' + object.count
+ '</span><span><strong>Contexts: </strong>' + (object.contexts === null || object.contexts === '' ? 'Not set' : object.contexts)
+ '</span>'
);
}
});
})(PhpDebugBar.$);