1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Spatie\Activitylog\ActivityLogger; |
11
|
|
|
use Spatie\Activitylog\ActivitylogServiceProvider; |
12
|
|
|
|
13
|
|
|
trait ActivityLogs |
14
|
|
|
{ |
15
|
|
|
use DetectsChanges; |
16
|
|
|
|
17
|
|
|
protected $enableLoggingModelsEvents = true; |
18
|
|
|
|
19
|
|
|
protected static function bootLogsActivity() |
20
|
|
|
{ |
21
|
|
|
static::eventsToBeRecorded()->each(function ($eventName) { |
22
|
|
|
return static::$eventName(function (Model $model) use ($eventName) { |
23
|
|
|
if (! $model->shouldLogEvent($eventName)) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$description = $model->getDescriptionForEvent($eventName); |
28
|
|
|
|
29
|
|
|
$logName = $model->getLogNameToUse($eventName); |
30
|
|
|
|
31
|
|
|
if ($description == '') { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$attrs = $model->attributeValuesToBeLogged($eventName); |
36
|
|
|
|
37
|
|
|
if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$logger = app(ActivityLogger::class) |
42
|
|
|
->useLog($logName) |
43
|
|
|
->performedOn($model) |
44
|
|
|
->withProperties($attrs); |
45
|
|
|
|
46
|
|
|
if (method_exists($model, 'tapActivity')) { |
47
|
|
|
$logger->tap([$model, 'tapActivity'], $eventName); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$logger->log($description); |
51
|
|
|
}); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function shouldSubmitEmptyLogs(): bool |
56
|
|
|
{ |
57
|
|
|
return ! isset(static::$submitEmptyLogs) ? true : static::$submitEmptyLogs; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isLogEmpty($attrs): bool |
61
|
|
|
{ |
62
|
|
|
return empty($attrs['attributes'] ?? []) && empty($attrs['old'] ?? []); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function disableLogging() |
66
|
|
|
{ |
67
|
|
|
$this->enableLoggingModelsEvents = false; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function enableLogging() |
73
|
|
|
{ |
74
|
|
|
$this->enableLoggingModelsEvents = true; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function activities(): MorphMany |
80
|
|
|
{ |
81
|
|
|
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject'); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getDescriptionForEvent(string $eventName): string |
85
|
|
|
{ |
86
|
|
|
return $eventName; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getLogNameToUse(string $eventName = ''): string |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
if (isset(static::$logName)) { |
92
|
|
|
return static::$logName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return config('activitylog.default_log_name'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/* |
99
|
|
|
* Get the event names that should be recorded. |
100
|
|
|
*/ |
101
|
|
|
protected static function eventsToBeRecorded(): Collection |
102
|
|
|
{ |
103
|
|
|
if (isset(static::$recordEvents)) { |
104
|
|
|
return collect(static::$recordEvents); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$events = collect([ |
108
|
|
|
'created', |
109
|
|
|
'updated', |
110
|
|
|
'deleted', |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
if (collect(class_uses_recursive(static::class))->contains(SoftDeletes::class)) { |
114
|
|
|
$events->push('restored'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $events; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function attributesToBeIgnored(): array |
121
|
|
|
{ |
122
|
|
|
if (! isset(static::$ignoreChangedAttributes)) { |
123
|
|
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return static::$ignoreChangedAttributes; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function shouldLogEvent(string $eventName): bool |
130
|
|
|
{ |
131
|
|
|
if (! $this->enableLoggingModelsEvents) { |
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (! in_array($eventName, ['created', 'updated'])) { |
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (Arr::has($this->getDirty(), 'deleted_at')) { |
|
|
|
|
140
|
|
|
if ($this->getDirty()['deleted_at'] === null) { |
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
//do not log update event if only ignored attributes are changed |
146
|
|
|
return (bool) count(Arr::except($this->getDirty(), $this->attributesToBeIgnored())); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.