1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Spatie\Activitylog\Exceptions\CouldNotLogChanges; |
10
|
|
|
|
11
|
|
|
trait DetectsChanges |
12
|
|
|
{ |
13
|
|
|
protected $oldAttributes = []; |
14
|
|
|
|
15
|
224 |
|
protected static function bootDetectsChanges() |
16
|
|
|
{ |
17
|
224 |
|
if (static::eventsToBeRecorded()->contains('updated')) { |
18
|
|
|
static::updating(function (Model $model) { |
19
|
|
|
|
20
|
|
|
//temporary hold the original attributes on the model |
21
|
|
|
//as we'll need these in the updating event |
22
|
112 |
|
$oldValues = (new static)->setRawAttributes($model->getOriginal()); |
|
|
|
|
23
|
|
|
|
24
|
112 |
|
$model->oldAttributes = static::logChanges($oldValues); |
25
|
224 |
|
}); |
26
|
|
|
} |
27
|
224 |
|
} |
28
|
|
|
|
29
|
216 |
|
public function attributesToBeLogged(): array |
30
|
|
|
{ |
31
|
216 |
|
$attributes = []; |
32
|
|
|
|
33
|
216 |
|
if (isset(static::$logFillable) && static::$logFillable) { |
34
|
8 |
|
$attributes = array_merge($attributes, $this->getFillable()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
216 |
|
if ($this->shouldLogUnguarded()) { |
38
|
4 |
|
$attributes = array_merge($attributes, array_diff(array_keys($this->getAttributes()), $this->getGuarded())); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
216 |
|
if (isset(static::$logAttributes) && is_array(static::$logAttributes)) { |
42
|
144 |
|
$attributes = array_merge($attributes, array_diff(static::$logAttributes, ['*'])); |
43
|
|
|
|
44
|
144 |
|
if (in_array('*', static::$logAttributes)) { |
45
|
40 |
|
$attributes = array_merge($attributes, array_keys($this->getAttributes())); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
216 |
|
if (isset(static::$logAttributesToIgnore) && is_array(static::$logAttributesToIgnore)) { |
50
|
8 |
|
$attributes = array_diff($attributes, static::$logAttributesToIgnore); |
51
|
|
|
} |
52
|
|
|
|
53
|
216 |
|
return $attributes; |
54
|
|
|
} |
55
|
|
|
|
56
|
148 |
|
public function shouldLogOnlyDirty(): bool |
57
|
|
|
{ |
58
|
148 |
|
if (! isset(static::$logOnlyDirty)) { |
59
|
88 |
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
60 |
|
return static::$logOnlyDirty; |
63
|
|
|
} |
64
|
|
|
|
65
|
216 |
|
public function shouldLogUnguarded(): bool |
66
|
|
|
{ |
67
|
216 |
|
if (! isset(static::$logUnguarded)) { |
68
|
208 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
if (! static::$logUnguarded) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
if (in_array('*', $this->getGuarded())) { |
|
|
|
|
76
|
4 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
216 |
|
public function attributeValuesToBeLogged(string $processingEvent): array |
83
|
|
|
{ |
84
|
216 |
|
if (! count($this->attributesToBeLogged())) { |
85
|
68 |
|
return []; |
86
|
|
|
} |
87
|
|
|
|
88
|
148 |
|
$properties['attributes'] = static::logChanges( |
|
|
|
|
89
|
148 |
|
$this->exists |
|
|
|
|
90
|
148 |
|
? $this->fresh() ?? $this |
|
|
|
|
91
|
148 |
|
: $this |
92
|
|
|
); |
93
|
|
|
|
94
|
148 |
|
if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { |
95
|
88 |
|
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null); |
96
|
|
|
|
97
|
88 |
|
$properties['old'] = array_merge($nullProperties, $this->oldAttributes); |
98
|
|
|
|
99
|
88 |
|
$this->oldAttributes = []; |
100
|
|
|
} |
101
|
|
|
|
102
|
148 |
|
if ($this->shouldLogOnlyDirty() && isset($properties['old'])) { |
103
|
56 |
|
$properties['attributes'] = array_udiff_assoc( |
104
|
56 |
|
$properties['attributes'], |
105
|
56 |
|
$properties['old'], |
106
|
|
|
function ($new, $old) { |
107
|
56 |
|
if ($old === null || $new === null) { |
108
|
20 |
|
return $new === $old ? 0 : 1; |
109
|
|
|
} |
110
|
|
|
|
111
|
52 |
|
return $new <=> $old; |
112
|
56 |
|
} |
113
|
|
|
); |
114
|
56 |
|
$properties['old'] = collect($properties['old']) |
115
|
56 |
|
->only(array_keys($properties['attributes'])) |
116
|
56 |
|
->all(); |
117
|
|
|
} |
118
|
|
|
|
119
|
148 |
|
return $properties; |
120
|
|
|
} |
121
|
|
|
|
122
|
172 |
|
public static function logChanges(Model $model): array |
123
|
|
|
{ |
124
|
172 |
|
$changes = []; |
125
|
172 |
|
$attributes = $model->attributesToBeLogged(); |
126
|
|
|
|
127
|
172 |
|
foreach ($attributes as $attribute) { |
128
|
148 |
|
if (Str::contains($attribute, '.')) { |
129
|
24 |
|
$changes += self::getRelatedModelAttributeValue($model, $attribute); |
130
|
|
|
|
131
|
24 |
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
148 |
|
if (Str::contains($attribute, '->')) { |
135
|
24 |
|
Arr::set( |
136
|
24 |
|
$changes, |
137
|
24 |
|
str_replace('->', '.', $attribute), |
138
|
24 |
|
static::getModelAttributeJsonValue($model, $attribute) |
139
|
|
|
); |
140
|
|
|
|
141
|
24 |
|
continue; |
142
|
|
|
} |
143
|
|
|
|
144
|
148 |
|
$changes[$attribute] = $model->getAttribute($attribute); |
145
|
|
|
|
146
|
148 |
|
if (is_null($changes[$attribute])) { |
147
|
72 |
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
148 |
|
if ($model->isDateAttribute($attribute)) { |
|
|
|
|
151
|
40 |
|
$changes[$attribute] = $model->serializeDate( |
|
|
|
|
152
|
40 |
|
$model->asDateTime($changes[$attribute]) |
|
|
|
|
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
148 |
|
if ($model->hasCast($attribute)) { |
157
|
48 |
|
$cast = $model->getCasts()[$attribute]; |
158
|
|
|
|
159
|
48 |
|
if ($model->isCustomDateTimeCast($cast)) { |
|
|
|
|
160
|
4 |
|
$changes[$attribute] = $model->asDateTime($changes[$attribute])->format(explode(':', $cast, 2)[1]); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
172 |
|
return $changes; |
166
|
|
|
} |
167
|
|
|
|
168
|
24 |
|
protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
169
|
|
|
{ |
170
|
24 |
|
if (substr_count($attribute, '.') > 1) { |
171
|
|
|
throw CouldNotLogChanges::invalidAttribute($attribute); |
172
|
|
|
} |
173
|
|
|
|
174
|
24 |
|
[$relatedModelName, $relatedAttribute] = explode('.', $attribute); |
|
|
|
|
175
|
|
|
|
176
|
24 |
|
$relatedModelName = Str::camel($relatedModelName); |
|
|
|
|
177
|
|
|
|
178
|
24 |
|
$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName(); |
179
|
|
|
|
180
|
24 |
|
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute ?? null]; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
24 |
|
protected static function getModelAttributeJsonValue(Model $model, string $attribute) |
184
|
|
|
{ |
185
|
24 |
|
$path = explode('->', $attribute); |
186
|
24 |
|
$modelAttribute = array_shift($path); |
187
|
24 |
|
$modelAttribute = collect($model->getAttribute($modelAttribute)); |
188
|
|
|
|
189
|
24 |
|
return data_get($modelAttribute, implode('.', $path)); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.