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