1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing\Events; |
4
|
|
|
|
5
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class DispatchAudit |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The Auditable model. |
12
|
|
|
* |
13
|
|
|
* @var Auditable |
14
|
|
|
*/ |
15
|
|
|
public $model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new DispatchAudit event instance. |
19
|
|
|
* |
20
|
|
|
* @param Auditable $model |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Auditable $model) |
23
|
|
|
{ |
24
|
|
|
$this->model = $model; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Prepare the instance values for serialization. |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function __serialize() |
33
|
|
|
{ |
34
|
|
|
$values = [ |
35
|
|
|
'class' => get_class($this->model), |
36
|
|
|
'model_data' => [ |
37
|
|
|
'exists' => true, |
38
|
|
|
'connection' => $this->model->getQueueableConnection() |
|
|
|
|
39
|
|
|
] |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
$customProperties = array_merge([ |
43
|
|
|
'attributes', |
44
|
|
|
'original', |
45
|
|
|
'excludedAttributes', |
46
|
|
|
'auditEvent', |
47
|
|
|
'auditExclude', |
48
|
|
|
'auditCustomOld', |
49
|
|
|
'auditCustomNew', |
50
|
|
|
'isCustomEvent', |
51
|
|
|
'preloadedResolverData', |
52
|
|
|
], $this->model->auditEventSerializedProperties ?? []); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$reflection = new ReflectionClass($this->model); |
55
|
|
|
|
56
|
|
|
foreach ($customProperties as $key) { |
57
|
|
|
try { |
58
|
|
|
$values['model_data'][$key] = $this->getModelPropertyValue($reflection, $key); |
59
|
|
|
} catch (\Throwable $e){ |
60
|
|
|
// |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $values; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Restore the model after serialization. |
69
|
|
|
* |
70
|
|
|
* @param array $values |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function __unserialize(array $values) |
74
|
|
|
{ |
75
|
|
|
$this->model = new $values['class']; |
76
|
|
|
|
77
|
|
|
$reflection = new ReflectionClass($this->model); |
78
|
|
|
foreach ($values['model_data'] as $key => $value) { |
79
|
|
|
$this->setModelPropertyValue($reflection, $key, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $values; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set the property value for the given property. |
87
|
|
|
*/ |
88
|
|
|
protected function setModelPropertyValue(ReflectionClass $reflection, string $name, $value) |
89
|
|
|
{ |
90
|
|
|
$property = $reflection->getProperty($name); |
91
|
|
|
|
92
|
|
|
$property->setAccessible(true); |
93
|
|
|
|
94
|
|
|
$property->setValue($this->model, $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the property value for the given property. |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
protected function getModelPropertyValue(ReflectionClass $reflection, string $name) |
103
|
|
|
{ |
104
|
|
|
$property = $reflection->getProperty($name); |
105
|
|
|
|
106
|
|
|
$property->setAccessible(true); |
107
|
|
|
|
108
|
|
|
return $property->getValue($this->model); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|