Skip to content
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

Support for installing on fresh L11 installations #557

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
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
Loading