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