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