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