Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| HasLifecycleHooks | |
100.00% |
26 / 26 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| bootHasLifecycleHooks | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| beforeValidate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| beforeSave | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| afterSave | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| beforeDelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| afterDelete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace DevToolbelt\LaravelEloquentPlus\Concerns; |
| 6 | |
| 7 | /** |
| 8 | * Trait for model lifecycle hooks. |
| 9 | * |
| 10 | * Provides hook methods that can be overridden in child classes |
| 11 | * to execute custom logic at specific points in the model lifecycle: |
| 12 | * - beforeValidate: Before validation runs |
| 13 | * - beforeSave: After validation, before database write |
| 14 | * - afterSave: After the model is persisted |
| 15 | * - beforeDelete: Before the model is deleted |
| 16 | * - afterDelete: After the model is deleted |
| 17 | * |
| 18 | * @package DevToolbelt\LaravelEloquentPlus\Concerns |
| 19 | */ |
| 20 | trait HasLifecycleHooks |
| 21 | { |
| 22 | /** |
| 23 | * Boot the HasLifecycleHooks trait. |
| 24 | * |
| 25 | * Registers model event listeners to trigger lifecycle hooks. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | protected static function bootHasLifecycleHooks(): void |
| 30 | { |
| 31 | static::creating(static function (self $model): void { |
| 32 | $model->beforeSave(); |
| 33 | }); |
| 34 | |
| 35 | static::updating(static function (self $model): void { |
| 36 | $model->beforeSave(); |
| 37 | }); |
| 38 | |
| 39 | static::created(static function (self $model): void { |
| 40 | $model->afterSave(); |
| 41 | }); |
| 42 | |
| 43 | static::updated(static function (self $model): void { |
| 44 | $model->afterSave(); |
| 45 | }); |
| 46 | |
| 47 | static::saved(static function (self $model): void { |
| 48 | $model->afterSave(); |
| 49 | }); |
| 50 | |
| 51 | static::deleting(static function (self $model): void { |
| 52 | $model->beforeDelete(); |
| 53 | }); |
| 54 | |
| 55 | static::deleted(static function (self $model): void { |
| 56 | $model->afterDelete(); |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Hook executed before validation runs. |
| 62 | * |
| 63 | * Override this method in child classes to perform actions |
| 64 | * before the model is validated (e.g., data normalization). |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | protected function beforeValidate(): void |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Hook executed before the model is saved (created or updated). |
| 74 | * |
| 75 | * Override this method in child classes to perform actions |
| 76 | * after validation passes but before the database write. |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | protected function beforeSave(): void |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Hook executed after the model is saved (created or updated). |
| 86 | * |
| 87 | * Override this method in child classes to perform actions |
| 88 | * after the model has been persisted to the database. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | protected function afterSave(): void |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Hook executed before the model is deleted. |
| 98 | * |
| 99 | * Override this method in child classes to perform actions |
| 100 | * before the model is removed (e.g., cleanup related data). |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | protected function beforeDelete(): void |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Hook executed after the model is deleted. |
| 110 | * |
| 111 | * Override this method in child classes to perform actions |
| 112 | * after the model has been removed from the database. |
| 113 | * |
| 114 | * @return void |
| 115 | */ |
| 116 | protected function afterDelete(): void |
| 117 | { |
| 118 | } |
| 119 | } |