Skip to content

Commit c92753c

Browse files
committed
fix some error on ishell env
1 parent 05a4337 commit c92753c

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

examples/Controller/ShowController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function panelCommand(): void
114114
'borderChar' => '='
115115
]);
116116

117-
Panel::create([
117+
Panel::new([
118118
'data' => $data,
119119
'title' => 'panel show',
120120
'titleBorder' => '=',

src/AbstractApplication.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ public function run(bool $exit = true)
302302
// do run ...
303303
$command = $this->commandName;
304304
$result = $this->dispatch($command, $this->flags->getRawArgs());
305-
306305
} catch (Throwable $e) {
307306
$this->fire(ConsoleEvent::ON_RUN_ERROR, $e, $this);
308307
$result = $e->getCode() === 0 ? $e->getLine() : $e->getCode();
@@ -355,7 +354,6 @@ public function stop(int $code = 0)
355354
public function runWithArgs(array $args)
356355
{
357356
$this->input->setArgs($args);
358-
359357
return $this->run(false);
360358
}
361359

@@ -499,9 +497,8 @@ protected function handleGlobalCommand(string $command): bool
499497
*/
500498
protected function startInteractiveShell(): void
501499
{
502-
$in = $this->input;
500+
// $in = $this->input;
503501
$out = $this->output;
504-
505502
$out->title("Welcome interactive shell for run application", [
506503
'titlePos' => Title::POS_MIDDLE,
507504
]);
@@ -529,6 +526,12 @@ protected function startInteractiveShell(): void
529526
'exit' => 1,
530527
];
531528

529+
// set helper render
530+
$this->flags->setHelpRenderer(function () {
531+
$this->showHelpInfo();
532+
// $this->stop(); not exit
533+
});
534+
532535
while (true) {
533536
$line = Interact::readln("<comment>$prefix ></comment> ");
534537
if (strlen($line) < 5) {
@@ -548,13 +551,26 @@ protected function startInteractiveShell(): void
548551
}
549552

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

553556
// reload and parse args
554-
$in->parse($args);
555-
$in->setFullScript($line);
557+
$this->flags->resetResults();
558+
// $this->flags->setTrustedOpt('debug');
559+
$this->flags->parse($args);
560+
// $in->parse($args);
561+
// $in->setFullScript($line);
562+
563+
// fire event ON_BEFORE_RUN, if it is registered.
564+
$this->fire(ConsoleEvent::ON_BEFORE_RUN, $this);
565+
if (!$this->beforeRun()) {
566+
continue;
567+
}
568+
569+
// do run ...
570+
$command = $this->commandName;
571+
$this->dispatch($command, $this->flags->getRawArgs());
556572

557-
$this->run(false);
573+
$this->debugf('ishell - the command "%s" run completed', $command);
558574
$out->println('');
559575
}
560576

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Component\Formatter;
4+
5+
use Inhere\Console\Component\MessageFormatter;
6+
use Toolkit\Stdlib\Str;
7+
use function json_decode;
8+
9+
/**
10+
* class JSONPretty
11+
*/
12+
class JSONPretty extends MessageFormatter
13+
{
14+
/**
15+
* @var array|iterable
16+
*/
17+
public $data;
18+
19+
/**
20+
* @param string $json
21+
*
22+
* @return static
23+
*/
24+
public static function newFromString(string $json): self
25+
{
26+
$self = new self();
27+
$self->setData((array)json_decode($json, true));
28+
29+
return $self;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function format(): string
36+
{
37+
$buf = Str\StrBuffer::new();
38+
39+
foreach ($this->data as $key => $item) {
40+
// TODO
41+
}
42+
43+
return $buf->toString();
44+
}
45+
46+
/**
47+
* @param array|iterable $data
48+
*/
49+
public function setData($data): void
50+
{
51+
$this->data = $data;
52+
}
53+
}

src/Component/MessageFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ abstract class MessageFormatter implements FormatterInterface
4242
*
4343
* @return MessageFormatter
4444
*/
45-
public static function create(array $config = []): self
45+
public static function new(array $config = []): self
4646
{
4747
return new static($config);
4848
}

src/Handler/AbstractHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ protected function annotationVars(): array
245245
*/
246246
protected function initFlagsParser(Input $input): void
247247
{
248+
// if on interactive shell environment(GlobalOption::ISHELL=true)
249+
if ($this->flags->isLocked()) {
250+
return;
251+
}
252+
248253
$input->setFs($this->flags);
249254
$this->flags->setDesc(self::getDesc());
250255
$this->flags->setScriptName(self::getName());

0 commit comments

Comments
 (0)