|
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'] = collect($properties['attributes'])->diff($properties['old'])->all(); |
|
60
|
|
|
$properties['old'] = collect($properties['old'])->only(array_keys($properties['attributes']))->all(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $properties; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function logChanges(Model $model): array |
|
67
|
|
|
{ |
|
68
|
|
|
return collect($model->attributesToBeLogged())->mapWithKeys( |
|
69
|
|
|
function ($attribute) use ($model) { |
|
70
|
|
|
if (str_contains($attribute, '.')) { |
|
71
|
|
|
return self::getRelatedModelAttributeValue($model, $attribute); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return collect($model)->only($attribute); |
|
75
|
|
|
} |
|
76
|
|
|
)->toArray(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected static function getRelatedModelAttributeValue(Model $model, string $attribute): array |
|
80
|
|
|
{ |
|
81
|
|
|
if (substr_count($attribute, '.') > 1) { |
|
82
|
|
|
throw CouldNotLogChanges::invalidAttribute($attribute); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
list($relatedModelName, $relatedAttribute) = explode('.', $attribute); |
|
86
|
|
|
|
|
87
|
|
|
$relatedModel = $model->$relatedModelName ?? $model->$relatedModelName(); |
|
88
|
|
|
|
|
89
|
|
|
return ["{$relatedModelName}.{$relatedAttribute}" => $relatedModel->$relatedAttribute]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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: