|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Djunehor |
|
5
|
|
|
* Date: 10/5/2019 |
|
6
|
|
|
* Time: 1:27 AM. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Djunehor\EventRevert; |
|
10
|
|
|
|
|
11
|
|
|
use Djunehor\EventRevert\ModelLog as Activity; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Automatically Log Add, Update, Delete events of Model. |
|
16
|
|
|
*/ |
|
17
|
|
|
trait ModelEventLogger |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Automatically boot with Model, and register Events handler. |
|
21
|
|
|
*/ |
|
22
|
14 |
|
protected static function boot() |
|
23
|
|
|
{ |
|
24
|
14 |
|
parent::boot(); |
|
25
|
|
|
|
|
26
|
14 |
|
foreach (static::getRecordActivityEvents() as $eventName) { |
|
27
|
|
|
static::$eventName(function (Model $model) use ($eventName) { |
|
28
|
|
|
try { |
|
29
|
14 |
|
$reflect = new \ReflectionClass($model); |
|
30
|
|
|
|
|
31
|
14 |
|
return Activity::create([ |
|
32
|
14 |
|
'causer_id' => auth()->id(), |
|
|
|
|
|
|
33
|
14 |
|
'causer_type' => auth()->check() ? get_class(auth()->user()) : null, |
|
|
|
|
|
|
34
|
14 |
|
'subject_id' => $model->id, |
|
35
|
14 |
|
'subject_type' => get_class($model), |
|
36
|
14 |
|
'action' => static::getActionName($eventName), |
|
37
|
14 |
|
'description' => ucfirst($eventName).' a '.$reflect->getShortName(), |
|
38
|
14 |
|
'old' => json_encode($model->getOriginal()), |
|
39
|
14 |
|
'new' => json_encode($model->getDirty()), |
|
40
|
14 |
|
'route' => (php_sapi_name() === 'cli' or defined('STDIN')) ? null : request()->url(), |
|
41
|
|
|
]); |
|
42
|
|
|
} catch (\Exception $e) { |
|
43
|
|
|
echo $e->getMessage(); |
|
44
|
|
|
} |
|
45
|
14 |
|
}); |
|
46
|
|
|
} |
|
47
|
14 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set the default events to be recorded if the $recordEvents |
|
51
|
|
|
* property does not exist on the model. |
|
52
|
|
|
* |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
14 |
|
protected static function getRecordActivityEvents() |
|
56
|
|
|
{ |
|
57
|
14 |
|
if (isset(static::$recordEvents)) { |
|
58
|
|
|
return static::$recordEvents; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return [ |
|
62
|
14 |
|
'created', |
|
63
|
|
|
'updated', |
|
64
|
|
|
'deleted', |
|
65
|
|
|
// 'inserted', |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return Suitable action name for Supplied Event. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $event |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
14 |
|
protected static function getActionName($event) |
|
76
|
|
|
{ |
|
77
|
14 |
|
switch (strtolower($event)) { |
|
78
|
14 |
|
case 'created': |
|
79
|
14 |
|
return 'create'; |
|
80
|
|
|
break; |
|
|
|
|
|
|
81
|
7 |
|
case 'updated': |
|
82
|
6 |
|
return 'update'; |
|
83
|
|
|
break; |
|
|
|
|
|
|
84
|
1 |
|
case 'deleted': |
|
85
|
1 |
|
return 'delete'; |
|
86
|
|
|
break; |
|
|
|
|
|
|
87
|
|
|
default: |
|
88
|
|
|
return 'unknown'; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: