Skip to content

Commit dfde15c

Browse files
authored
Merge pull request #9 from laravel/cleanup_code_1
refactor: cleanup BoostServiceProvider
2 parents d1fc067 + 2dacc9f commit dfde15c

26 files changed

+95
-80
lines changed

config/boost.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
declare(strict_types=1);
44

55
return [
6-
'project_purpose' => null,
7-
'browser_logs' => true,
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Boost Browser Logs Watcher Switch
10+
|--------------------------------------------------------------------------
11+
|
12+
| The following option may be used to enable or disable browser logs watcher
13+
| functionality which simply provides a single and convenient way to
14+
| enable or disable browser logs functionality in Boost.
15+
*/
16+
17+
'browser_logs_watcher' => env('BOOST_BROWSER_LOGS_WATCHER', true),
818
];

pint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
"phpdoc_align": {
112112
"align": "left",
113113
"spacing": {
114-
"param": 2
114+
"param": 1
115115
}
116116
},
117117
"phpdoc_indent": true,

src/BoostServiceProvider.php

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
88
use Illuminate\Http\Request;
99
use Illuminate\Routing\Router;
10-
use Illuminate\Support\Facades\Blade;
1110
use Illuminate\Support\Facades\Log;
1211
use Illuminate\Support\Facades\Route;
1312
use Illuminate\Support\ServiceProvider;
13+
use Illuminate\View\Compilers\BladeCompiler;
1414
use Laravel\Boost\Mcp\Boost;
1515
use Laravel\Boost\Middleware\InjectBoost;
1616
use Laravel\Mcp\Server\Facades\Mcp;
@@ -21,18 +21,21 @@ class BoostServiceProvider extends ServiceProvider
2121
public function register(): void
2222
{
2323
$this->mergeConfigFrom(
24-
__DIR__.'/../config/boost.php', 'boost'
24+
__DIR__.'/../config/boost.php',
25+
'boost'
2526
);
2627

2728
$this->app->singleton(Roster::class, function () {
28-
$composerLockPath = base_path('composer.lock');
29-
$packageLockPath = base_path('package-lock.json');
29+
$lockFiles = [
30+
base_path('composer.lock'),
31+
base_path('package-lock.json'),
32+
base_path('bun.lockb'),
33+
base_path('pnpm-lock.yaml'),
34+
base_path('yarn.lock'),
35+
];
3036

3137
$cacheKey = 'boost.roster.scan';
32-
$lastModified = max(
33-
file_exists($composerLockPath) ? filemtime($composerLockPath) : 0,
34-
file_exists($packageLockPath) ? filemtime($packageLockPath) : 0
35-
);
38+
$lastModified = max(array_map(fn ($path) => file_exists($path) ? filemtime($path) : 0, $lockFiles));
3639

3740
$cached = cache()->get($cacheKey);
3841
if ($cached && isset($cached['timestamp']) && $cached['timestamp'] >= $lastModified) {
@@ -62,7 +65,7 @@ public function boot(Router $router): void
6265
$this->registerCommands();
6366
$this->registerRoutes();
6467
$this->registerBrowserLogger();
65-
$this->registerBladeDirectives();
68+
$this->callAfterResolving('blade.compiler', fn (BladeCompiler $bladeCompiler) => $this->registerBladeDirectives($bladeCompiler));
6669
$this->hookIntoResponses($router);
6770
}
6871

@@ -93,10 +96,17 @@ private function registerRoutes(): void
9396
/** @var \Illuminate\Log\Logger $logger */
9497
$logger = Log::channel('browser');
9598

96-
/** @var array{type: 'error'|'warn'|'info'|'log'|'table'|'window_error'|'uncaught_error'|'unhandled_rejection', timestamp: string, data: array<mixed>, url:string, userAgent:string} $log */
99+
/**
100+
* @var array{
101+
* type: 'error'|'warn'|'info'|'log'|'table'|'window_error'|'uncaught_error'|'unhandled_rejection',
102+
* timestamp: string,
103+
* data: array<mixed>,
104+
* url:string,
105+
* userAgent:string
106+
* } $log */
97107
foreach ($logs as $log) {
98108
$logger->write(
99-
level: $this->jsTypeToPsr3($log['type']),
109+
level: $this->mapJsTypeToPsr3Level($log['type']),
100110
message: $this->buildLogMessageFromData($log['data']),
101111
context: [
102112
'url' => $log['url'],
@@ -107,58 +117,53 @@ private function registerRoutes(): void
107117
}
108118

109119
return response()->json(['status' => 'logged']);
110-
})->name('boost.browser-logs')->withoutMiddleware(VerifyCsrfToken::class);
120+
})
121+
->name('boost.browser-logs')
122+
->withoutMiddleware(VerifyCsrfToken::class);
111123
}
112124

113125
/**
114126
* Build a string message for the log based on various input types. Single dimensional, and multi:
115-
* "data":[{"message":"Unhandled Promise Rejection","reason":{"name":"TypeError","message":"NetworkError when attempting to fetch resource.","stack":""}}]
127+
* "data":[
128+
* {"message":"Unhandled Promise Rejection","reason":{"name":"TypeError","message":"NetworkError when attempting to fetch resource.","stack":""}}]
116129
*
117-
* @param array<mixed> $data
130+
* @param array<mixed> $data
118131
*/
119132
protected function buildLogMessageFromData(array $data): string
120133
{
121134
$messages = [];
122135

123-
foreach ($data as $key => $value) {
124-
if (is_array($value)) {
125-
$nestedMessage = $this->buildLogMessageFromData($value);
126-
if ($nestedMessage !== '') {
127-
$messages[] = $nestedMessage;
128-
}
129-
} elseif (is_string($value) || is_numeric($value)) {
130-
$messages[] = (string) $value;
131-
} elseif (is_bool($value)) {
132-
$messages[] = $value ? 'true' : 'false';
133-
} elseif (is_null($value)) {
134-
$messages[] = 'null';
135-
} elseif (is_object($value)) {
136-
$messages[] = json_encode($value);
137-
}
136+
foreach ($data as $value) {
137+
$messages[] = match (true) {
138+
is_array($value) => $this->buildLogMessageFromData($value),
139+
is_string($value), is_numeric($value) => (string) $value,
140+
is_bool($value) => $value ? 'true' : 'false',
141+
is_null($value) => 'null',
142+
is_object($value) => json_encode($value),
143+
};
138144
}
139145

140146
return implode(' ', $messages);
141147
}
142148

143149
protected function registerBrowserLogger(): void
144150
{
145-
// Register a custom log channel for browser logs
146-
config(['logging.channels.browser' => [
147-
'driver' => 'single',
148-
'path' => storage_path('logs/browser.log'),
149-
'level' => env('LOG_LEVEL', 'debug'),
150-
'days' => 14,
151-
]]);
151+
config([
152+
'logging.channels.browser' => [
153+
'driver' => 'single',
154+
'path' => storage_path('logs/browser.log'),
155+
'level' => env('LOG_LEVEL', 'debug'),
156+
'days' => 14,
157+
],
158+
]);
152159
}
153160

154-
protected function registerBladeDirectives(): void
161+
protected function registerBladeDirectives(BladeCompiler $bladeCompiler): void
155162
{
156-
Blade::directive('boostJs', function () {
157-
return '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>';
158-
});
163+
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
159164
}
160165

161-
private function jsTypeToPsr3(string $type): string
166+
private function mapJsTypeToPsr3Level(string $type): string
162167
{
163168
return match ($type) {
164169
'warn' => 'warning',
@@ -173,7 +178,7 @@ private function jsTypeToPsr3(string $type): string
173178

174179
private function hookIntoResponses(Router $router): void
175180
{
176-
if (config('boost.browser_logs', true) === false) {
181+
if (! config('boost.browser_logs_watcher', true)) {
177182
return;
178183
}
179184

src/Concerns/MakesHttpRequests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function get(string $url): Response
3030
}
3131

3232
/**
33-
* @param array<string, mixed> $json
33+
* @param array<string, mixed> $json
3434
*/
3535
public function json(string $url, array $json): Response
3636
{

src/Contracts/Ide.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ interface Ide
1010
// Things to note: supports relative (absolute path required)? global mcp only? Prefer local file, but if global only we have to add the project name to the server name
1111

1212
/**
13-
* @param array<int, string> $args
14-
* @param array<string, string> $env
13+
* @param array<int, string> $args
14+
* @param array<string, string> $env
1515
*/
1616
public function installMcp(string $key, string $command, array $args = [], array $env = []): bool;
1717
}

src/Install/Agents/FileMcpIde.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function mcpPath(): string
1616
}
1717

1818
/**
19-
* @param array<int, string> $args
20-
* @param array<string, string> $env
19+
* @param array<int, string> $args
20+
* @param array<string, string> $env
2121
*/
2222
public function installMcp(string $key, string $command, array $args = [], array $env = []): bool
2323
{

src/Install/Agents/ShellMcpIde.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ abstract class ShellMcpIde implements Ide
1212
protected string $shellCommand = 'echo "{command} {args} {env}"';
1313

1414
/**
15-
* @param array<int, string> $args
16-
* @param array<string, string> $env
15+
* @param array<int, string> $args
16+
* @param array<string, string> $env
1717
*/
1818
public function installMcp(string $key, string $command, array $args = [], array $env = []): bool
1919
{

src/Install/ApplicationDetector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function detectInProject(string $basePath): array
189189
/**
190190
* Check if an application is installed based on its configuration.
191191
*
192-
* @param array<string, string|array<string>> $config
192+
* @param array<string, string|array<string>> $config
193193
*/
194194
protected function isAppInstalled(array $config, string $platform): bool
195195
{
@@ -219,7 +219,7 @@ protected function isAppInstalled(array $config, string $platform): bool
219219
/**
220220
* Check if an application is used in the current project.
221221
*
222-
* @param array<string, string|array<string>> $config
222+
* @param array<string, string|array<string>> $config
223223
*/
224224
protected function isAppUsedInProject(array $config, string $basePath): bool
225225
{
@@ -297,7 +297,7 @@ protected function getPlatform(): string
297297
/**
298298
* Add custom detection configuration for an application.
299299
*
300-
* @param array<string, array<string, string|array<string>>> $config
300+
* @param array<string, array<string, string|array<string>>> $config
301301
*/
302302
public function addDetectionConfig(string $app, array $config, ?string $platform = null): void
303303
{
@@ -314,7 +314,7 @@ public function addDetectionConfig(string $app, array $config, ?string $platform
314314
/**
315315
* Add custom project detection configuration for an application.
316316
*
317-
* @param array<string, string|array<string>> $config
317+
* @param array<string, string|array<string>> $config
318318
*/
319319
public function addProjectDetectionConfig(string $app, array $config): void
320320
{

src/Install/Cli/DisplayHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class DisplayHelper
88
{
99
/**
10-
* @param array<int, array<int|string, mixed>> $data
10+
* @param array<int, array<int|string, mixed>> $data
1111
*/
1212
public static function datatable(array $data, int $cols = 80): void
1313
{
@@ -96,7 +96,7 @@ public static function datatable(array $data, int $cols = 80): void
9696
}
9797

9898
/**
99-
* @param array<int, string> $items
99+
* @param array<int, string> $items
100100
*/
101101
public static function grid(array $items, int $cols = 80): void
102102
{

src/Mcp/ToolExecutor.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct()
1919
/**
2020
* Execute a tool with the given arguments.
2121
*
22-
* @param array<string, mixed> $arguments
22+
* @param array<string, mixed> $arguments
2323
*/
2424
public function execute(string $toolClass, array $arguments = []): ToolResult
2525
{
@@ -37,7 +37,7 @@ public function execute(string $toolClass, array $arguments = []): ToolResult
3737
/**
3838
* Execute tool in a separate process for isolation.
3939
*
40-
* @param array<string, mixed> $arguments
40+
* @param array<string, mixed> $arguments
4141
*/
4242
protected function executeInProcess(string $toolClass, array $arguments): ToolResult
4343
{
@@ -80,7 +80,7 @@ protected function executeInProcess(string $toolClass, array $arguments): ToolRe
8080
/**
8181
* Execute tool inline (current process).
8282
*
83-
* @param array<string, mixed> $arguments
83+
* @param array<string, mixed> $arguments
8484
*/
8585
protected function executeInline(string $toolClass, array $arguments): ToolResult
8686
{
@@ -117,7 +117,7 @@ protected function getTimeout(): int
117117
/**
118118
* Reconstruct a ToolResult from JSON data.
119119
*
120-
* @param array<string, mixed> $data
120+
* @param array<string, mixed> $data
121121
*/
122122
protected function reconstructToolResult(array $data): ToolResult
123123
{

0 commit comments

Comments
 (0)