From ca03fcb9841ed978ef7b64e69fbe70d3a9f237c3 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Wed, 6 Mar 2024 19:37:41 +0330 Subject: [PATCH] [11.x] Add support for installing Laravel passport on `install:api` artisan command (#50386) * add passport installation to `install:api` command * formatting * formatting --------- Co-authored-by: Taylor Otwell --- .../Foundation/Console/ApiInstallCommand.php | 47 ++++++++++++++++--- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Foundation/Console/ApiInstallCommand.php b/src/Illuminate/Foundation/Console/ApiInstallCommand.php index 9edf1318079e..967c6fa16f56 100644 --- a/src/Illuminate/Foundation/Console/ApiInstallCommand.php +++ b/src/Illuminate/Foundation/Console/ApiInstallCommand.php @@ -20,14 +20,15 @@ class ApiInstallCommand extends Command */ protected $signature = 'install:api {--composer=global : Absolute path to the Composer binary which should be used to install packages} - {--force : Overwrite any existing API routes file}'; + {--force : Overwrite any existing API routes file} + {--passport : Install Laravel Passport instead of Laravel Sanctum}'; /** * The console command description. * * @var string */ - protected $description = 'Create an API routes file and install Laravel Sanctum'; + protected $description = 'Create an API routes file and install Laravel Sanctum or Laravel Passport'; /** * Execute the console command. @@ -36,7 +37,11 @@ class ApiInstallCommand extends Command */ public function handle() { - $this->installSanctum(); + if ($this->option('passport')) { + $this->installPassport(); + } else { + $this->installSanctum(); + } if (file_exists($apiRoutesPath = $this->laravel->basePath('routes/api.php')) && ! $this->option('force')) { @@ -46,14 +51,30 @@ public function handle() copy(__DIR__.'/stubs/api-routes.stub', $apiRoutesPath); + if ($this->option('passport')) { + (new Filesystem)->replaceInFile( + 'auth:sanctum', + 'auth:api', + $apiRoutesPath, + ); + } + $this->uncommentApiRoutesFile(); } - if ($this->confirm('One new database migration has been published. Would you like to run all pending database migrations?', false)) { - $this->call('migrate'); - } + if ($this->option('passport')) { + $this->call('passport:install', [ + '--uuids' => $this->confirm('Would you like to use UUIDs for all client IDs?'), + ]); - $this->components->info('API scaffolding installed. Please add the [Laravel\Sanctum\HasApiTokens] trait to your User model.'); + $this->components->info('API scaffolding installed. Please add the [Laravel\Passport\HasApiTokens] trait to your User model.'); + } else { + if ($this->confirm('One new database migration has been published. Would you like to run all pending database migrations?', false)) { + $this->call('migrate'); + } + + $this->components->info('API scaffolding installed. Please add the [Laravel\Sanctum\HasApiTokens] trait to your User model.'); + } } /** @@ -111,4 +132,16 @@ protected function installSanctum() ]); } } + + /** + * Install Laravel Passport into the application. + * + * @return void + */ + protected function installPassport() + { + $this->requireComposerPackages($this->option('composer'), [ + 'laravel/passport:^12.0', + ]); + } }