Skip to content

Commit

Permalink
Deals with conflict.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshSalway committed Dec 20, 2024
1 parent 66ef6c1 commit 8c5ecd4
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Illuminate/Foundation/Console/ApiInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,24 @@ protected function addTraitToModel(string $trait, string $model)

$content = file_get_contents($modelPath);
$traitBasename = class_basename($trait);
$topLevelCheck = "use $trait;";
$classLevelCheck = $traitBasename;
$sanctumTrait = 'Laravel\\Sanctum\\HasApiTokens';
$passportTrait = 'Laravel\\Passport\\HasApiTokens';

// 1. Check if the trait is already imported or used
$isTopLevelImported = strpos($content, $topLevelCheck) !== false;
$isClassLevelUsed = preg_match('/use\s+([A-Za-z,\\\\\s]+);/', $content, $matches) &&
strpos($matches[1], $classLevelCheck) !== false;
// Detect conflicts
if (str_contains($content, "use $sanctumTrait;")) {
$this->warn("Sanctum is already installed in your [$model] model. Please manually switch to Passport if needed.");
return;
}

if ($isTopLevelImported && $isClassLevelUsed) {
$this->components->info("The [{$trait}] trait is already present in your [{$model}] model.");
if (str_contains($content, "use $passportTrait;")) {
$this->warn("Passport is already installed in your [$model] model. Please manually switch to Sanctum if needed.");
return;
}

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

// 2. Add the top-level `use` statement if missing
if (! $isTopLevelImported) {
$content = preg_replace(
'/^(namespace\s+[\w\\\\]+;\s*(?:\/\/.*\n)*)((?:use\s+[\w\\\\]+;\n)*)/m',
Expand All @@ -216,15 +218,18 @@ protected function addTraitToModel(string $trait, string $model)
}
}

// 3. Add the class-level trait if missing
// Add the class-level trait if missing
$isClassLevelUsed = preg_match('/use\s+([A-Za-z,\\\\\s]+);/', $content, $matches) &&
str_contains($matches[1], $traitBasename);

if (! $isClassLevelUsed) {
if (preg_match('/class\s+\w+\s+extends\s+\w+[A-Za-z\\\\]*\s*\{/', $content, $matches, PREG_OFFSET_CAPTURE)) {
$insertPosition = $matches[0][1] + strlen($matches[0][0]);

if (preg_match('/use\s+(.*?);/s', $content, $useMatches, PREG_OFFSET_CAPTURE, $insertPosition)) {
$traits = array_map('trim', explode(',', $useMatches[1][0]));

if (! in_array($traitBasename, $traits)) {
if (!in_array($traitBasename, $traits, true)) {
$traits[] = $traitBasename;
$content = substr_replace(
$content,
Expand All @@ -246,12 +251,12 @@ protected function addTraitToModel(string $trait, string $model)
}
}

// 4. Write the changes back to the file
// Save changes if modified
if ($modified) {
file_put_contents($modelPath, $content);
$this->components->info("The [{$trait}] trait has been added to your [{$model}] model.");
$this->components->info("The [$trait] trait has been added to your [$model] model.");
} else {
$this->components->info("No changes were made to your [{$model}] model.");
$this->components->info("No changes were made to your [$model] model.");
}
}
}

0 comments on commit 8c5ecd4

Please sign in to comment.