Skip to content

Commit

Permalink
Support for installing on fresh L11 installations (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet authored Mar 18, 2024
1 parent 3f0b168 commit dffa10b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run-stub-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand Down
38 changes: 34 additions & 4 deletions src/Commands/InstallsSpladeExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));';
Expand Down
37 changes: 34 additions & 3 deletions src/Commands/InstallsSpladeRouteMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 9 additions & 16 deletions src/Commands/SpladeInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
});

Expand Down

0 comments on commit dffa10b

Please sign in to comment.