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
|
|
|
if (! isset(static::$logAttributes)) { |
29
|
|
|
return []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return static::$logAttributes; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function shouldlogOnlyDirty(): bool |
36
|
|
|
{ |
37
|
|
|
if (! isset(static::$logOnlyDirty)) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return static::$logOnlyDirty; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function attributeValuesToBeLogged(string $processingEvent): array |
45
|
|
|
{ |
46
|
|
|
if (! count($this->attributesToBeLogged())) { |
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$properties['attributes'] = static::logChanges($this->exists ? $this->fresh() : $this); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { |
53
|
|
|
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null); |
54
|
|
|
|
55
|
|
|
$properties['old'] = array_merge($nullProperties, $this->oldAttributes); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->shouldlogOnlyDirty() && isset($properties['old'])) { |
59
|
|
|
$properties['attributes'] = array_udiff( |
60
|
|
|
$properties['attributes'], |
61
|
|
|
$properties['old'], |
62
|
|
|
function ($new, $old) { |
63
|
|
|
return $new <=> $old; |
64
|
|
|
} |
65
|
|
|
); |
66
|
|
|
$properties['old'] = collect($properties['old'])->only(array_keys($properties['attributes']))->all(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $properties; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function logChanges(Model $model): array |
73
|
|
|
{ |
74
|
|
|
return collect($model->attributesToBeLogged())->mapWithKeys( |
75
|
|
|
function ($attribute) use ($model) { |
76
|
|
|
if (str_contains($attribute, '.')) { |
77
|
|
|
return self::getRelatedModelAttributeValue($model, $attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return collect($model)->only($attribute); |
81
|
|
|
} |
82
|
|
|
)->toArray(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
86
|
|
|
{ |
87
|
|
|
if (substr_count($attribute, '.') > 1) { |
88
|
|
|
throw CouldNotLogChanges::invalidAttribute($attribute); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
list($relatedModelName, $relatedAttribute) = explode('.', $attribute); |
92
|
|
|
|
93
|
|
|
$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName(); |
94
|
|
|
|
95
|
|
|
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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: