1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OwenIt\Auditing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use OwenIt\Auditing\Console\AuditDriverMakeCommand; |
7
|
|
|
use OwenIt\Auditing\Contracts\Auditor; |
|
|
|
|
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
use RecursiveIteratorIterator; |
10
|
|
|
use RecursiveRegexIterator; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class AuditingServiceProvider extends ServiceProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
protected $defer = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Bootstrap the service provider. |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
282 |
|
public function boot() |
26
|
|
|
{ |
27
|
282 |
|
$config = __DIR__.'/../config/audit.php'; |
28
|
282 |
|
$migration = __DIR__.'/../database/migrations/audits.stub'; |
29
|
|
|
|
30
|
|
|
// Lumen lacks a config_path() helper, so we use base_path() |
31
|
282 |
|
if($this->app->runningInConsole()) { |
32
|
|
|
|
33
|
282 |
|
$this->publishes( [ |
34
|
282 |
|
$config => base_path( 'config/audit.php' ), |
35
|
282 |
|
], 'config' ); |
36
|
|
|
|
37
|
282 |
|
$this->publishes( [ |
38
|
282 |
|
$migration => database_path( 'migrations/' . $this->deprecatedFileName() ), |
39
|
282 |
|
], 'migrations' ); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
282 |
|
$this->mergeConfigFrom($config, 'audit'); |
44
|
282 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check to see if package has been used previously. |
48
|
|
|
* If it has, use that file name, if not, use a file name that timestamps the day of this pull request |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
282 |
|
protected function deprecatedFileName() |
52
|
|
|
{ |
53
|
282 |
|
$iterator = iterator_to_array(new RecursiveIteratorIterator( |
54
|
282 |
|
new RecursiveRegexIterator( |
55
|
282 |
|
new RecursiveDirectoryIterator(database_path('migrations'), RecursiveDirectoryIterator::KEY_AS_PATHNAME|RecursiveDirectoryIterator::SKIP_DOTS), |
56
|
282 |
|
'/(\d{4})_(\d{2})_(\d{2})_(\d{6})_create_audits_table\.php/i', RecursiveRegexIterator::MATCH |
57
|
|
|
), |
58
|
282 |
|
RecursiveIteratorIterator::SELF_FIRST |
59
|
|
|
)); |
60
|
|
|
|
61
|
282 |
|
return count($iterator) ? reset($iterator)->getFilename() : '2018_11_22_000000_create_audits_table.php'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Register the service provider. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
282 |
|
public function register() |
70
|
|
|
{ |
71
|
282 |
|
$this->commands([ |
72
|
282 |
|
AuditDriverMakeCommand::class, |
73
|
|
|
]); |
74
|
|
|
|
75
|
282 |
|
$this->app->singleton(Auditor::class, function ($app) { |
76
|
147 |
|
return new \OwenIt\Auditing\Auditor($app); |
77
|
282 |
|
}); |
78
|
282 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
3 |
|
public function provides() |
84
|
|
|
{ |
85
|
|
|
return [ |
86
|
3 |
|
Auditor::class, |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: