|
5 | 5 | namespace Hypervel\Database\Eloquent\Concerns; |
6 | 6 |
|
7 | 7 | use Hyperf\Collection\Arr; |
| 8 | +use Hyperf\Collection\Collection; |
| 9 | +use Hyperf\Database\Model\Model as HyperfModel; |
8 | 10 | use Hypervel\Context\ApplicationContext; |
| 11 | +use Hypervel\Database\Eloquent\Attributes\ObservedBy; |
9 | 12 | use Hypervel\Database\Eloquent\ObserverManager; |
| 13 | +use ReflectionAttribute; |
| 14 | +use ReflectionClass; |
10 | 15 | use RuntimeException; |
11 | 16 |
|
12 | 17 | trait HasObservers |
13 | 18 | { |
| 19 | + /** |
| 20 | + * Boot the has observers trait for a model. |
| 21 | + * |
| 22 | + * Automatically registers any observers declared via the ObservedBy attribute. |
| 23 | + */ |
| 24 | + public static function bootHasObservers(): void |
| 25 | + { |
| 26 | + $observers = static::resolveObserveAttributes(); |
| 27 | + |
| 28 | + if (! empty($observers)) { |
| 29 | + static::observe($observers); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Resolve the observer class names from the ObservedBy attributes. |
| 35 | + * |
| 36 | + * Collects ObservedBy attributes from parent classes, traits, and the |
| 37 | + * current class itself, merging them together. The order is: |
| 38 | + * parent class observers -> trait observers -> class observers. |
| 39 | + * |
| 40 | + * @return array<int, class-string> |
| 41 | + */ |
| 42 | + public static function resolveObserveAttributes(): array |
| 43 | + { |
| 44 | + $reflectionClass = new ReflectionClass(static::class); |
| 45 | + |
| 46 | + $parentClass = get_parent_class(static::class); |
| 47 | + $hasParentWithTrait = $parentClass |
| 48 | + && $parentClass !== HyperfModel::class |
| 49 | + && method_exists($parentClass, 'resolveObserveAttributes'); |
| 50 | + |
| 51 | + // Collect attributes from traits, then from the class itself |
| 52 | + $attributes = new Collection(); |
| 53 | + |
| 54 | + foreach ($reflectionClass->getTraits() as $trait) { |
| 55 | + foreach ($trait->getAttributes(ObservedBy::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 56 | + $attributes->push($attribute); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + foreach ($reflectionClass->getAttributes(ObservedBy::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) { |
| 61 | + $attributes->push($attribute); |
| 62 | + } |
| 63 | + |
| 64 | + // Process all collected attributes |
| 65 | + $observers = $attributes |
| 66 | + ->map(fn (ReflectionAttribute $attribute) => $attribute->getArguments()) |
| 67 | + ->flatten(); |
| 68 | + |
| 69 | + // Prepend parent's observers if applicable |
| 70 | + return $observers |
| 71 | + ->when($hasParentWithTrait, function (Collection $attrs) use ($parentClass) { |
| 72 | + /** @var class-string $parentClass */ |
| 73 | + return (new Collection($parentClass::resolveObserveAttributes())) |
| 74 | + ->merge($attrs); |
| 75 | + }) |
| 76 | + ->all(); |
| 77 | + } |
| 78 | + |
14 | 79 | /** |
15 | 80 | * Register observers with the model. |
16 | 81 | * |
|
0 commit comments