Skip to content

Commit

Permalink
feat: support add CmdHandler class to CliApp
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 9, 2021
1 parent 9bf54ac commit f085b16
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Create an multi commands application, run subcommand. see example file [example/
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlagTest\Cases\DemoCmdHandler;

$app = new CliApp();

Expand Down Expand Up @@ -322,6 +323,8 @@ $app->add('test2', function (FlagsParser $fs) {
// fn - required php 7.4+
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));

$app->addHandler(DemoCmdHandler::class);

$app->run();
```

Expand Down
3 changes: 3 additions & 0 deletions example/cliapp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Toolkit\Cli\Cli;
use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlagTest\Cases\DemoCmdHandler;

require dirname(__DIR__) . '/test/bootstrap.php';

Expand Down Expand Up @@ -37,5 +38,7 @@

$app->add('show-err', fn() => throw new RuntimeException('test show exception'));

$app->addHandler(DemoCmdHandler::class);

$app->run();

30 changes: 28 additions & 2 deletions src/CliApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
use Throwable;
use Toolkit\Cli\Cli;
use Toolkit\Cli\Color;
use Toolkit\PFlag\Contract\CmdHandlerInterface;
use Toolkit\Stdlib\Arr;
use Toolkit\Stdlib\Helper\Assert;
use Toolkit\Stdlib\Helper\Valid;
use Toolkit\Stdlib\Obj\Traits\AutoConfigTrait;
use function array_merge;
use function array_shift;
Expand Down Expand Up @@ -61,12 +64,14 @@ class CliApp
/**
* @var FlagsParser
*/
protected FlagsParser|SFlags $flags;
protected FlagsParser $flags;

/**
* Flags for current run command
*
* @var FlagsParser|null
*/
protected ?FlagsParser $cmdFlags;
protected ?FlagsParser $cmdFlags = null;

/**
* @var string
Expand Down Expand Up @@ -358,6 +363,27 @@ public function add(string $command, callable $handler, array $config = []): voi
$this->addCommand($command, $handler, $config);
}

/**
* @param class-string|CmdHandlerInterface $handler
*/
public function addHandler(string|CmdHandlerInterface $handler): void
{
if (is_string($handler)) {
// class string.
if (class_exists($handler)) {
$handler = new $handler;
Assert::isTrue($handler instanceof CmdHandlerInterface, 'must be an class and instance of CmdHandlerInterface');
} else {
throw new InvalidArgumentException('must be an class string');
}
}

$config = $handler->metadata();
$command = Valid::arrayHasNoEmptyKey($config, 'name');

$this->addCommand($command, $handler, $config);
}

/**
* @param string $command
* @param callable|object|class-string $handler
Expand Down
5 changes: 2 additions & 3 deletions src/CliCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Toolkit\PFlag;

use InvalidArgumentException;
use RuntimeException;
use Throwable;
use Toolkit\Stdlib\Obj\Traits\AutoConfigTrait;
Expand All @@ -27,7 +26,7 @@ class CliCmd
/**
* @var FlagsParser
*/
private FlagsParser|SFlags $flags;
private FlagsParser $flags;

/**
* @var callable(FlagsParser): mixed
Expand Down Expand Up @@ -102,7 +101,7 @@ protected function prepare(FlagsParser $fs): void
}

/**
* @return int|mixed
* @return mixed
*/
public function run(): mixed
{
Expand Down
34 changes: 34 additions & 0 deletions src/Contract/CmdHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace Toolkit\PFlag\Contract;

use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\FlagsParser;

/**
* interface CmdHandlerInterface
*
* @author inhere
*/
interface CmdHandlerInterface
{
/**
* @return array{name:string, desc: string, example:string, help: string}
*/
public function metadata(): array;

/**
* @param FlagsParser $fs
*
* @return void
*/
public function configure(FlagsParser $fs): void;

/**
* @param FlagsParser $fs
* @param CliApp $app
*
* @return mixed
*/
public function execute(FlagsParser $fs, CliApp $app): mixed;
}
51 changes: 51 additions & 0 deletions test/Cases/DemoCmdHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Toolkit\PFlagTest\Cases;

use Toolkit\PFlag\CliApp;
use Toolkit\PFlag\Contract\CmdHandlerInterface;
use Toolkit\PFlag\FlagsParser;
use function vdump;

/**
* class DemoCmdHandler
*
* @author inhere
*/
class DemoCmdHandler implements CmdHandlerInterface
{
/**
* @return array{name:string, desc: string, example:string, help: string}
*/
public function metadata(): array
{
return [
'name' => 'demo',
'desc' => 'desc for demo command handler',
];
}

/**
* @param FlagsParser $fs
*
* @return void
*/
public function configure(FlagsParser $fs): void
{
$fs->addOptsByRules([
'opt1' => 'string;a string opt1 for command test2, and is required;true',
'opt2' => 'int;a int opt2 for command test2',
]);
}

/**
* @param FlagsParser $fs
* @param CliApp $app
*
* @return mixed
*/
public function execute(FlagsParser $fs, CliApp $app): mixed
{
vdump(__METHOD__);
}
}
2 changes: 1 addition & 1 deletion test/RuleParser.php → test/Cases/RuleParser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Toolkit\PFlagTest;
namespace Toolkit\PFlagTest\Cases;

use Toolkit\PFlag\Concern\RuleParserTrait;

Expand Down
9 changes: 5 additions & 4 deletions test/CliAppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Toolkit\PFlagTest;

use Toolkit\PFlag\CliApp;
use Toolkit\PFlagTest\Cases\DemoCmdHandler;
use Toolkit\Stdlib\Obj\DataObject;

/**
Expand All @@ -17,10 +18,7 @@ private function initApp(CliApp $app): DataObject
$buf = DataObject::new();

// php 7.4+
// $app->add('test1', fn() => $buf->set('key', 'in test1'));
$app->add('test1', function () use ($buf) {
$buf->set('key', 'in test1');
});
$app->add('test1', fn() => $buf->set('key', 'in test1'));

$app->addCommands([
'test2' => [
Expand All @@ -35,6 +33,8 @@ private function initApp(CliApp $app): DataObject
],
]);

$app->addHandler(DemoCmdHandler::class);

return $buf;
}

Expand All @@ -52,6 +52,7 @@ public function testCliApp_basic(): void

$this->assertTrue($app->hasCommand('test1'));
$this->assertTrue($app->hasCommand('test2'));
$this->assertTrue($app->hasCommand('demo'));

$app->runByArgs(['test1']);
$this->assertEquals('in test1', $buf->get('key'));
Expand Down
3 changes: 1 addition & 2 deletions test/Concern/RuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Toolkit\PFlagTest\Concern;

use Toolkit\PFlagTest\BaseFlagsTestCase;
use Toolkit\PFlagTest\RuleParser;
use function vdump;
use Toolkit\PFlagTest\Cases\RuleParser;

/**
* class RuleParserTest
Expand Down
2 changes: 0 additions & 2 deletions test/FlagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Toolkit\PFlag\Flags;
use Toolkit\PFlag\Flag\Option;
use Toolkit\PFlag\FlagType;
use function edump;
use function vdump;

/**
* Class FlagsTest
Expand Down

0 comments on commit f085b16

Please sign in to comment.