Skip to content

Commit

Permalink
refactor: load config using Util::loadCommandFromConfig()
Browse files Browse the repository at this point in the history
- load command config now wrap in array (no need array merge), so this util use for flatten wrapper array
  • Loading branch information
SonyPradana committed Jun 4, 2024
1 parent 67c2e6e commit 75b6370
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
6 changes: 1 addition & 5 deletions src/System/Integrate/Console/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use System\Console\Style\Style;
use System\Console\Traits\PrintHelpTrait;
use System\Integrate\Application;
use System\Integrate\ConfigRepository;
use System\Integrate\ValueObjects\CommandMap;
use System\Text\Str;

Expand Down Expand Up @@ -261,9 +260,6 @@ public function commandHelp(): int
*/
private function commandMaps()
{
return array_map(
static fn ($command): CommandMap => new CommandMap($command),
Application::getIntance()[ConfigRepository::class]->get('commands', [])
);
return Util::loadCommandFromConfig(Application::getIntance());
}
}
11 changes: 3 additions & 8 deletions src/System/Integrate/Console/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace System\Integrate\Console;

use DI\Container;
use System\Console\Style\Style;
use System\Integrate\Application;
use System\Integrate\Bootstrap\BootProviders;
use System\Integrate\Bootstrap\ConfigProviders;
use System\Integrate\Bootstrap\RegisterProviders;
use System\Integrate\ConfigRepository;
use System\Integrate\ValueObjects\CommandMap;

class Karnel
Expand Down Expand Up @@ -168,13 +166,10 @@ public function exit_code()
/**
* Command route.
*
* @return \System\Integrate\ValueObjects\CommandMap[]
* @return CommandMap[]
*/
protected function commands()
protected function commands(): array
{
return array_map(
static fn ($command): CommandMap => new CommandMap($command),
$this->app[ConfigRepository::class]->get('commands', [])
);
return Util::loadCommandFromConfig($this->app);
}
}
29 changes: 29 additions & 0 deletions src/System/Integrate/Console/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Console;

use System\Integrate\Application;
use System\Integrate\ConfigRepository;
use System\Integrate\ValueObjects\CommandMap;

final class Util
{
/**
* Convert command from array to CommandMap.
*
* @return CommandMap[]
*/
public static function loadCommandFromConfig(Application $app): array
{
$command_map = [];
foreach ($app[ConfigRepository::class]->get('commands', []) as $commands) {
foreach ($commands as $command) {
$command_map[] = new CommandMap($command);
}
}

return $command_map;
}
}
2 changes: 1 addition & 1 deletion tests/Integrate/Commands/HelpCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void
{
parent::setUp();
$this->app->set('config', fn () => new ConfigRepository([
'commands' => $this->command,
'commands' => [$this->command],
]));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Integrate/Console/KarnelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function itCanCallCommand()

class NormalCommand extends Karnel
{
protected function commands()
protected function commands(): array
{
return [
// olr style
Expand Down

0 comments on commit 75b6370

Please sign in to comment.