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