1 | <?php |
||
10 | trait DetectsChanges |
||
11 | { |
||
12 | protected $oldAttributes = []; |
||
13 | |||
14 | 216 | protected static function bootDetectsChanges() |
|
15 | { |
||
16 | 216 | if (static::eventsToBeRecorded()->contains('updated')) { |
|
17 | static::updating(function (Model $model) { |
||
18 | |||
19 | //temporary hold the original attributes on the model |
||
20 | //as we'll need these in the updating event |
||
21 | 112 | $oldValues = (new static)->setRawAttributes($model->getOriginal()); |
|
|
|||
22 | |||
23 | 112 | $model->oldAttributes = static::logChanges($oldValues); |
|
24 | 216 | }); |
|
25 | } |
||
26 | 216 | } |
|
27 | |||
28 | 208 | public function attributesToBeLogged(): array |
|
29 | { |
||
30 | 208 | $attributes = []; |
|
31 | |||
32 | 208 | if (isset(static::$logFillable) && static::$logFillable) { |
|
33 | 8 | $attributes = array_merge($attributes, $this->getFillable()); |
|
34 | } |
||
35 | |||
36 | 208 | if ($this->shouldLogUnguarded()) { |
|
37 | 4 | $attributes = array_merge($attributes, array_diff(array_keys($this->getAttributes()), $this->getGuarded())); |
|
38 | } |
||
39 | |||
40 | 208 | if (isset(static::$logAttributes) && is_array(static::$logAttributes)) { |
|
41 | 136 | $attributes = array_merge($attributes, array_diff(static::$logAttributes, ['*'])); |
|
42 | |||
43 | 136 | if (in_array('*', static::$logAttributes)) { |
|
44 | 32 | $attributes = array_merge($attributes, array_keys($this->getAttributes())); |
|
45 | } |
||
46 | } |
||
47 | |||
48 | 208 | if (isset(static::$logAttributesToIgnore) && is_array(static::$logAttributesToIgnore)) { |
|
49 | 8 | $attributes = array_diff($attributes, static::$logAttributesToIgnore); |
|
50 | } |
||
51 | |||
52 | 208 | return $attributes; |
|
53 | } |
||
54 | |||
55 | 140 | public function shouldLogOnlyDirty(): bool |
|
56 | { |
||
57 | 140 | if (! isset(static::$logOnlyDirty)) { |
|
58 | 80 | return false; |
|
59 | } |
||
60 | |||
61 | 60 | return static::$logOnlyDirty; |
|
62 | } |
||
63 | |||
64 | 208 | public function shouldLogUnguarded(): bool |
|
65 | { |
||
66 | 208 | if (! isset(static::$logUnguarded)) { |
|
67 | 200 | return false; |
|
68 | } |
||
69 | |||
70 | 8 | if (! static::$logUnguarded) { |
|
71 | return false; |
||
72 | } |
||
73 | |||
74 | 8 | if (in_array('*', $this->getGuarded())) { |
|
75 | 4 | return false; |
|
76 | } |
||
77 | |||
78 | 4 | return true; |
|
79 | } |
||
80 | |||
81 | 208 | public function attributeValuesToBeLogged(string $processingEvent): array |
|
120 | |||
121 | 164 | public static function logChanges(Model $model): array |
|
122 | { |
||
123 | 164 | $changes = []; |
|
124 | 164 | $attributes = $model->attributesToBeLogged(); |
|
151 | |||
152 | 24 | protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
|
166 | |||
167 | 24 | protected static function getModelAttributeJsonValue(Model $model, string $attribute) |
|
175 | } |
||
176 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.