Skip to content
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
wants to merge 17 commits into
base: 11.x
Choose a base branch
from

Conversation

JoshSalway
Copy link

@JoshSalway JoshSalway commented Dec 20, 2024

Before:

Screenshot 2024-12-20 at 8 28 44 pm

After:

Screenshot 2024-12-21 at 12 20 06 am
Expected changes to the file:

image

This PR improves the developer experience when running php artisan install:api. After installing Sanctum or Passport, the command now offers to automatically add the HasApiTokens trait to the User model. If App\Models\User does not exist, it provides a gentle warning rather than making incorrect assumptions.

Key Changes:

  • Added a confirmation prompt after installing Sanctum or Passport to optionally insert the HasApiTokens trait into the User model.
  • Verified existence of App\Models\User before editing the file, preventing errors if the model has been relocated.
  • Ensured non-destructive file modifications by checking for existing trait usage and use statements.
  • Maintained backward compatibility and did not break any existing behavior. If the user declines or the model is missing, the command simply informs them and moves on.

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:

  • Run php artisan install:api without --passport to install Sanctum and confirm the migration and trait insertion prompts.
  • Run php artisan install:api --passport to test Passport installation and the subsequent trait insertion prompt.
  • Verify that the User model is only modified if App\Models\User exists

Feature-by-Feature Analysis

Feature Does the Code Support It? Explanation
Detects existing top-level trait The code uses strpos to detect if the top-level use statement (e.g., use Laravel\Passport\HasApiTokens;) exists.
Detects existing class-level trait The code uses preg_match on the class-level use block to check if the trait is already included.
Adds top-level use statement If the use statement is missing, it adds it using preg_replace.
Adds class-level use statement If the trait is missing in the class-level use block, it inserts it in the correct location.
Modifies existing use block Appends the trait to an existing use block inside the class (e.g., use HasFactory, Notifiable;).
Prevents duplicate additions The code checks both top-level and class-level locations to ensure the trait isn’t added redundantly.
Provides conflict warnings Warns the user if Sanctum or Passport traits are detected, asking them to manually resolve conflicts.
Handles multiple traits in class Uses array_map to handle multiple traits and appends the new one if missing.
Writes changes to file Writes the updated content back to the file if modifications were made.

Potential issues:

  • If anyone has a better way to accurately find the correct text to change with regex, let me know.
  • Code simplified or improved to the Laravel way added this PR as a proof of concept if acceptable solution, may need improvements.
  • Scenario: How should we handle a situation where someone runs php artisan install:api and later runs php artisan install:api --passport?
    Proposed Solution: Notify the user if Sanctum or Passport traits are already present, advising them to manually address any potential conflicts.

…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.
@JoshSalway JoshSalway marked this pull request as draft December 20, 2024 14:28
- 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.
…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.
@JoshSalway JoshSalway marked this pull request as ready for review December 20, 2024 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant