|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Activitylog\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\Activitylog\ActivityLogger; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
9
|
|
|
use Spatie\Activitylog\ActivitylogServiceProvider; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
11
|
|
|
|
|
12
|
|
|
trait LogsActivity |
|
13
|
|
|
{ |
|
14
|
|
|
use DetectsChanges; |
|
15
|
|
|
|
|
16
|
|
|
protected $enableLoggingModelsEvents = true; |
|
17
|
|
|
|
|
18
|
|
|
protected static function bootLogsActivity() |
|
19
|
|
|
{ |
|
20
|
|
|
static::eventsToBeRecorded()->each(function ($eventName) { |
|
21
|
|
|
return static::$eventName(function (Model $model) use ($eventName) { |
|
22
|
|
|
if (! $model->shouldLogEvent($eventName)) { |
|
23
|
|
|
return; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$description = $model->getDescriptionForEvent($eventName); |
|
27
|
|
|
|
|
28
|
|
|
$logName = $model->getLogNameToUse($eventName); |
|
29
|
|
|
|
|
30
|
|
|
if ($description == '') { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
app(ActivityLogger::class) |
|
35
|
|
|
->useLog($logName) |
|
36
|
|
|
->performedOn($model) |
|
37
|
|
|
->withProperties($model->attributeValuesToBeLogged($eventName)) |
|
38
|
|
|
->log($description); |
|
39
|
|
|
}); |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function disableLogging() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->enableLoggingModelsEvents = false; |
|
46
|
|
|
|
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function enableLogging() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->enableLoggingModelsEvents = true; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function activity(): MorphMany |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->morphMany(ActivitylogServiceProvider::determineActivityModel(), 'subject'); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getDescriptionForEvent(string $eventName): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $eventName; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getLogNameToUse(string $eventName = ''): string |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
return config('laravel-activitylog.default_log_name'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* |
|
73
|
|
|
* Get the event names that should be recorded. |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static function eventsToBeRecorded(): Collection |
|
76
|
|
|
{ |
|
77
|
|
|
if (isset(static::$recordEvents)) { |
|
78
|
|
|
return collect(static::$recordEvents); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$events = collect([ |
|
82
|
|
|
'created', |
|
83
|
|
|
'updated', |
|
84
|
|
|
'deleted', |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
if (collect(class_uses_recursive(__CLASS__))->contains(SoftDeletes::class)) { |
|
88
|
|
|
$events->push('restored'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $events; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function attributesToBeIgnored(): array |
|
95
|
|
|
{ |
|
96
|
|
|
if (! isset(static::$ignoreChangedAttributes)) { |
|
97
|
|
|
return []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return static::$ignoreChangedAttributes; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function shouldLogEvent(string $eventName): bool |
|
104
|
|
|
{ |
|
105
|
|
|
if (! $this->enableLoggingModelsEvents) { |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (! in_array($eventName, ['created', 'updated'])) { |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (array_has($this->getDirty(), 'deleted_at')) { |
|
|
|
|
|
|
114
|
|
|
if ($this->getDirty()['deleted_at'] === null) { |
|
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
//do not log update event if only ignored attributes are changed |
|
120
|
|
|
return (bool) count(array_except($this->getDirty(), $this->attributesToBeIgnored())); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.