Skip to content

Commit a8b2a6b

Browse files
committed
feat(commands): add install command
1 parent 001ea7e commit a8b2a6b

File tree

4 files changed

+91
-18
lines changed

4 files changed

+91
-18
lines changed

src/AuditingServiceProvider.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Illuminate\Contracts\Support\DeferrableProvider;
66
use Illuminate\Support\ServiceProvider;
7-
use OwenIt\Auditing\Console\AuditDriverMakeCommand;
7+
88
use OwenIt\Auditing\Contracts\Auditor;
9+
use OwenIt\Auditing\Console\AuditDriverCommand;
10+
use OwenIt\Auditing\Console\InstallCommand;
911

1012
class AuditingServiceProvider extends ServiceProvider implements DeferrableProvider
1113
{
@@ -16,19 +18,8 @@ class AuditingServiceProvider extends ServiceProvider implements DeferrableProvi
1618
*/
1719
public function boot()
1820
{
19-
$config = __DIR__.'/../config/audit.php';
20-
$migration = __DIR__.'/../database/migrations/audits.stub';
21-
22-
// Lumen lacks a config_path() helper, so we use base_path()
23-
$this->publishes([
24-
$config => base_path('config/audit.php'),
25-
], 'config');
26-
27-
$this->publishes([
28-
$migration => database_path(sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))),
29-
], 'migrations');
30-
31-
$this->mergeConfigFrom($config, 'audit');
21+
$this->registerPublishing();
22+
$this->mergeConfigFrom(__DIR__.'/../config/audit.php', 'audit');
3223
}
3324

3425
/**
@@ -39,14 +30,36 @@ public function boot()
3930
public function register()
4031
{
4132
$this->commands([
42-
AuditDriverMakeCommand::class,
33+
AuditDriverCommand::class,
34+
InstallCommand::class,
4335
]);
4436

4537
$this->app->singleton(Auditor::class, function ($app) {
4638
return new \OwenIt\Auditing\Auditor($app);
4739
});
4840
}
4941

42+
/**
43+
* Register the package's publishable resources.
44+
*
45+
* @return void
46+
*/
47+
private function registerPublishing()
48+
{
49+
if ($this->app->runningInConsole()) {
50+
// Lumen lacks a config_path() helper, so we use base_path()
51+
$this->publishes([
52+
__DIR__.'/../config/audit.php' => base_path('config/audit.php'),
53+
], 'config');
54+
55+
$this->publishes([
56+
__DIR__.'/../database/migrations/audits.stub' => database_path(
57+
sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
58+
),
59+
], 'migrations');
60+
}
61+
}
62+
5063
/**
5164
* {@inheritdoc}
5265
*/

src/Console/AuditDriverMakeCommand.php renamed to src/Console/AuditDriverCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Illuminate\Console\GeneratorCommand;
66

7-
class AuditDriverMakeCommand extends GeneratorCommand
7+
class AuditDriverCommand extends GeneratorCommand
88
{
99
/**
1010
* {@inheritdoc}
1111
*/
12-
protected $name = 'make:audit-driver';
12+
protected $name = 'auditing:audit-driver';
1313

1414
/**
1515
* {@inheritdoc}

src/Console/InstallCommand.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace OwenIt\Auditing\Console;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Console\DetectsApplicationNamespace;
8+
9+
class InstallCommand extends Command
10+
{
11+
use DetectsApplicationNamespace;
12+
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected $signature = 'auditing:install';
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected $description = 'Install all of the Auditing resources';
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function handle()
27+
{
28+
$this->comment('Publishing Auditing Configuration...');
29+
$this->callSilent('vendor:publish', ['--tag' => 'config']);
30+
31+
$this->comment('Publishing Auditing Migrations...');
32+
$this->callSilent('vendor:publish', ['--tag' => 'migrations']);
33+
34+
$this->registerAuditingServiceProvider();
35+
36+
$this->info('Auditing installed successfully.');
37+
}
38+
39+
/**
40+
* Register the Auditing service provider in the application configuration file.
41+
*
42+
* @return void
43+
*/
44+
protected function registerAuditingServiceProvider()
45+
{
46+
$namespace = str_replace_last('\\', '', $this->getAppNamespace());
47+
48+
$appConfig = file_get_contents(config_path('app.php'));
49+
50+
if (Str::contains($appConfig, 'OwenIt\\Auditing\\AuditingServiceProvider::class')) {
51+
return;
52+
}
53+
54+
file_put_contents(config_path('app.php'), str_replace(
55+
"{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL,
56+
"{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL." OwenIt\Auditing\AuditingServiceProvider::class,".PHP_EOL,
57+
$appConfig
58+
));
59+
}
60+
}

tests/Functional/CommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function itWillGenerateTheAuditDriver()
1818

1919
$this->assertInstanceOf(
2020
\Illuminate\Foundation\Testing\PendingCommand::class,
21-
$this->artisan('make:audit-driver', [
21+
$this->artisan('auditing:audit-driver', [
2222
'name' => 'TestDriver',
2323
]
2424
)

0 commit comments

Comments
 (0)