|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing; |
|
4
|
|
|
|
|
5
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
|
|
|
|
|
|
6
|
|
|
use OwenIt\Auditing\Facades\Auditor; |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class AuditableObserver |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Is the model being restored? |
|
12
|
|
|
* |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
public static $restoring = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Handle the retrieved event. |
|
19
|
|
|
* |
|
20
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function retrieved(Auditable $model) |
|
25
|
|
|
{ |
|
26
|
3 |
|
Auditor::execute($model->setAuditEvent('retrieved')); |
|
27
|
3 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Handle the created event. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
6 |
|
public function created(Auditable $model) |
|
37
|
|
|
{ |
|
38
|
6 |
|
Auditor::execute($model->setAuditEvent('created')); |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Handle the updated event. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function updated(Auditable $model) |
|
49
|
|
|
{ |
|
50
|
|
|
// Ignore the updated event when restoring |
|
51
|
|
|
if (!static::$restoring) { |
|
52
|
|
|
Auditor::execute($model->setAuditEvent('updated')); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Handle the deleted event. |
|
58
|
|
|
* |
|
59
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function deleted(Auditable $model) |
|
64
|
|
|
{ |
|
65
|
|
|
Auditor::execute($model->setAuditEvent('deleted')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Handle the restoring event. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
public function restoring(Auditable $model) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
// When restoring a model, an updated event is also fired. |
|
78
|
|
|
// By keeping track of the main event that took place, |
|
79
|
|
|
// we avoid creating a second audit with wrong values |
|
80
|
|
|
static::$restoring = true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Handle the restored event. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function restored(Auditable $model) |
|
91
|
|
|
{ |
|
92
|
|
|
Auditor::execute($model->setAuditEvent('restored')); |
|
93
|
|
|
|
|
94
|
|
|
// Once the model is restored, we need to put everything back |
|
95
|
|
|
// as before, in case a legitimate update event is fired |
|
96
|
|
|
static::$restoring = false; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: