Skip to content

Commit

Permalink
feat(commands): Add RunCommand
Browse files Browse the repository at this point in the history
- Added new RunCommand class
- Run Soar with the given options
- Handle the execution of Soar and display the results
  • Loading branch information
guanguans committed Jun 7, 2024
1 parent a354fc2 commit f062a45
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 51 deletions.
73 changes: 73 additions & 0 deletions src/Commands/Concerns/WithSoarOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/** @noinspection MethodVisibilityInspection */

declare(strict_types=1);

/**
* This file is part of the guanguans/laravel-soar.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelSoar\Commands\Concerns;

use Guanguans\LaravelSoar\Soar;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

/**
* @mixin \Illuminate\Console\Command
*/
trait WithSoarOptions
{
protected function configure(): void
{
$this->setDefinition($this->definition());
}

/**
* @return array<\Symfony\Component\Console\Input\InputArgument|\Symfony\Component\Console\Input\InputOption>
*/
protected function definition(): array
{
return [
new InputOption(
'option',
'o',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'The option to be passed to Soar(e.g. `--option=-report-type=markdown` or `--option report-type=markdown` or `-o report-type=markdown`)',
),
];
}

protected function debugSoar(): Soar
{
$soar = $this->soar();

if ($this->option('verbose')) {
$soar->dump();
$this->newLine();
}

return $soar;
}

protected function soar(): Soar
{
return app(Soar::class)->mergeOptions($this->normalizedSoarOptions());
}

protected function normalizedSoarOptions(): array
{
return collect($this->option('option'))
->mapWithKeys(static function (string $option): array {
[$key, $value] = Str::of($option)->explode('=', 2)->pad(2, null)->all();

return [Str::start($key, '-') => $value];
})
->all();
}
}
32 changes: 32 additions & 0 deletions src/Commands/RunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/laravel-soar.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\LaravelSoar\Commands;

use Guanguans\LaravelSoar\Commands\Concerns\WithSoarOptions;
use Illuminate\Console\Command;

class RunCommand extends Command
{
use WithSoarOptions;
protected $signature = 'soar:run';

protected $description = 'Run Soar with the given options';

/**
* @throws \Guanguans\SoarPHP\Exceptions\InvalidOptionException
*/
public function handle(): void
{
$this->info($this->debugSoar()->run());
}
}
56 changes: 5 additions & 51 deletions src/Commands/ScoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@

namespace Guanguans\LaravelSoar\Commands;

use Guanguans\LaravelSoar\Soar;
use Guanguans\LaravelSoar\Commands\Concerns\WithSoarOptions;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;

class ScoreCommand extends Command
{
use WithSoarOptions;
protected $signature = 'soar:score';

protected $description = 'Get the scores of the given SQL statements';
protected $description = 'Get the Soar scores of the given SQL statements';

/**
* @noinspection MethodShouldBeFinalInspection
Expand All @@ -36,57 +35,12 @@ public function handle(): void

for (;;) {
$query = $query ?: $this->ask('Please input the SQL statements');

if ($query) {
break;
}
}

$this->info(tap($soar, $this->soarTapper())->scores($query));
}

protected function configure(): void
{
$this->setDefinition($this->definition());
}

/**
* @return array<\Symfony\Component\Console\Input\InputArgument|\Symfony\Component\Console\Input\InputOption>
*/
protected function definition(): array
{
return [
new InputOption(
'option',
'o',
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'The option to be passed to Soar(e.g. --option=-report-type=markdown)',
),
];
}

protected function soar(): Soar
{
return app(Soar::class)->mergeOptions($this->normalizedSoarOptions());
}

protected function soarTapper(): \Closure
{
return function (Soar $soar): void {
if ($this->option('verbose')) {
$soar->dump();
$this->newLine();
}
};
}

protected function normalizedSoarOptions(): array
{
return collect($this->option('option'))
->mapWithKeys(static function (string $option): array {
[$key, $value] = Str::of($option)->explode('=', 2)->pad(2, null)->all();

return [Str::start($key, '-') => $value];
})
->all();
$this->info($this->debugSoar()->scores($query));
}
}
2 changes: 2 additions & 0 deletions src/SoarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Guanguans\LaravelSoar;

use Guanguans\LaravelSoar\Commands\ClearCommand;
use Guanguans\LaravelSoar\Commands\RunCommand;
use Guanguans\LaravelSoar\Commands\ScoreCommand;
use Guanguans\LaravelSoar\Macros\QueryBuilderMacro;
use Guanguans\LaravelSoar\Outputs\ClockworkOutput;
Expand Down Expand Up @@ -152,6 +153,7 @@ protected function registerCommands(): void
if ($this->app->runningInConsole()) {
$this->commands([
ClearCommand::class,
RunCommand::class,
ScoreCommand::class,
]);
}
Expand Down

0 comments on commit f062a45

Please sign in to comment.