1 | <?php |
||
14 | trait LogsActivity |
||
15 | { |
||
16 | use DetectsChanges; |
||
17 | |||
18 | protected $enableLoggingModelsEvents = true; |
||
19 | 216 | ||
20 | protected static function bootLogsActivity() |
||
21 | { |
||
22 | static::eventsToBeRecorded()->each(function ($eventName) { |
||
23 | 212 | return static::$eventName(function (Model $model) use ($eventName) { |
|
24 | 12 | if (! $model->shouldLogEvent($eventName)) { |
|
25 | return; |
||
26 | } |
||
27 | 208 | ||
28 | $description = $model->getDescriptionForEvent($eventName); |
||
29 | 208 | ||
30 | $logName = $model->getLogNameToUse($eventName); |
||
31 | 208 | ||
32 | if ($description == '') { |
||
33 | return; |
||
34 | } |
||
35 | 208 | ||
36 | $attrs = $model->attributeValuesToBeLogged($eventName); |
||
37 | 208 | ||
38 | 4 | if ($model->isLogEmpty($attrs) && ! $model->shouldSubmitEmptyLogs()) { |
|
39 | return; |
||
40 | } |
||
41 | 208 | ||
42 | 208 | $logger = app(ActivityLogger::class) |
|
43 | 208 | ->useLog($logName) |
|
44 | 208 | ->performedOn($model) |
|
45 | ->withProperties($attrs); |
||
46 | 208 | ||
47 | 8 | if (method_exists($model, 'tapActivity')) { |
|
48 | $logger->tap([$model, 'tapActivity'], $eventName); |
||
49 | } |
||
50 | 208 | ||
51 | 216 | $logger->log($description); |
|
52 | 216 | }); |
|
53 | 216 | }); |
|
54 | } |
||
55 | 72 | ||
56 | public function shouldSubmitEmptyLogs(): bool |
||
60 | 208 | ||
61 | public function isLogEmpty($attrs): bool |
||
65 | 12 | ||
66 | public function disableLogging() |
||
72 | 4 | ||
73 | public function enableLogging() |
||
79 | 28 | ||
80 | public function activities(): MorphMany |
||
84 | 204 | ||
85 | public function getDescriptionForEvent(string $eventName): string |
||
89 | 204 | ||
90 | public function getLogNameToUse(string $eventName = ''): string |
||
98 | |||
99 | /* |
||
100 | * Get the event names that should be recorded. |
||
101 | 216 | */ |
|
102 | protected static function eventsToBeRecorded(): Collection |
||
120 | 208 | ||
121 | public function attributesToBeIgnored(): array |
||
129 | 212 | ||
130 | protected function shouldLogEvent(string $eventName): bool |
||
151 | } |
||
152 |
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.