Skip to content

Commit

Permalink
feat(commands): add install command
Browse files Browse the repository at this point in the history
  • Loading branch information
anteriovieira committed Mar 7, 2019
1 parent 001ea7e commit a8b2a6b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
43 changes: 28 additions & 15 deletions src/AuditingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use OwenIt\Auditing\Console\AuditDriverMakeCommand;

use OwenIt\Auditing\Contracts\Auditor;
use OwenIt\Auditing\Console\AuditDriverCommand;
use OwenIt\Auditing\Console\InstallCommand;

class AuditingServiceProvider extends ServiceProvider implements DeferrableProvider
{
Expand All @@ -16,19 +18,8 @@ class AuditingServiceProvider extends ServiceProvider implements DeferrableProvi
*/
public function boot()
{
$config = __DIR__.'/../config/audit.php';
$migration = __DIR__.'/../database/migrations/audits.stub';

// Lumen lacks a config_path() helper, so we use base_path()
$this->publishes([
$config => base_path('config/audit.php'),
], 'config');

$this->publishes([
$migration => database_path(sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))),
], 'migrations');

$this->mergeConfigFrom($config, 'audit');
$this->registerPublishing();
$this->mergeConfigFrom(__DIR__.'/../config/audit.php', 'audit');
}

/**
Expand All @@ -39,14 +30,36 @@ public function boot()
public function register()
{
$this->commands([
AuditDriverMakeCommand::class,
AuditDriverCommand::class,
InstallCommand::class,
]);

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

/**
* Register the package's publishable resources.
*
* @return void
*/
private function registerPublishing()
{
if ($this->app->runningInConsole()) {
// Lumen lacks a config_path() helper, so we use base_path()
$this->publishes([
__DIR__.'/../config/audit.php' => base_path('config/audit.php'),
], 'config');

$this->publishes([
__DIR__.'/../database/migrations/audits.stub' => database_path(
sprintf('migrations/%s_create_audits_table.php', date('Y_m_d_His'))
),
], 'migrations');
}
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use Illuminate\Console\GeneratorCommand;

class AuditDriverMakeCommand extends GeneratorCommand
class AuditDriverCommand extends GeneratorCommand
{
/**
* {@inheritdoc}
*/
protected $name = 'make:audit-driver';
protected $name = 'auditing:audit-driver';

/**
* {@inheritdoc}
Expand Down
60 changes: 60 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace OwenIt\Auditing\Console;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;

class InstallCommand extends Command
{
use DetectsApplicationNamespace;

/**
* {@inheritdoc}
*/
protected $signature = 'auditing:install';

/**
* {@inheritdoc}
*/
protected $description = 'Install all of the Auditing resources';

/**
* {@inheritdoc}
*/
public function handle()
{
$this->comment('Publishing Auditing Configuration...');
$this->callSilent('vendor:publish', ['--tag' => 'config']);

$this->comment('Publishing Auditing Migrations...');
$this->callSilent('vendor:publish', ['--tag' => 'migrations']);

$this->registerAuditingServiceProvider();

$this->info('Auditing installed successfully.');
}

/**
* Register the Auditing service provider in the application configuration file.
*
* @return void
*/
protected function registerAuditingServiceProvider()
{
$namespace = str_replace_last('\\', '', $this->getAppNamespace());

$appConfig = file_get_contents(config_path('app.php'));

if (Str::contains($appConfig, 'OwenIt\\Auditing\\AuditingServiceProvider::class')) {
return;
}

file_put_contents(config_path('app.php'), str_replace(
"{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL,
"{$namespace}\\Providers\EventServiceProvider::class,".PHP_EOL." OwenIt\Auditing\AuditingServiceProvider::class,".PHP_EOL,
$appConfig
));
}
}
2 changes: 1 addition & 1 deletion tests/Functional/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function itWillGenerateTheAuditDriver()

$this->assertInstanceOf(
\Illuminate\Foundation\Testing\PendingCommand::class,
$this->artisan('make:audit-driver', [
$this->artisan('auditing:audit-driver', [
'name' => 'TestDriver',
]
)
Expand Down

0 comments on commit a8b2a6b

Please sign in to comment.