Skip to content

Refactor BoostServiceProvider for readability and efficiency #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/BoostServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Http\Request;
use Illuminate\Log\Logger;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -69,7 +70,7 @@
$this->hookIntoResponses($router);
}

protected function registerPublishing(): void
private function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
Expand All @@ -78,7 +79,7 @@
}
}

protected function registerCommands(): void
private function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Expand All @@ -93,14 +94,14 @@
{
Route::post('/_boost/browser-logs', function (Request $request) {
$logs = $request->input('logs', []);
/** @var \Illuminate\Log\Logger $logger */
/** @var Logger $logger */
$logger = Log::channel('browser');

/**
* @var array{
* type: 'error'|'warn'|'info'|'log'|'table'|'window_error'|'uncaught_error'|'unhandled_rejection',
* timestamp: string,
* data: array<mixed>,
* data: array,
* url:string,
* userAgent:string
* } $log */
Expand All @@ -123,18 +124,18 @@
}

/**
* Build a string message for the log based on various input types. Single dimensional, and multi:
* "data":[
* {"message":"Unhandled Promise Rejection","reason":{"name":"TypeError","message":"NetworkError when attempting to fetch resource.","stack":""}}]
* Build a string message for the log based on various input types. Single-dimensional, and multi:
* "data": {"message":"Unhandled Promise Rejection","reason":{"name":"TypeError","message":"NetworkError when attempting to fetch resource.","stack":""}}]
*
* @param array<mixed> $data
* @param array $data
* @return string
*/
protected function buildLogMessageFromData(array $data): string
private function buildLogMessageFromData(array $data): string
{
$messages = [];

foreach ($data as $value) {
$messages[] = match (true) {

Check failure on line 138 in src/BoostServiceProvider.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Match expression does not handle remaining value: true
is_array($value) => $this->buildLogMessageFromData($value),
is_string($value), is_numeric($value) => (string) $value,
is_bool($value) => $value ? 'true' : 'false',
Expand All @@ -146,7 +147,7 @@
return implode(' ', $messages);
}

protected function registerBrowserLogger(): void
private function registerBrowserLogger(): void
{
config([
'logging.channels.browser' => [
Expand All @@ -158,7 +159,7 @@
]);
}

protected function registerBladeDirectives(BladeCompiler $bladeCompiler): void
private function registerBladeDirectives(BladeCompiler $bladeCompiler): void
{
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
}
Expand All @@ -167,11 +168,8 @@
{
return match ($type) {
'warn' => 'warning',
'log' => 'debug',
'table' => 'debug',
'window_error' => 'error',
'uncaught_error' => 'error',
'unhandled_rejection' => 'error',
'log', 'table' => 'debug',
'window_error', 'uncaught_error', 'unhandled_rejection' => 'error',
default => $type
};
}
Expand Down
Loading