- WIP on CLI works
This commit is contained in:
parent
32d5079559
commit
6d771ed88f
|
@ -48,14 +48,15 @@ class CliMiddleware implements MiddlewareInterface
|
||||||
|
|
||||||
$cliRequest = new CliRequest($request);
|
$cliRequest = new CliRequest($request);
|
||||||
|
|
||||||
# $cliRequest->setOptions($commands->getOptions());
|
if ( $command = $this->commands->matchCommand($cliRequest->command) ) {
|
||||||
|
|
||||||
if ( $command = $this->commands->matchCommand($cliRequest->command, $cliRequest->options) ) {
|
|
||||||
if ($command->callback) {
|
if ($command->callback) {
|
||||||
if (is_string($command->callback)) {
|
if (is_string($command->callback)) {
|
||||||
list($class, $method) = explode('::', $command->callback);
|
list($class, $method) = explode('::', $command->callback);
|
||||||
|
|
||||||
return $this->container->make($class)->{$method}($request->withAttribute('cli.command', $command)->withAttribute('cli.request', $cliRequest), []);
|
$request = $request->withAttribute('cli.command', $command)
|
||||||
|
->withAttribute('cli.request', $cliRequest);
|
||||||
|
|
||||||
|
return $this->container->make($class)->{$method}($request, []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -80,11 +81,11 @@ class CliMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
$text = [
|
$text = [
|
||||||
$command->description, "",
|
$command->description, "",
|
||||||
"Usage:",
|
"#[color: rgb(145;130;195), format: bold]Usage:[#]",
|
||||||
" command [options] [arguments]", "",
|
" command [options] [arguments]", "",
|
||||||
"Options:",
|
"#[color: rgb(145;130;195), format: bold]Options:[#]",
|
||||||
" OPTIONS HERE", "",
|
" OPTIONS HERE", "",
|
||||||
"Available commands:",
|
"#[color: rgb(145;130;195), format: bold]Available commands:[#]",
|
||||||
" COMMANDS HERE", "",
|
" COMMANDS HERE", "",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -47,8 +47,6 @@ class CliRequest
|
||||||
$this->command[] = $cmd;
|
$this->command[] = $cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
dump($executionString);
|
|
||||||
|
|
||||||
$this->command = $executionString;
|
$this->command = $executionString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Mcnd\CLI;
|
namespace Mcnd\CLI;
|
||||||
|
|
||||||
#[\Attribute]
|
|
||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
|
|
@ -19,16 +19,27 @@ class CommandStack
|
||||||
$this->commands[] = $command;
|
$this->commands[] = $command;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function matchCommand(array $commands, array $options) : null|Command
|
/**
|
||||||
|
* Matches the closest command found from the stack
|
||||||
|
*
|
||||||
|
* @param array $commands
|
||||||
|
* @param array $options
|
||||||
|
* @return Command|null
|
||||||
|
*/
|
||||||
|
public function matchCommand(array $commands) : null|Command
|
||||||
{
|
{
|
||||||
|
while ($commands) {
|
||||||
$cmd = implode(' ', $commands);
|
$cmd = implode(' ', $commands);
|
||||||
|
|
||||||
foreach($this->commands as $command) {
|
foreach ($this->commands as $command) {
|
||||||
if ($command->name === $cmd) {
|
if ($command->name === $cmd) {
|
||||||
return $command;
|
return $command;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
array_pop($commands);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -53,7 +53,7 @@ class AsciiFormatter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
list($scope, $value) = explode(':', $formatting, 2);
|
list($scope, $value) = array_map('trim', explode(':', $formatting, 2));
|
||||||
|
|
||||||
switch($scope) {
|
switch($scope) {
|
||||||
case 'color':
|
case 'color':
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Mcnd\CLI;
|
namespace Mcnd\CLI;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS)]
|
|
||||||
class Option
|
class Option
|
||||||
{
|
{
|
||||||
public const ARGUMENT_NONE = 0;
|
public const ARGUMENT_NONE = 0;
|
||||||
|
|
Loading…
Reference in New Issue