Skip to content

Commit dffa10b

Browse files
authored
Support for installing on fresh L11 installations (#557)
1 parent 3f0b168 commit dffa10b

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

.github/workflows/run-stub-tests.yml

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ jobs:
1212
os: [ubuntu-22.04, windows-latest]
1313
php: [8.3, 8.2]
1414
laravel: [11.0, 10.2]
15-
exclude:
16-
- laravel: 11.0
1715

1816
name: Test Stubs ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }}
1917

src/Commands/InstallsSpladeExceptionHandler.php

+34-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,44 @@ trait InstallsSpladeExceptionHandler
1313
*/
1414
protected function installExceptionHandler()
1515
{
16+
version_compare(app()->version(), '11.0', '<')
17+
? $this->installExceptionHandlerForLegacyLaravelSkeleton()
18+
: $this->installExceptionHandlerForModernLaravelSkeleton();
19+
}
20+
21+
protected function installExceptionHandlerForModernLaravelSkeleton()
22+
{
23+
$appBoostrap = file_get_contents(base_path('bootstrap/app.php'));
24+
1625
$eol = SpladeInstallCommand::eol();
1726

18-
$exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php'));
27+
$search = '->withExceptions(function (Exceptions $exceptions) {';
28+
29+
$exceptionHandler = '$exceptions->renderable(\ProtoneMedia\Splade\SpladeCore::exceptionHandler($exceptions->handler));';
30+
31+
$exceptionsAfter = Str::after($appBoostrap, $search);
32+
33+
if (Str::contains($appBoostrap, $exceptionHandler)) {
34+
return;
35+
}
1936

20-
$search = version_compare(app()->version(), '10.0', '>=')
21-
? 'public function register(): void' . $eol . ' {'
22-
: 'public function register()' . $eol . ' {';
37+
file_put_contents(
38+
base_path('bootstrap/app.php'),
39+
str_replace(
40+
$exceptionsAfter,
41+
$eol . ' ' . $exceptionHandler . $exceptionsAfter,
42+
$appBoostrap
43+
)
44+
);
45+
}
46+
47+
protected function installExceptionHandlerForLegacyLaravelSkeleton()
48+
{
49+
$eol = SpladeInstallCommand::eol();
50+
51+
$exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php'));
2352

53+
$search = 'public function register(): void' . $eol . ' {';
2454
$registerMethodAfter = Str::after($exceptionHandler, $search);
2555

2656
$renderable = '$this->renderable(\ProtoneMedia\Splade\SpladeCore::exceptionHandler($this));';

src/Commands/InstallsSpladeRouteMiddleware.php

+34-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,45 @@ trait InstallsSpladeRouteMiddleware
1212
* @return void
1313
*/
1414
protected function installRouteMiddleware()
15+
{
16+
version_compare(app()->version(), '11.0', '<')
17+
? $this->installRouteMiddlewareForLegacyLaravelSkeleton()
18+
: $this->installRouteMiddlewareForModernLaravelSkeleton();
19+
}
20+
21+
protected function installRouteMiddlewareForModernLaravelSkeleton()
22+
{
23+
$appBoostrap = file_get_contents(base_path('bootstrap/app.php'));
24+
25+
$eol = SpladeInstallCommand::eol();
26+
27+
$search = '->withMiddleware(function (Middleware $middleware) {';
28+
29+
$routeMiddleware = '$middleware->group(\'splade\', [\ProtoneMedia\Splade\Http\SpladeMiddleware::class]);';
30+
31+
$middlewareAfter = Str::after($appBoostrap, $search);
32+
33+
if (Str::contains($appBoostrap, $routeMiddleware)) {
34+
return;
35+
}
36+
37+
file_put_contents(
38+
base_path('bootstrap/app.php'),
39+
str_replace(
40+
$middlewareAfter,
41+
$eol . ' ' . $routeMiddleware . $middlewareAfter,
42+
$appBoostrap
43+
)
44+
);
45+
}
46+
47+
protected function installRouteMiddlewareForLegacyLaravelSkeleton()
1548
{
1649
$httpKernel = file_get_contents(app_path('Http/Kernel.php'));
1750

1851
$eol = SpladeInstallCommand::eol();
1952

20-
$search = version_compare(app()->version(), '10.0', '>=')
21-
? 'protected $middlewareAliases = [' . $eol
22-
: 'protected $routeMiddleware = [' . $eol;
53+
$search = 'protected $middlewareAliases = [' . $eol;
2354

2455
$routeMiddlewareAfter = Str::after($httpKernel, $search);
2556

src/Commands/SpladeInstallCommand.php

+9-16
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ class SpladeInstallCommand extends Command
2323
*/
2424
public function handle(): int
2525
{
26-
// Check Laravel version...
27-
if (version_compare(app()->version(), '11.0', '>=')) {
28-
$this->error('Installing Splade is only supported on Laravel 10.x. Support for Laravel 11.x will be added in a future release.');
29-
30-
return self::FAILURE;
31-
}
32-
3326
$this->installRouteMiddleware();
3427

3528
$this->installExceptionHandler();
@@ -38,15 +31,15 @@ public function handle(): int
3831
$this->updateNodePackages(function ($packages) {
3932
return [
4033
'@protonemedia/laravel-splade' => '^1.4.16',
41-
'@tailwindcss/forms' => '^0.5.2',
42-
'@tailwindcss/typography' => '^0.5.2',
43-
'@vitejs/plugin-vue' => '^4.0.0',
44-
'autoprefixer' => '^10.4.7',
45-
'laravel-vite-plugin' => '^0.7.5',
46-
'postcss' => '^8.4.14',
47-
'tailwindcss' => '^3.3.0',
48-
'vite' => '^4.0.0',
49-
'vue' => '^3.2.37',
34+
'@tailwindcss/forms' => '^0.5.7',
35+
'@tailwindcss/typography' => '^0.5.10',
36+
'@vitejs/plugin-vue' => '^5.0',
37+
'autoprefixer' => '^10.4.16',
38+
'postcss' => '^8.4.32',
39+
'laravel-vite-plugin' => '^1.0',
40+
'tailwindcss' => '^3.4',
41+
'vite' => '^5.0',
42+
'vue' => '^3.4',
5043
] + $packages;
5144
});
5245

0 commit comments

Comments
 (0)