1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Illuminate\Support\Manager; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
|
|
|
|
9
|
|
|
use OwenIt\Auditing\Contracts\AuditDriver; |
10
|
|
|
use OwenIt\Auditing\Drivers\Database; |
11
|
|
|
use OwenIt\Auditing\Events\Audited; |
12
|
|
|
use OwenIt\Auditing\Events\Auditing; |
13
|
|
|
use OwenIt\Auditing\Exceptions\AuditingException; |
14
|
|
|
|
15
|
|
|
class Auditor extends Manager implements Contracts\Auditor |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
|
|
public function getDefaultDriver() |
21
|
|
|
{ |
22
|
|
|
return 'database'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
15 |
|
protected function createDriver($driver) |
29
|
|
|
{ |
30
|
|
|
try { |
31
|
15 |
|
return parent::createDriver($driver); |
32
|
|
|
} catch (InvalidArgumentException $exception) { |
33
|
|
|
if (class_exists($driver)) { |
34
|
|
|
return $this->container->make($driver); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw $exception; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
15 |
|
public function auditDriver(Auditable $model): AuditDriver |
45
|
|
|
{ |
46
|
15 |
|
$driver = $this->driver($model->getAuditDriver()); |
47
|
|
|
|
48
|
15 |
|
if (!$driver instanceof AuditDriver) { |
49
|
|
|
throw new AuditingException('The driver must implement the AuditDriver contract'); |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
return $driver; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
15 |
|
public function execute(Auditable $model): void |
59
|
|
|
{ |
60
|
15 |
|
if (!$model->readyForAuditing()) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
15 |
|
$driver = $this->auditDriver($model); |
65
|
|
|
|
66
|
15 |
|
if (!$this->fireAuditingEvent($model, $driver)) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Check if we want to avoid storing empty values |
71
|
15 |
|
$allowEmpty = Config::get('audit.empty_values'); |
72
|
15 |
|
$explicitAllowEmpty = in_array($model->getAuditEvent(), Config::get('audit.allowed_empty_values', [])); |
73
|
|
|
|
74
|
15 |
|
if (!$allowEmpty && !$explicitAllowEmpty) { |
75
|
|
|
if ( |
76
|
|
|
empty($model->toAudit()['new_values']) && |
77
|
|
|
empty($model->toAudit()['old_values']) |
78
|
|
|
) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
$audit = $driver->audit($model); |
84
|
15 |
|
if (!$audit) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
15 |
|
$driver->prune($model); |
89
|
|
|
|
90
|
15 |
|
$this->container->make('events')->dispatch( |
91
|
15 |
|
new Audited($model, $driver, $audit) |
92
|
15 |
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create an instance of the Database audit driver. |
97
|
|
|
*/ |
98
|
|
|
protected function createDatabaseDriver(): Database |
99
|
|
|
{ |
100
|
15 |
|
return $this->container->make(Database::class); |
101
|
|
|
} |
102
|
15 |
|
|
103
|
|
|
/** |
104
|
|
|
* Fire the Auditing event. |
105
|
|
|
*/ |
106
|
|
|
protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool |
107
|
|
|
{ |
108
|
|
|
return $this |
109
|
|
|
->container |
110
|
|
|
->make('events') |
111
|
|
|
->until(new Auditing($model, $driver)) !== false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: