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