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