Skip to content

Commit

Permalink
feat(commands): add ClearCommand
Browse files Browse the repository at this point in the history
- Add a new command `ClearCommand` to clear the Soar log file
- The command is executed with the signature `soar:clear`
- The log file is determined based on the configuration or the default path
- Display success message after clearing the log file
  • Loading branch information
guanguans committed Jul 16, 2023
1 parent 90f55dd commit 8b772d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/laravel-soar.
*
* (c) guanguans <ityaozm@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelSoar\Commands;

use Guanguans\LaravelSoar\Soar;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class ClearCommand extends Command
{
protected $signature = 'soar:clear';

protected $description = 'Clear the Soar log file';

public function handle(Soar $soar): void
{
$this->info('Clearing Soar log file...');

$logFile = config('soar.options.-log-output') ?: \dirname($soar->getSoarPath()).'/soar.log';
File::delete($logFile);

$this->info("The Soar log file($logFile) has been cleared.");
}
}
14 changes: 13 additions & 1 deletion src/SoarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Guanguans\LaravelSoar;

use Guanguans\LaravelSoar\Commands\ClearCommand;
use Guanguans\LaravelSoar\Macros\QueryBuilderMacro;
use Guanguans\LaravelSoar\Outputs\ClockworkOutput;
use Guanguans\LaravelSoar\Outputs\ConsoleOutput;
Expand Down Expand Up @@ -58,14 +59,16 @@ public function register(): void
$this->registerMacros();
$this->registerSoar();
$this->registerOutputManager();
$this->loadRoutes();
}

/**
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function boot(): void
{
$this->registerCommands();
$this->loadRoutes();

if (config('soar.enabled', false)) {
$this->app->make(Bootstrapper::class)->boot();
}
Expand Down Expand Up @@ -153,6 +156,15 @@ protected function loadRoutes(): void
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
}

protected function registerCommands(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
ClearCommand::class,
]);
}
}

/**
* @param class-string $class
*/
Expand Down

0 comments on commit 8b772d7

Please sign in to comment.