Skip to content

Commit 6f97050

Browse files
committed
refactor: refacting the input output class.
1 parent e4a2695 commit 6f97050

12 files changed

+91
-162
lines changed

src/Contract/InputInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ public function getCommand(): string;
4040
* Whether the stream is an interactive terminal
4141
*/
4242
public function isInteractive(): bool;
43+
44+
/**
45+
* @return string
46+
*/
47+
public function toString(): string;
4348
}

src/IO/AbstractInput.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
namespace Inhere\Console\IO;
1111

1212
use Inhere\Console\Contract\InputInterface;
13+
use Toolkit\Cli\Helper\FlagHelper;
1314
use Toolkit\PFlag\FlagsParser;
1415
use Toolkit\PFlag\SFlags;
16+
use function array_map;
1517
use function array_shift;
1618
use function basename;
1719
use function getcwd;
1820
use function implode;
1921
use function is_int;
2022
use function is_string;
23+
use function preg_match;
2124
use function trim;
2225

2326
/**
@@ -111,7 +114,33 @@ public function __toString(): string
111114
/**
112115
* @return string
113116
*/
114-
abstract public function toString(): string;
117+
public function toString(): string
118+
{
119+
if (!$this->tokens) {
120+
return '';
121+
}
122+
123+
$tokens = array_map([$this, 'tokenEscape'], $this->tokens);
124+
return implode(' ', $tokens);
125+
}
126+
127+
/**
128+
* @param string $token
129+
*
130+
* @return string
131+
*/
132+
protected function tokenEscape(string $token): string
133+
{
134+
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
135+
return $match[1] . FlagHelper::escapeToken($match[2]);
136+
}
137+
138+
if ($token && $token[0] !== '-') {
139+
return FlagHelper::escapeToken($token);
140+
}
141+
142+
return $token;
143+
}
115144

116145
/**
117146
* @param array $rawFlags

src/IO/Input.php

+4-78
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99

1010
namespace Inhere\Console\IO;
1111

12+
use Inhere\Console\IO\Input\StreamInput;
1213
use Toolkit\Cli\Cli;
13-
use Toolkit\Cli\Flags;
14-
use Toolkit\Cli\Helper\FlagHelper;
15-
use Toolkit\FsUtil\File;
16-
use function array_map;
17-
use function fwrite;
18-
use function implode;
19-
use function preg_match;
2014

2115
/**
22-
* Class Input - The input information. by parse global var $argv.
16+
* Class Input - The std input.
2317
*
2418
* @package Inhere\Console\IO
2519
*/
26-
class Input extends AbstractInput
20+
class Input extends StreamInput
2721
{
2822
/**
2923
* The real command ID(group:command)
@@ -33,13 +27,6 @@ class Input extends AbstractInput
3327
*/
3428
protected $commandId = '';
3529

36-
/**
37-
* Default is STDIN
38-
*
39-
* @var resource
40-
*/
41-
protected $stream;
42-
4330
/**
4431
* Input constructor.
4532
*
@@ -51,63 +38,10 @@ public function __construct(array $args = null)
5138
$args = $_SERVER['argv'];
5239
}
5340

54-
$this->stream = Cli::getInputStream();
41+
parent::__construct(Cli::getInputStream());
5542
$this->collectInfo($args);
5643
}
5744

58-
/**
59-
* @return string
60-
*/
61-
public function toString(): string
62-
{
63-
$tokens = array_map([$this, 'tokenEscape'], $this->tokens);
64-
65-
return implode(' ', $tokens);
66-
}
67-
68-
/**
69-
* @param string $token
70-
*
71-
* @return string
72-
*/
73-
protected function tokenEscape(string $token): string
74-
{
75-
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
76-
return $match[1] . FlagHelper::escapeToken($match[2]);
77-
}
78-
79-
if ($token && $token[0] !== '-') {
80-
return FlagHelper::escapeToken($token);
81-
}
82-
83-
return $token;
84-
}
85-
86-
/**
87-
* @return string
88-
*/
89-
public function readAll(): string
90-
{
91-
return File::streamReadAll($this->stream);
92-
}
93-
94-
/**
95-
* Read input information
96-
*
97-
* @param string $question 若不为空,则先输出文本消息
98-
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
99-
*
100-
* @return string
101-
*/
102-
public function readln(string $question = '', bool $nl = false): string
103-
{
104-
if ($question) {
105-
fwrite($this->stream, $question . ($nl ? "\n" : ''));
106-
}
107-
108-
return File::streamFgets($this->stream);
109-
}
110-
11145
/**
11246
* @return resource
11347
*/
@@ -141,14 +75,6 @@ public function getFullCommand(): string
14175
return $this->scriptFile . ' ' . $this->getCommandPath();
14276
}
14377

144-
/**
145-
* @return resource
146-
*/
147-
public function getStream()
148-
{
149-
return $this->stream;
150-
}
151-
15278
/**
15379
* Get command ID e.g `http:start`
15480
*

src/IO/Input/AloneInput.php

-33
This file was deleted.

src/IO/Input/ArrayInput.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Inhere\Console\IO\Input;
1313
use Toolkit\Cli\Flags;
14+
use function is_int;
1415

1516
/**
1617
* Class ArrayInput
@@ -22,10 +23,19 @@ class ArrayInput extends Input
2223
/**
2324
* Input constructor.
2425
*
25-
* @param null|array $args
26+
* @param array $arr
2627
*/
27-
public function __construct(array $args = null)
28+
public function __construct(array $arr = [])
2829
{
30+
$args = [];
31+
foreach ($arr as $key => $val) {
32+
if (!is_int($key)) {
33+
$args[] = $key;
34+
}
35+
36+
$args[] = $val;
37+
}
38+
2939
parent::__construct($args);
3040
}
3141
}

src/IO/Input/StreamInput.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ public function __construct($stream = STDIN)
3939
$this->setStream($stream);
4040
}
4141

42-
public function toString(): string
43-
{
44-
return '';
45-
}
46-
4742
/**
4843
* @return string
4944
*/
@@ -98,7 +93,7 @@ protected function setStream($stream): void
9893
File::assertStream($stream);
9994

10095
$meta = stream_get_meta_data($stream);
101-
if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) {
96+
if (!str_contains($meta['mode'], 'r') && !str_contains($meta['mode'], '+')) {
10297
throw new InvalidArgumentException('Expected a readable stream');
10398
}
10499

src/IO/Input/StringInput.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Inhere\Console\IO\Input;
1111

1212
use Inhere\Console\IO\Input;
13-
use Toolkit\Cli\Flags;
1413
use Toolkit\Cli\Util\LineParser;
1514

1615
/**
@@ -28,6 +27,7 @@ class StringInput extends Input
2827
public function __construct(string $line)
2928
{
3029
$flags = LineParser::parseIt($line);
30+
3131
parent::__construct($flags);
3232
}
3333
}

0 commit comments

Comments
 (0)