1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gamer\Observers; |
4
|
|
|
|
5
|
|
|
use Event; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Log; |
8
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @todo Passar pa/ support |
13
|
|
|
* Call no-op classes on models for all event types. This just simplifies |
14
|
|
|
* the handling of model events for models. |
15
|
|
|
*/ |
16
|
|
|
class BaseObserver |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Only log the following events |
20
|
|
|
* |
21
|
|
|
* @param array |
22
|
|
|
*/ |
23
|
|
|
protected $supported = ['created', 'updated', 'deleted']; |
24
|
|
|
|
25
|
|
|
protected $action = false; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
protected function isToIgnore($model, $event) |
29
|
|
|
{ |
30
|
|
|
// Don't log changes to pivot models. Even though a user may have initiated |
31
|
|
|
// this, it's kind of meaningless to them. These events can happen when a |
32
|
|
|
// user messes with drag and drop positioning. |
33
|
|
|
if (!empty($this->getDontLog())) { |
34
|
|
|
foreach ($this->getDontLog() as $logClass) { |
35
|
|
|
if (is_a($model, $logClass)) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
if (!empty($this->getDontLogAlias())) { |
41
|
|
|
foreach ($this->getDontLogAlias() as $logClassAlias) { |
42
|
|
|
if (strpos($event, $logClassAlias) !== false) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Get the action of the event |
49
|
|
|
preg_match('#eloquent\.(\w+)#', $event, $matches); |
50
|
|
|
$this->action = $matches[1]; |
|
|
|
|
51
|
|
|
if (!in_array($this->action, $this->supported)) { |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function getDontLog() |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
|
|
'Aschmelyun\Larametrics\Models\LarametricsLog', |
62
|
|
|
'Illuminate\Database\Eloquent\Relations\Pivot', |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getDontLogAlias() |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
'Tracking\Models', |
70
|
|
|
'Analytics', |
71
|
|
|
'Spatie\Analytics', |
72
|
|
|
'Wnx\LaravelStats', |
73
|
|
|
'Aschmelyun\Larametrics\Models', |
74
|
|
|
'Laravel\Horizon', |
75
|
|
|
'Support\Models\Application', |
76
|
|
|
'Support\Models\Ardent', |
77
|
|
|
'Support\Models\Code', |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.