99
1010namespace Inhere \Console \IO ;
1111
12+ use Inhere \Console \IO \Input \StreamInput ;
1213use 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 *
0 commit comments