Skip to content

Commit

Permalink
[11.x] Add support for installing Laravel passport on install:api a…
Browse files Browse the repository at this point in the history
…rtisan command (#50386)

* add passport installation to `install:api` command

* formatting

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Mar 6, 2024
1 parent 0edc507 commit ca03fcb
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/Illuminate/Foundation/Console/ApiInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')) {
Expand All @@ -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.');
}
}

/**
Expand Down Expand Up @@ -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',
]);
}
}

0 comments on commit ca03fcb

Please sign in to comment.