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