From 05261b90cd7634e15d1b9c94106effa06c2803eb Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Mon, 18 Mar 2024 14:38:00 +0100 Subject: [PATCH] Support for installing on fresh L11 installations --- .github/workflows/run-stub-tests.yml | 2 - .../InstallsSpladeExceptionHandler.php | 38 +++++++++++++++++-- .../InstallsSpladeRouteMiddleware.php | 37 ++++++++++++++++-- src/Commands/SpladeInstallCommand.php | 25 +++++------- 4 files changed, 77 insertions(+), 25 deletions(-) diff --git a/.github/workflows/run-stub-tests.yml b/.github/workflows/run-stub-tests.yml index 306b5cec..944740f0 100644 --- a/.github/workflows/run-stub-tests.yml +++ b/.github/workflows/run-stub-tests.yml @@ -12,8 +12,6 @@ jobs: os: [ubuntu-22.04, windows-latest] php: [8.3, 8.2] laravel: [11.0, 10.2] - exclude: - - laravel: 11.0 name: Test Stubs ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }} diff --git a/src/Commands/InstallsSpladeExceptionHandler.php b/src/Commands/InstallsSpladeExceptionHandler.php index ce5416b0..08103a19 100644 --- a/src/Commands/InstallsSpladeExceptionHandler.php +++ b/src/Commands/InstallsSpladeExceptionHandler.php @@ -13,14 +13,44 @@ trait InstallsSpladeExceptionHandler */ protected function installExceptionHandler() { + version_compare(app()->version(), '11.0', '<') + ? $this->installExceptionHandlerForLegacyLaravelSkeleton() + : $this->installExceptionHandlerForModernLaravelSkeleton(); + } + + protected function installExceptionHandlerForModernLaravelSkeleton() + { + $appBoostrap = file_get_contents(base_path('bootstrap/app.php')); + $eol = SpladeInstallCommand::eol(); - $exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php')); + $search = '->withExceptions(function (Exceptions $exceptions) {'; + + $exceptionHandler = '$exceptions->renderable(\ProtoneMedia\Splade\SpladeCore::exceptionHandler($exceptions->handler));'; + + $exceptionsAfter = Str::after($appBoostrap, $search); + + if (Str::contains($appBoostrap, $exceptionHandler)) { + return; + } - $search = version_compare(app()->version(), '10.0', '>=') - ? 'public function register(): void' . $eol . ' {' - : 'public function register()' . $eol . ' {'; + file_put_contents( + base_path('bootstrap/app.php'), + str_replace( + $exceptionsAfter, + $eol . ' ' . $exceptionHandler . $exceptionsAfter, + $appBoostrap + ) + ); + } + + protected function installExceptionHandlerForLegacyLaravelSkeleton() + { + $eol = SpladeInstallCommand::eol(); + + $exceptionHandler = file_get_contents(app_path('Exceptions/Handler.php')); + $search = 'public function register(): void' . $eol . ' {'; $registerMethodAfter = Str::after($exceptionHandler, $search); $renderable = '$this->renderable(\ProtoneMedia\Splade\SpladeCore::exceptionHandler($this));'; diff --git a/src/Commands/InstallsSpladeRouteMiddleware.php b/src/Commands/InstallsSpladeRouteMiddleware.php index a0136f55..9a4c3802 100644 --- a/src/Commands/InstallsSpladeRouteMiddleware.php +++ b/src/Commands/InstallsSpladeRouteMiddleware.php @@ -12,14 +12,45 @@ trait InstallsSpladeRouteMiddleware * @return void */ protected function installRouteMiddleware() + { + version_compare(app()->version(), '11.0', '<') + ? $this->installRouteMiddlewareForLegacyLaravelSkeleton() + : $this->installRouteMiddlewareForModernLaravelSkeleton(); + } + + protected function installRouteMiddlewareForModernLaravelSkeleton() + { + $appBoostrap = file_get_contents(base_path('bootstrap/app.php')); + + $eol = SpladeInstallCommand::eol(); + + $search = '->withMiddleware(function (Middleware $middleware) {'; + + $routeMiddleware = '$middleware->group(\'splade\', [\ProtoneMedia\Splade\Http\SpladeMiddleware::class]);'; + + $middlewareAfter = Str::after($appBoostrap, $search); + + if (Str::contains($appBoostrap, $routeMiddleware)) { + return; + } + + file_put_contents( + base_path('bootstrap/app.php'), + str_replace( + $middlewareAfter, + $eol . ' ' . $routeMiddleware . $middlewareAfter, + $appBoostrap + ) + ); + } + + protected function installRouteMiddlewareForLegacyLaravelSkeleton() { $httpKernel = file_get_contents(app_path('Http/Kernel.php')); $eol = SpladeInstallCommand::eol(); - $search = version_compare(app()->version(), '10.0', '>=') - ? 'protected $middlewareAliases = [' . $eol - : 'protected $routeMiddleware = [' . $eol; + $search = 'protected $middlewareAliases = [' . $eol; $routeMiddlewareAfter = Str::after($httpKernel, $search); diff --git a/src/Commands/SpladeInstallCommand.php b/src/Commands/SpladeInstallCommand.php index bc81cf70..e8d3c03a 100644 --- a/src/Commands/SpladeInstallCommand.php +++ b/src/Commands/SpladeInstallCommand.php @@ -23,13 +23,6 @@ class SpladeInstallCommand extends Command */ public function handle(): int { - // Check Laravel version... - if (version_compare(app()->version(), '11.0', '>=')) { - $this->error('Installing Splade is only supported on Laravel 10.x. Support for Laravel 11.x will be added in a future release.'); - - return self::FAILURE; - } - $this->installRouteMiddleware(); $this->installExceptionHandler(); @@ -38,15 +31,15 @@ public function handle(): int $this->updateNodePackages(function ($packages) { return [ '@protonemedia/laravel-splade' => '^1.4.16', - '@tailwindcss/forms' => '^0.5.2', - '@tailwindcss/typography' => '^0.5.2', - '@vitejs/plugin-vue' => '^4.0.0', - 'autoprefixer' => '^10.4.7', - 'laravel-vite-plugin' => '^0.7.5', - 'postcss' => '^8.4.14', - 'tailwindcss' => '^3.3.0', - 'vite' => '^4.0.0', - 'vue' => '^3.2.37', + '@tailwindcss/forms' => '^0.5.7', + '@tailwindcss/typography' => '^0.5.10', + '@vitejs/plugin-vue' => '^5.0', + 'autoprefixer' => '^10.4.16', + 'postcss' => '^8.4.32', + 'laravel-vite-plugin' => '^1.0', + 'tailwindcss' => '^3.4', + 'vite' => '^5.0', + 'vue' => '^3.4', ] + $packages; });