Skip to content

Commit

Permalink
fix some error on ishell env
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 24, 2021
1 parent 05a4337 commit c92753c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/Controller/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function panelCommand(): void
'borderChar' => '='
]);

Panel::create([
Panel::new([
'data' => $data,
'title' => 'panel show',
'titleBorder' => '=',
Expand Down
32 changes: 24 additions & 8 deletions src/AbstractApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ public function run(bool $exit = true)
// do run ...
$command = $this->commandName;
$result = $this->dispatch($command, $this->flags->getRawArgs());

} catch (Throwable $e) {
$this->fire(ConsoleEvent::ON_RUN_ERROR, $e, $this);
$result = $e->getCode() === 0 ? $e->getLine() : $e->getCode();
Expand Down Expand Up @@ -355,7 +354,6 @@ public function stop(int $code = 0)
public function runWithArgs(array $args)
{
$this->input->setArgs($args);

return $this->run(false);
}

Expand Down Expand Up @@ -499,9 +497,8 @@ protected function handleGlobalCommand(string $command): bool
*/
protected function startInteractiveShell(): void
{
$in = $this->input;
// $in = $this->input;
$out = $this->output;

$out->title("Welcome interactive shell for run application", [
'titlePos' => Title::POS_MIDDLE,
]);
Expand Down Expand Up @@ -529,6 +526,12 @@ protected function startInteractiveShell(): void
'exit' => 1,
];

// set helper render
$this->flags->setHelpRenderer(function () {
$this->showHelpInfo();
// $this->stop(); not exit
});

while (true) {
$line = Interact::readln("<comment>$prefix ></comment> ");
if (strlen($line) < 5) {
Expand All @@ -548,13 +551,26 @@ protected function startInteractiveShell(): void
}

$args = LineParser::parseIt($line);
$this->debugf('input line: %s, parsed args: %s', $line, DataHelper::toString($args));
$this->debugf('ishell - input line: %s, split args: %s', $line, DataHelper::toString($args));

// reload and parse args
$in->parse($args);
$in->setFullScript($line);
$this->flags->resetResults();
// $this->flags->setTrustedOpt('debug');
$this->flags->parse($args);
// $in->parse($args);
// $in->setFullScript($line);

// fire event ON_BEFORE_RUN, if it is registered.
$this->fire(ConsoleEvent::ON_BEFORE_RUN, $this);
if (!$this->beforeRun()) {
continue;
}

// do run ...
$command = $this->commandName;
$this->dispatch($command, $this->flags->getRawArgs());

$this->run(false);
$this->debugf('ishell - the command "%s" run completed', $command);
$out->println('');
}

Expand Down
53 changes: 53 additions & 0 deletions src/Component/Formatter/JSONPretty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace Inhere\Console\Component\Formatter;

use Inhere\Console\Component\MessageFormatter;
use Toolkit\Stdlib\Str;
use function json_decode;

/**
* class JSONPretty
*/
class JSONPretty extends MessageFormatter
{
/**
* @var array|iterable
*/
public $data;

/**
* @param string $json
*
* @return static
*/
public static function newFromString(string $json): self
{
$self = new self();
$self->setData((array)json_decode($json, true));

return $self;
}

/**
* @return string
*/
public function format(): string
{
$buf = Str\StrBuffer::new();

foreach ($this->data as $key => $item) {
// TODO
}

return $buf->toString();
}

/**
* @param array|iterable $data
*/
public function setData($data): void
{
$this->data = $data;
}
}
2 changes: 1 addition & 1 deletion src/Component/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ abstract class MessageFormatter implements FormatterInterface
*
* @return MessageFormatter
*/
public static function create(array $config = []): self
public static function new(array $config = []): self
{
return new static($config);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ protected function annotationVars(): array
*/
protected function initFlagsParser(Input $input): void
{
// if on interactive shell environment(GlobalOption::ISHELL=true)
if ($this->flags->isLocked()) {
return;
}

$input->setFs($this->flags);
$this->flags->setDesc(self::getDesc());
$this->flags->setScriptName(self::getName());
Expand Down

0 comments on commit c92753c

Please sign in to comment.