Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshSalway committed Dec 20, 2024
1 parent 8c5ecd4 commit bbbc0d6
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Illuminate/Foundation/Console/ApiInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ protected function installPassport()
}

/**
* Attempt to add the given trait to the specified model.
*
* @return void
*/
* Attempt to add the given trait to the specified model.
*
* @return void
*/
protected function addTraitToModel(string $trait, string $model)
{
$modelPath = $this->laravel->basePath(str_replace('\\', '/', $model) . '.php');
Expand All @@ -190,7 +190,7 @@ protected function addTraitToModel(string $trait, string $model)
$sanctumTrait = 'Laravel\\Sanctum\\HasApiTokens';
$passportTrait = 'Laravel\\Passport\\HasApiTokens';

// Detect conflicts
// Detect existing traits and warn
if (str_contains($content, "use $sanctumTrait;")) {
$this->warn("Sanctum is already installed in your [$model] model. Please manually switch to Passport if needed.");
return;
Expand All @@ -201,8 +201,18 @@ protected function addTraitToModel(string $trait, string $model)
return;
}

// Add the top-level `use` statement if missing
// Confirm with the user before making changes
if (! $this->components->confirm(
"Would you like to add the [$trait] trait to your [$model] model now?",
true
)) {
$this->components->info("No changes were made to your [$model] model.");
return;
}

$modified = false;

// Add the top-level `use` statement if missing
$isTopLevelImported = str_contains($content, "use $trait;");

if (! $isTopLevelImported) {
Expand Down

0 comments on commit bbbc0d6

Please sign in to comment.