Skip to content

Commit

Permalink
Merge branch 'master' into feat-debugbar-themes
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Jan 26, 2025
2 parents 94adc43 + 30105e7 commit ddb2979
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 23 deletions.
5 changes: 3 additions & 2 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

'enabled' => env('DEBUGBAR_ENABLED', null),
'hide_empty_tabs' => false, // Hide tabs until they have content
'hide_empty_tabs' => true, // Hide tabs until they have content
'except' => [
'telescope*',
'horizon*',
Expand Down Expand Up @@ -168,7 +168,7 @@
'log' => true, // Logs from Monolog (merged in messages if enabled)
'db' => true, // Show database (PDO) queries and bindings
'views' => true, // Views with their data
'route' => true, // Current route information
'route' => false, // Current route information
'auth' => false, // Display Laravel authentication status
'gate' => true, // Display Laravel Gate checks
'session' => true, // Display session data
Expand Down Expand Up @@ -250,6 +250,7 @@
'hiddens' => [], // Hides sensitive values using array paths
],
'symfony_request' => [
'label' => true, // Show route on bar
'hiddens' => [], // Hides sensitive values using array paths, example: request_request.password
],
'events' => [
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Total Downloads](https://img.shields.io/packagist/dt/barryvdh/laravel-debugbar?label=Downloads)](https://packagist.org/packages/barryvdh/laravel-debugbar)
[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](https://fruitcake.nl/)

This is a package to integrate [PHP Debug Bar](http://phpdebugbar.com/) with Laravel.
This is a package to integrate [PHP Debug Bar](https://github.com/php-debugbar/php-debugbar) with Laravel.
It includes a ServiceProvider to register the debugbar and attach it to the output. You can publish assets and configure it through Laravel.
It bootstraps some Collectors to work with Laravel and implements a couple custom DataCollectors, specific for Laravel.
It is configured to display Redirects and (jQuery) Ajax Requests. (Shown in a dropdown)
Expand Down
134 changes: 126 additions & 8 deletions src/DataCollector/RequestCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\Renderable;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -20,7 +23,7 @@ class RequestCollector extends DataCollector implements DataCollectorInterface,
{
/** @var \Symfony\Component\HttpFoundation\Request $request */
protected $request;
/** @var \Symfony\Component\HttpFoundation\Request $response */
/** @var \Symfony\Component\HttpFoundation\Response $response */
protected $response;
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
protected $session;
Expand Down Expand Up @@ -63,14 +66,33 @@ public function getName()
*/
public function getWidgets()
{
return [
$widgets = [
"request" => [
"icon" => "tags",
"widget" => "PhpDebugBar.Widgets.HtmlVariableListWidget",
"map" => "request",
"default" => "{}"
]
];

if (Config::get('debugbar.options.request.label', true)) {
$widgets['currentrequest'] = [
"icon" => "share",
"tooltip" => [
'status' => $this->response->getStatusCode()
],
"map" => "request.uri",
"link" => "request",
"default" => ""
];
if ($this->request instanceof Request) {
$widgets['currentrequest']['tooltip'] += [
'controller_action' => optional($this->request->route())->getActionName(),
];
}
}

return $widgets;
}

/**
Expand Down Expand Up @@ -99,15 +121,37 @@ public function collect()
}

$statusCode = $response->getStatusCode();
$startTime = defined('LARAVEL_START') ? LARAVEL_START : $request->server->get('REQUEST_TIME_FLOAT');
$query = $request->getQueryString();
$htmlData = [];

$data = [
'path_info' => $request->getPathInfo(),
'status_code' => $statusCode,
'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '',
'format' => $request->getRequestFormat(),
'content_type' => $response->headers->get('Content-Type') ? $response->headers->get(
'status' => $statusCode . ' ' . (isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : ''),
'duration' => $startTime ? $this->formatDuration(microtime(true) - $startTime) : null,
'peak_memory' => $this->formatBytes(memory_get_peak_usage(true), 1),
];

if ($request instanceof Request) {

if ($route = $request->route()) {
$htmlData += $this->getRouteInformation($route);
}

$fulLUrl = $request->fullUrl();
$data += [
'full_url' => strlen($fulLUrl) > 100 ? [$fulLUrl] : $fulLUrl,
];
}

if ($response instanceof RedirectResponse) {
$data['response'] = 'Redirect to ' . $response->getTargetUrl();
}

$data += [
'response' => $response->headers->get('Content-Type') ? $response->headers->get(
'Content-Type'
) : 'text/html',
'request_format' => $request->getRequestFormat(),
'request_query' => $request->query->all(),
'request_request' => $request->request->all(),
'request_headers' => $request->headers->all(),
Expand Down Expand Up @@ -137,7 +181,6 @@ public function collect()
}
}

$htmlData = [];
if (class_exists(Telescope::class)) {
$entry = IncomingEntry::make([
'requestId' => $this->currentRequestId,
Expand All @@ -150,6 +193,81 @@ public function collect()
return $htmlData + $data;
}

protected function getRouteInformation($route)
{
if (!is_a($route, 'Illuminate\Routing\Route')) {
return [];
}
$uri = head($route->methods()) . ' ' . $route->uri();
$action = $route->getAction();

$result = [
'uri' => $uri ?: '-',
];

$result = array_merge($result, $action);
$uses = $action['uses'] ?? null;
$controller = is_string($action['controller'] ?? null) ? $action['controller'] : '';

if (request()->hasHeader('X-Livewire')) {
try {
$component = request('components')[0];
$name = json_decode($component['snapshot'], true)['memo']['name'];
$method = $component['calls'][0]['method'];
$class = app(\Livewire\Mechanisms\ComponentRegistry::class)->getClass($name);
if (class_exists($class) && method_exists($class, $method)) {
$controller = $class . '@' . $method;
$result['controller'] = ltrim($controller, '\\');
}
} catch (\Throwable $e) {
//
}
}

if (str_contains($controller, '@')) {
list($controller, $method) = explode('@', $controller);
if (class_exists($controller) && method_exists($controller, $method)) {
$reflector = new \ReflectionMethod($controller, $method);
}
unset($result['uses']);
} elseif ($uses instanceof \Closure) {
$reflector = new \ReflectionFunction($uses);
$result['uses'] = $this->formatVar($uses);
} elseif (is_string($uses) && str_contains($uses, '@__invoke')) {
if (class_exists($controller) && method_exists($controller, 'render')) {
$reflector = new \ReflectionMethod($controller, 'render');
$result['controller'] = $controller . '@render';
}
}

if (isset($reflector)) {
$filename = $this->normalizeFilePath($reflector->getFileName());

if ($link = $this->getXdebugLink($reflector->getFileName(), $reflector->getStartLine())) {
$result['file'] = sprintf(
'<a href="%s" onclick="%s" class="phpdebugbar-widgets-editor-link">%s:%s-%s</a>',
$link['url'],
$link['ajax'] ? 'event.preventDefault();$.ajax(this.href);' : '',
$filename,
$reflector->getStartLine(),
$reflector->getEndLine()
);

if (isset($result['controller'])) {
$result['controller'] .= '<a href="'.$link['url'].'" class="phpdebugbar-widgets-editor-link"></a>';
}
} else {
$result['file'] = sprintf('%s:%s-%s', $filename, $reflector->getStartLine(), $reflector->getEndLine());
}
}

if (isset($result['middleware']) && is_array($result['middleware'])) {
$result['middleware'] = implode(', ', $result['middleware']);
}

return array_filter($result);
}

private function getCookieHeader($name, $value, $expires, $path, $domain, $secure, $httponly)
{
$cookie = sprintf('%s=%s', $name, urlencode($value ?? ''));
Expand Down
18 changes: 6 additions & 12 deletions src/DataCollector/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ protected function getRouteInformation($route)

if ($link = $this->getXdebugLink($reflector->getFileName(), $reflector->getStartLine())) {
$result['file'] = sprintf(
'<a href="%s" onclick="%s">%s:%s-%s</a>',
'<a href="%s" onclick="%s" class="phpdebugbar-widgets-editor-link">%s:%s-%s</a>',
$link['url'],
$link['ajax'] ? 'event.preventDefault();$.ajax(this.href);' : '',
$filename,
$reflector->getStartLine(),
$reflector->getEndLine()
);

if (isset($result['controller'])) {
$result['controller'] .= '<a href="'.$link['url'].'" class="phpdebugbar-widgets-editor-link"></a>';
}
} else {
$result['file'] = sprintf('%s:%s-%s', $filename, $reflector->getStartLine(), $reflector->getEndLine());
}
Expand All @@ -110,9 +114,7 @@ protected function getRouteInformation($route)
$result['middleware'] = $middleware;
}



return $result;
return array_filter($result);
}

/**
Expand Down Expand Up @@ -149,14 +151,6 @@ public function getWidgets()
"default" => "{}"
]
];
if (Config::get('debugbar.options.route.label', true)) {
$widgets['currentroute'] = [
"icon" => "share",
"tooltip" => "Route",
"map" => "route.uri",
"default" => ""
];
}
return $widgets;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/DataCollector/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ protected function setUp(): void
$this->routeCollector = debugbar()->getCollector('route');
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('debugbar.collectors.route', true);

parent::getEnvironmentSetUp($app);
}

public function testItCollectsRouteUri()
{
$this->get('web/html');
Expand Down

0 comments on commit ddb2979

Please sign in to comment.