1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Manager; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use OwenIt\Auditing\Contracts\Auditable; |
|
|
|
|
8
|
|
|
use OwenIt\Auditing\Contracts\AuditDriver; |
9
|
|
|
use OwenIt\Auditing\Drivers\Database; |
10
|
|
|
use OwenIt\Auditing\Events\Audited; |
11
|
|
|
use OwenIt\Auditing\Events\Auditing; |
12
|
|
|
use OwenIt\Auditing\Exceptions\AuditingException; |
13
|
|
|
|
14
|
|
|
class Auditor extends Manager implements Contracts\Auditor |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public function getDefaultDriver() |
20
|
|
|
{ |
21
|
|
|
return 'database'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
12 |
|
protected function createDriver($driver) |
28
|
|
|
{ |
29
|
|
|
try { |
30
|
12 |
|
return parent::createDriver($driver); |
31
|
|
|
} catch (InvalidArgumentException $exception) { |
32
|
|
|
if (class_exists($driver)) { |
33
|
|
|
return $this->container->make($driver); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
throw $exception; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
12 |
|
public function auditDriver(Auditable $model): AuditDriver |
44
|
|
|
{ |
45
|
12 |
|
$driver = $this->driver($model->getAuditDriver()); |
46
|
|
|
|
47
|
12 |
|
if (!$driver instanceof AuditDriver) { |
48
|
|
|
throw new AuditingException('The driver must implement the AuditDriver contract'); |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
return $driver; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
12 |
|
public function execute(Auditable $model) |
58
|
|
|
{ |
59
|
12 |
|
if (!$model->readyForAuditing()) { |
60
|
4 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
$driver = $this->auditDriver($model); |
64
|
|
|
|
65
|
12 |
|
if (!$this->fireAuditingEvent($model, $driver)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
12 |
|
if ($audit = $driver->audit($model)) { |
70
|
12 |
|
$driver->prune($model); |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
$this->container->make('events')->dispatch( |
74
|
12 |
|
new Audited($model, $driver, $audit) |
75
|
|
|
); |
76
|
12 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create an instance of the Database audit driver. |
80
|
|
|
* |
81
|
|
|
* @return \OwenIt\Auditing\Drivers\Database |
82
|
|
|
*/ |
83
|
12 |
|
protected function createDatabaseDriver(): Database |
84
|
|
|
{ |
85
|
12 |
|
return $this->container->make(Database::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fire the Auditing event. |
90
|
|
|
* |
91
|
|
|
* @param \OwenIt\Auditing\Contracts\Auditable $model |
92
|
|
|
* @param \OwenIt\Auditing\Contracts\AuditDriver $driver |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
12 |
|
protected function fireAuditingEvent(Auditable $model, AuditDriver $driver): bool |
97
|
|
|
{ |
98
|
12 |
|
return $this->container->make('events')->until( |
99
|
12 |
|
new Auditing($model, $driver) |
100
|
12 |
|
) !== false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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: