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 |
|
$this->publishes([ |
32
|
282 |
|
$config => base_path('config/audit.php'), |
33
|
282 |
|
], 'config'); |
34
|
|
|
|
35
|
282 |
|
$this->publishes([ |
36
|
282 |
|
$migration => database_path('migrations/'.$this->deprecatedFileName()), |
37
|
282 |
|
], 'migrations'); |
38
|
|
|
|
39
|
282 |
|
$this->mergeConfigFrom($config, 'audit'); |
40
|
282 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check to see if package has been used previously. |
44
|
|
|
* If it has, use that file name, if not, use a file name that timestamps the day of this pull request |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
282 |
|
protected function deprecatedFileName() |
48
|
|
|
{ |
49
|
282 |
|
$iterator = iterator_to_array(new RecursiveIteratorIterator( |
50
|
282 |
|
new RecursiveRegexIterator( |
51
|
282 |
|
new RecursiveDirectoryIterator(database_path('migrations'), RecursiveDirectoryIterator::KEY_AS_PATHNAME|RecursiveDirectoryIterator::SKIP_DOTS), |
52
|
282 |
|
'/(\d{4})_(\d{2})_(\d{2})_(\d{6})_create_audits_table\.php/i', RecursiveRegexIterator::MATCH |
53
|
|
|
), |
54
|
282 |
|
RecursiveIteratorIterator::SELF_FIRST |
55
|
|
|
)); |
56
|
|
|
|
57
|
282 |
|
return count($iterator) ? reset($iterator)->getFilename() : '2018_11_22_000000_create_audits_table.php'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Register the service provider. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
282 |
|
public function register() |
67
|
|
|
{ |
68
|
282 |
|
$this->commands([ |
69
|
282 |
|
AuditDriverMakeCommand::class, |
70
|
|
|
]); |
71
|
|
|
|
72
|
282 |
|
$this->app->singleton(Auditor::class, function ($app) { |
73
|
147 |
|
return new \OwenIt\Auditing\Auditor($app); |
74
|
282 |
|
}); |
75
|
282 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
3 |
|
public function provides() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
3 |
|
Auditor::class, |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: