1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Spatie\Activitylog\Exceptions\CouldNotLogChanges; |
7
|
|
|
|
8
|
|
|
trait DetectsChanges |
9
|
|
|
{ |
10
|
|
|
protected $oldAttributes = []; |
11
|
|
|
|
12
|
|
|
protected static function bootDetectsChanges() |
13
|
|
|
{ |
14
|
|
|
if (static::eventsToBeRecorded()->contains('updated')) { |
15
|
|
|
static::updating(function (Model $model) { |
16
|
|
|
|
17
|
|
|
//temporary hold the original attributes on the model |
18
|
|
|
//as we'll need these in the updating event |
19
|
|
|
$oldValues = $model->replicate()->setRawAttributes($model->getOriginal()); |
20
|
|
|
|
21
|
|
|
$model->oldAttributes = static::logChanges($oldValues); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function attributesToBeLogged(): array |
27
|
|
|
{ |
28
|
|
|
$attributes = []; |
29
|
|
|
|
30
|
|
|
if (isset(static::$logFillable) && static::$logFillable) { |
31
|
|
|
$attributes = array_merge($attributes, $this->fillable); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (isset(static::$logAttributes) && is_array(static::$logAttributes)) { |
35
|
|
|
if (in_array('*', static::$logAttributes)) { |
36
|
|
|
$withoutWildcard = array_diff(static::$logAttributes, ['*']); |
37
|
|
|
|
38
|
|
|
$attributes = array_merge($attributes, array_keys($this->attributes), $withoutWildcard); |
|
|
|
|
39
|
|
|
} else { |
40
|
|
|
$attributes = array_merge($attributes, static::$logAttributes); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $attributes; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function shouldlogOnlyDirty(): bool |
48
|
|
|
{ |
49
|
|
|
if (! isset(static::$logOnlyDirty)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return static::$logOnlyDirty; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function attributeValuesToBeLogged(string $processingEvent): array |
57
|
|
|
{ |
58
|
|
|
if (! count($this->attributesToBeLogged())) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$properties['attributes'] = static::logChanges( |
|
|
|
|
63
|
|
|
$this->exists |
|
|
|
|
64
|
|
|
? $this->fresh() ?? $this |
|
|
|
|
65
|
|
|
: $this |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { |
69
|
|
|
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null); |
70
|
|
|
|
71
|
|
|
$properties['old'] = array_merge($nullProperties, $this->oldAttributes); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->shouldlogOnlyDirty() && isset($properties['old'])) { |
75
|
|
|
$properties['attributes'] = array_udiff_assoc( |
76
|
|
|
$properties['attributes'], |
77
|
|
|
$properties['old'], |
78
|
|
|
function ($new, $old) { |
79
|
|
|
return $new <=> $old; |
80
|
|
|
} |
81
|
|
|
); |
82
|
|
|
$properties['old'] = collect($properties['old']) |
83
|
|
|
->only(array_keys($properties['attributes'])) |
84
|
|
|
->all(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $properties; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function logChanges(Model $model): array |
91
|
|
|
{ |
92
|
|
|
$changes = []; |
93
|
|
|
foreach ($model->attributesToBeLogged() as $attribute) { |
94
|
|
|
if (str_contains($attribute, '.')) { |
95
|
|
|
$changes += self::getRelatedModelAttributeValue($model, $attribute); |
96
|
|
|
} else { |
97
|
|
|
$changes += collect($model)->only($attribute)->toArray(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $changes; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
105
|
|
|
{ |
106
|
|
|
if (substr_count($attribute, '.') > 1) { |
107
|
|
|
throw CouldNotLogChanges::invalidAttribute($attribute); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
list($relatedModelName, $relatedAttribute) = explode('.', $attribute); |
111
|
|
|
|
112
|
|
|
$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName(); |
113
|
|
|
|
114
|
|
|
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute ?? null]; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: