feat: Add #[ObservedBy] attribute support
#326
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 adds Laravel-style
#[ObservedBy]attribute support to Hypervel, allowing observers to be declared directly on model classes.Features
#[ObservedBy]AttributeDeclare observers directly on your models:
Inheritance Support
Observers declared on parent classes are automatically inherited by children:
Pivot Model Support
This PR also adds observer support to
PivotandMorphPivotmodels, similar to laravel. This was previously missing in Hypervel:Changes
New Files
src/core/src/Database/Eloquent/Attributes/ObservedBy.php- The attribute classtests/Core/Database/Eloquent/Concerns/HasObserversTest.php- Tests (13 tests, 16 assertions)Modified Files
src/core/src/Database/Eloquent/Concerns/HasObservers.php- AddedbootHasObservers()andresolveObserveAttributes()src/core/src/Database/Eloquent/Relations/Pivot.php- AddedHasObserverstraitsrc/core/src/Database/Eloquent/Relations/MorphPivot.php- AddedHasObserverstraitImplementation Notes
Laravel puts observer functionality in
HasEvents, but Hypervel already has a separateHasObserverstrait. This PR extends that existing pattern.For pivot models, Laravel's
PivotextendsModeldirectly, so it inherits observer support automatically. Hypervel'sPivotextends Hyperf'sPivot(which extends Hyperf'sModel), so we add theHasObserverstrait directly toPivotandMorphPivotto achieve the same result.Testing
All tests pass:
HasObserversTest- 13 tests covering attribute resolution, inheritance, and pivot supportObserverManagerTest- Existing tests still passModelListenerTest- Existing tests still pass