|
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
|
|
|
|
|
19
|
|
|
return static::$eventName(function (Model $model) use ($eventName) { |
|
20
|
|
|
|
|
21
|
|
|
if (count(array_except($model->getDirty(), $model->attributesToBeIgnored())) === 0) { |
|
22
|
|
|
return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$description = $model->getDescriptionForEvent($eventName); |
|
26
|
|
|
|
|
27
|
|
|
$logName = $model->getLogNameToUse($eventName); |
|
28
|
|
|
|
|
29
|
|
|
if ($description == '') { |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
app(ActivityLogger::class) |
|
34
|
|
|
->useLog($logName) |
|
35
|
|
|
->performedOn($model) |
|
36
|
|
|
->withProperties($model->attributeValuesToBeLogged($eventName)) |
|
37
|
|
|
->log($description); |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function activity(): MorphMany |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->morphMany(Activity::class, 'subject'); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getDescriptionForEvent(string $eventName): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $eventName; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getLogNameToUse(string $eventName = ''): string |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return config('laravel-activitylog.default_log_name'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
* Get the event names that should be recorded. |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static function eventsToBeRecorded(): Collection |
|
62
|
|
|
{ |
|
63
|
|
|
if (isset(static::$recordEvents)) { |
|
64
|
|
|
return collect(static::$recordEvents); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return collect([ |
|
68
|
|
|
'created', |
|
69
|
|
|
'updated', |
|
70
|
|
|
'deleted', |
|
71
|
|
|
]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function attributesToBeIgnored(): array |
|
75
|
|
|
{ |
|
76
|
|
|
if (!isset(static::$ignoreChangedAttributes)) { |
|
77
|
|
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return static::$ignoreChangedAttributes; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.