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
|
|
|
//temporary hold the original attributes on the model |
17
|
|
|
//as we'll need these in the updating event |
18
|
|
|
$oldValues = $model->replicate()->setRawAttributes($model->getOriginal()); |
19
|
|
|
|
20
|
|
|
$model->oldAttributes = static::logChanges($oldValues); |
21
|
|
|
}); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function attributesToBeLogged(): array |
26
|
|
|
{ |
27
|
|
|
if (! isset(static::$logAttributes)) { |
28
|
|
|
return []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return static::$logAttributes; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function shouldLogDirtyOnly(): bool |
35
|
|
|
{ |
36
|
|
|
if (! isset(static::$logDirtyOnly)) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return static::$logDirtyOnly; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function attributeValuesToBeLogged(string $processingEvent): array |
44
|
|
|
{ |
45
|
|
|
if (! count($this->attributesToBeLogged())) { |
46
|
|
|
return []; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$properties['attributes'] = static::logChanges($this->exists ? $this->fresh() : $this); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (static::eventsToBeRecorded()->contains('updated') && $processingEvent == 'updated') { |
52
|
|
|
$nullProperties = array_fill_keys(array_keys($properties['attributes']), null); |
53
|
|
|
|
54
|
|
|
$properties['old'] = array_merge($nullProperties, $this->oldAttributes); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Only dirty fields |
58
|
|
|
if (isset($properties['old']) && $this->shouldLogDirtyOnly()) { |
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: