Skip to content

Commit 2fed8f3

Browse files
committed
refactor input output logic
1 parent 3001e1d commit 2fed8f3

14 files changed

+420
-149
lines changed

src/Concern/InputOutputAwareTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace Inhere\Console\Concern;
1010

11-
use Inhere\Console\Console;
1211
use Inhere\Console\IO\Input;
1312
use Inhere\Console\Contract\InputInterface;
1413
use Inhere\Console\IO\Output;
1514
use Inhere\Console\Contract\OutputInterface;
15+
use Toolkit\PFlag\SFlags;
1616

1717
/**
1818
* Class InputOutputAwareTrait
@@ -21,6 +21,11 @@
2121
*/
2222
trait InputOutputAwareTrait
2323
{
24+
/**
25+
* @var SFlags
26+
*/
27+
// protected $flags;
28+
2429
/**
2530
* @var Input|InputInterface
2631
*/
@@ -140,9 +145,9 @@ public function getRequiredOpt(string $name, string $errMsg = '')
140145
*
141146
* @return string
142147
*/
143-
public function read(string $question = '', bool $nl = false): string
148+
public function readln(string $question = '', bool $nl = false): string
144149
{
145-
return $this->input->read($question, $nl);
150+
return $this->input->readln($question, $nl);
146151
}
147152

148153
/**
@@ -152,7 +157,7 @@ public function read(string $question = '', bool $nl = false): string
152157
*
153158
* @return int
154159
*/
155-
public function write($message, $nl = true, $quit = false): int
160+
public function write($message, bool $nl = true, $quit = false): int
156161
{
157162
return $this->output->write($message, $nl, $quit);
158163
}

src/Contract/InputInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ interface InputInterface
3838
*
3939
* @return string
4040
*/
41-
public function read(string $question = '', bool $nl = false): string;
41+
public function readln(string $question = '', bool $nl = false): string;
4242

4343
/**
4444
* @return string
@@ -77,4 +77,9 @@ public function getOpts(): array;
7777
* @return bool|mixed|null
7878
*/
7979
public function getOpt(string $name, $default = null);
80+
81+
/**
82+
* Whether the stream is an interactive terminal
83+
*/
84+
public function isInteractive() : bool;
8085
}

src/Contract/OutputInterface.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@
1616
interface OutputInterface
1717
{
1818
/**
19-
* Write a message to standard output stream.
19+
* Write a message to output
2020
*
21-
* @param mixed $messages Output message
22-
* @param bool $nl Output with newline
23-
* @param int|boolean $quit If is int, setting it is exit code.
21+
* @param string $content
2422
*
2523
* @return int
2624
*/
27-
public function write($messages, $nl = true, $quit = false): int;
25+
public function write(string $content): int;
26+
27+
/**
28+
* Write a message to output with newline
29+
*
30+
* @param string $content
31+
*
32+
* @return int
33+
*/
34+
public function writeln(string $content): int;
35+
36+
/**
37+
* Whether the stream is an interactive terminal
38+
*/
39+
public function isInteractive() : bool;
2840
}

src/IO/AbstractInput.php

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Inhere\Console\Concern\InputArgumentsTrait;
1212
use Inhere\Console\Concern\InputOptionsTrait;
1313
use Inhere\Console\Contract\InputInterface;
14+
use Toolkit\PFlag\AbstractFlags;
15+
use Toolkit\PFlag\SFlags;
1416
use function getcwd;
1517
use function is_int;
1618
use function trim;
@@ -24,6 +26,20 @@ abstract class AbstractInput implements InputInterface
2426
{
2527
use InputArgumentsTrait, InputOptionsTrait;
2628

29+
/**
30+
* Global flags parser
31+
*
32+
* @var AbstractFlags|SFlags
33+
*/
34+
protected $gfs;
35+
36+
/**
37+
* Command flags parser
38+
*
39+
* @var AbstractFlags|SFlags
40+
*/
41+
protected $fs;
42+
2743
/**
2844
* @var string
2945
*/
@@ -69,7 +85,8 @@ abstract class AbstractInput implements InputInterface
6985
protected $fullScript;
7086

7187
/**
72-
* Raw argv data.
88+
* Raw input argv data.
89+
* - first element is script file
7390
*
7491
* @var array
7592
*/
@@ -137,6 +154,14 @@ public function getCommandPath(): string
137154
return $path;
138155
}
139156

157+
/**
158+
* @return bool
159+
*/
160+
public function isInteractive(): bool
161+
{
162+
return false;
163+
}
164+
140165
/***********************************************************************************
141166
* getter/setter
142167
***********************************************************************************/
@@ -261,14 +286,6 @@ public function getRawFlags(): array
261286
return $this->tokens;
262287
}
263288

264-
/**
265-
* @return array
266-
*/
267-
public function getRawArgs(): array
268-
{
269-
return $this->tokens;
270-
}
271-
272289
/**
273290
* @return array
274291
*/
@@ -300,4 +317,36 @@ public function setSubCommand(string $subCommand): void
300317
{
301318
$this->subCommand = $subCommand;
302319
}
320+
321+
/**
322+
* @return AbstractFlags
323+
*/
324+
public function getGfs(): AbstractFlags
325+
{
326+
return $this->gfs;
327+
}
328+
329+
/**
330+
* @param AbstractFlags $gfs
331+
*/
332+
public function setGfs(AbstractFlags $gfs): void
333+
{
334+
$this->gfs = $gfs;
335+
}
336+
337+
/**
338+
* @return AbstractFlags
339+
*/
340+
public function getFs(): AbstractFlags
341+
{
342+
return $this->fs;
343+
}
344+
345+
/**
346+
* @param AbstractFlags $fs
347+
*/
348+
public function setFs(AbstractFlags $fs): void
349+
{
350+
$this->fs = $fs;
351+
}
303352
}

src/IO/AbstractOutput.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
4+
namespace Inhere\Console\IO;
5+
6+
use Inhere\Console\Contract\OutputInterface;
7+
8+
/**
9+
* Class AbstractOutput
10+
* @package Inhere\Console\IO
11+
*/
12+
abstract class AbstractOutput implements OutputInterface
13+
{
14+
/**
15+
* @return bool
16+
*/
17+
public function isInteractive(): bool
18+
{
19+
return false;
20+
}
21+
}

src/IO/Input.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Toolkit\Cli\Cli;
1212
use Toolkit\Cli\Flags;
1313
use Toolkit\Cli\Helper\FlagHelper;
14+
use Toolkit\FsUtil\File;
1415
use function array_map;
1516
use function array_shift;
1617
use function basename;
@@ -64,29 +65,29 @@ public function __construct(array $args = null, bool $parsing = true)
6465
}
6566

6667
/**
67-
* @param array $args
68+
* @param array $rawFlags
6869
*/
69-
protected function collectInfo(array $args): void
70+
protected function collectInfo(array $rawFlags): void
7071
{
7172
$this->getPwd();
72-
if (!$args) {
73+
if (!$rawFlags) {
7374
return;
7475
}
7576

76-
$this->tokens = $args;
77+
$this->tokens = $rawFlags;
7778

7879
// first is bin file
79-
if (isset($args[0]) && is_string($args[0])) {
80-
$this->script = array_shift($args);
80+
if (isset($rawFlags[0]) && is_string($rawFlags[0])) {
81+
$this->script = array_shift($rawFlags);
8182

8283
// bin name
8384
$this->scriptName = basename($this->script);
8485
}
8586

86-
$this->flags = $args; // no script
87+
$this->flags = $rawFlags; // no script
8788

8889
// full script
89-
$this->fullScript = implode(' ', $args);
90+
$this->fullScript = implode(' ', $rawFlags);
9091
}
9192

9293
/**
@@ -155,13 +156,13 @@ protected function tokenEscape(string $token): string
155156
*
156157
* @return string
157158
*/
158-
public function read(string $question = '', bool $nl = false): string
159+
public function readln(string $question = '', bool $nl = false): string
159160
{
160161
if ($question) {
161-
fwrite(Cli::getOutputStream(), $question . ($nl ? "\n" : ''));
162+
fwrite($this->inputStream, $question . ($nl ? "\n" : ''));
162163
}
163164

164-
return trim((string)fgets($this->inputStream));
165+
return File::streamFgets($this->inputStream);
165166
}
166167

167168
/***********************************************************************************

0 commit comments

Comments
 (0)