feat: Add #[Boot] and #[Initialize] attributes
#333
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.
This PR ports Laravel's
#[Boot]and#[Initialize]attributes to Hypervel, allowing trait methods to be marked as boot/initialize methods without requiring the conventional naming.Summary
#[Boot]attribute for marking static methods as boot methods#[Initialize]attribute for marking methods as initialize methodsHasBootableTraitstrait that overridesbootTraits()to support attributesModelto use the newHasBootableTraitstraitUsage
#[Boot] Attribute
Traditionally, trait boot methods must be named
boot{TraitName}:With the
#[Boot]attribute, you can name the method anything:#[Initialize] Attribute
Traditionally, trait initialize methods must be named
initialize{TraitName}:With the
#[Initialize]attribute, you can name the method anything:Changes
New Files
src/core/src/Database/Eloquent/Attributes/Boot.php- The Boot attribute classsrc/core/src/Database/Eloquent/Attributes/Initialize.php- The Initialize attribute classsrc/core/src/Database/Eloquent/Concerns/HasBootableTraits.php- Trait providing enhancedbootTraits()tests/Core/Database/Eloquent/Concerns/HasBootableTraitsTest.php- Tests (7 tests, 18 assertions)Modified Files
src/core/src/Database/Eloquent/Model.php- AddedHasBootableTraitstraitHow It Works
HasBootableTraitsoverrides Hyperf'sbootTraits()methodReflectionClassto iterate through all methods on the modelboot{TraitName}) and#[Boot]attributeinitialize{TraitName}) and#[Initialize]attributeNotes