1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-07-24 18:22:03 +02:00

Allow overwriting the trace finder on Messages (#758)

This commit is contained in:
cesarreyes3
2025-03-28 01:26:49 -05:00
committed by GitHub
parent e5b12b716d
commit e090910409

View File

@@ -30,6 +30,12 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
/** @var bool */
protected $collectFile = false;
/** @var int */
protected $backtraceLimit = 5;
/** @var array */
protected $backtraceExcludePaths = ['/vendor/'];
/**
* @param string $name
*/
@@ -44,6 +50,45 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
$this->collectFile = $enabled;
}
/**
* @param int $limit
*
* @return void
*/
public function limitBacktrace($limit)
{
$this->backtraceLimit = $limit;
}
/**
* Set paths to exclude from the backtrace
*
* @param array $excludePaths Array of file paths to exclude from backtrace
*/
public function addBacktraceExcludePaths($excludePaths)
{
$this->backtraceExcludePaths = array_merge($this->backtraceExcludePaths, $excludePaths);
}
/**
* Check if the given file is to be excluded from analysis
*
* @param string $file
* @return bool
*/
protected function fileIsInExcludedPath($file)
{
$normalizedPath = str_replace('\\', '/', $file);
foreach ($this->backtraceExcludePaths as $excludedPath) {
if (strpos($normalizedPath, $excludedPath) !== false) {
return true;
}
}
return false;
}
/**
* @param string|null $messageHtml
* @param mixed $message
@@ -60,6 +105,24 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
return $messageHtml;
}
/**
* @param array $stacktrace
*
* @return array
*/
protected function getStackTraceItem($stacktrace)
{
foreach ($stacktrace as $trace) {
if (!isset($trace['file']) || $this->fileIsInExcludedPath($trace['file'])) {
continue;
}
return $trace;
}
return $stacktrace[0];
}
/**
* Adds a message
*
@@ -83,16 +146,7 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
$stackItem = [];
if ($this->collectFile) {
$stacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
$stackItem = $stacktrace[0];
foreach ($stacktrace as $trace) {
if (!isset($trace['file']) || strpos($trace['file'], '/vendor/') !== false) {
continue;
}
$stackItem = $trace;
break;
}
$stackItem = $this->getStackTraceItem(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->backtraceLimit));
}
$this->messages[] = array(