-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Enhance install:api command to optionally add HasApiTokens trait to User model #53994
Open
JoshSalway
wants to merge
17
commits into
laravel:11.x
Choose a base branch
from
JoshSalway:11.x
base: 11.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−8
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…odel during API installation - When installing Sanctum or Passport via `install:api`, prompt the user to add the HasApiTokens trait automatically. - Check for `App\Models\User` before modifying the file, gracefully warning the user if it doesn't exist. - Only modify the User model if necessary, ensuring non-destructive and minimal changes. - Improve developer experience by reducing manual steps after API scaffolding installation.
- Implemented `addTraitToModel` function to add a specified trait to a model. - Handles importing the trait and ensuring its usage within the model class. - Prevents duplication if the trait already exists. - Includes file path validation and precise modifications to maintain code integrity.
This reverts commit 3704b36.
…model - Refactored `addTraitToModel` function to properly check for top-level `use` imports and class-level trait usage. - Prevented redundant additions of the trait in both top-level and class-level blocks. - Improved logic to handle cases where the trait is missing in either block and correctly adds it. - Ensured compatibility with Laravel conventions and better error handling for edge cases.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before:
After:
Expected changes to the file:
This PR improves the developer experience when running
php artisan install:api
. After installing Sanctum or Passport, the command now offers to automatically add theHasApiTokens
trait to theUser
model. IfApp\Models\User
does not exist, it provides a gentle warning rather than making incorrect assumptions.Key Changes:
App\Models\User
before editing the file, preventing errors if the model has been relocated.Why This Change?
This enhancement streamlines the initial setup process by removing an additional manual step often missed by newcomers. It ensures developers can get their API scaffolding set up and ready to use more quickly, without digging into model files themselves—unless they need to.
Testing Notes:
php artisan install:api
without --passport to install Sanctum and confirm the migration and trait insertion prompts.php artisan install:api --passport
to test Passport installation and the subsequent trait insertion prompt.User
model is only modified ifApp\Models\User
existsFeature-by-Feature Analysis
strpos
to detect if the top-leveluse
statement (e.g.,use Laravel\Passport\HasApiTokens;
) exists.preg_match
on the class-leveluse
block to check if the trait is already included.use
statementuse
statement is missing, it adds it usingpreg_replace
.use
statementuse
block, it inserts it in the correct location.use
blockuse
block inside the class (e.g.,use HasFactory, Notifiable;
).Sanctum
orPassport
traits are detected, asking them to manually resolve conflicts.array_map
to handle multiple traits and appends the new one if missing.Potential issues:
php artisan install:api
and later runsphp artisan install:api --passport
?Proposed Solution: Notify the user if
Sanctum
orPassport
traits are already present, advising them to manually address any potential conflicts.