diff --git a/src/Contract/InputInterface.php b/src/Contract/InputInterface.php index 099a328..1f24a44 100644 --- a/src/Contract/InputInterface.php +++ b/src/Contract/InputInterface.php @@ -40,4 +40,9 @@ public function getCommand(): string; * Whether the stream is an interactive terminal */ public function isInteractive(): bool; + + /** + * @return string + */ + public function toString(): string; } diff --git a/src/IO/AbstractInput.php b/src/IO/AbstractInput.php index a526c43..df1f421 100644 --- a/src/IO/AbstractInput.php +++ b/src/IO/AbstractInput.php @@ -10,14 +10,17 @@ namespace Inhere\Console\IO; use Inhere\Console\Contract\InputInterface; +use Toolkit\Cli\Helper\FlagHelper; use Toolkit\PFlag\FlagsParser; use Toolkit\PFlag\SFlags; +use function array_map; use function array_shift; use function basename; use function getcwd; use function implode; use function is_int; use function is_string; +use function preg_match; use function trim; /** @@ -111,7 +114,33 @@ public function __toString(): string /** * @return string */ - abstract public function toString(): string; + public function toString(): string + { + if (!$this->tokens) { + return ''; + } + + $tokens = array_map([$this, 'tokenEscape'], $this->tokens); + return implode(' ', $tokens); + } + + /** + * @param string $token + * + * @return string + */ + protected function tokenEscape(string $token): string + { + if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { + return $match[1] . FlagHelper::escapeToken($match[2]); + } + + if ($token && $token[0] !== '-') { + return FlagHelper::escapeToken($token); + } + + return $token; + } /** * @param array $rawFlags diff --git a/src/IO/Input.php b/src/IO/Input.php index 0d7e90d..84f930f 100644 --- a/src/IO/Input.php +++ b/src/IO/Input.php @@ -9,21 +9,15 @@ namespace Inhere\Console\IO; +use Inhere\Console\IO\Input\StreamInput; use Toolkit\Cli\Cli; -use Toolkit\Cli\Flags; -use Toolkit\Cli\Helper\FlagHelper; -use Toolkit\FsUtil\File; -use function array_map; -use function fwrite; -use function implode; -use function preg_match; /** - * Class Input - The input information. by parse global var $argv. + * Class Input - The std input. * * @package Inhere\Console\IO */ -class Input extends AbstractInput +class Input extends StreamInput { /** * The real command ID(group:command) @@ -33,13 +27,6 @@ class Input extends AbstractInput */ protected $commandId = ''; - /** - * Default is STDIN - * - * @var resource - */ - protected $stream; - /** * Input constructor. * @@ -51,63 +38,10 @@ public function __construct(array $args = null) $args = $_SERVER['argv']; } - $this->stream = Cli::getInputStream(); + parent::__construct(Cli::getInputStream()); $this->collectInfo($args); } - /** - * @return string - */ - public function toString(): string - { - $tokens = array_map([$this, 'tokenEscape'], $this->tokens); - - return implode(' ', $tokens); - } - - /** - * @param string $token - * - * @return string - */ - protected function tokenEscape(string $token): string - { - if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { - return $match[1] . FlagHelper::escapeToken($match[2]); - } - - if ($token && $token[0] !== '-') { - return FlagHelper::escapeToken($token); - } - - return $token; - } - - /** - * @return string - */ - public function readAll(): string - { - return File::streamReadAll($this->stream); - } - - /** - * Read input information - * - * @param string $question 若不为空,则先输出文本消息 - * @param bool $nl true 会添加换行符 false 原样输出,不添加换行符 - * - * @return string - */ - public function readln(string $question = '', bool $nl = false): string - { - if ($question) { - fwrite($this->stream, $question . ($nl ? "\n" : '')); - } - - return File::streamFgets($this->stream); - } - /** * @return resource */ @@ -141,14 +75,6 @@ public function getFullCommand(): string return $this->scriptFile . ' ' . $this->getCommandPath(); } - /** - * @return resource - */ - public function getStream() - { - return $this->stream; - } - /** * Get command ID e.g `http:start` * diff --git a/src/IO/Input/AloneInput.php b/src/IO/Input/AloneInput.php deleted file mode 100644 index db5d8fc..0000000 --- a/src/IO/Input/AloneInput.php +++ /dev/null @@ -1,33 +0,0 @@ -args, - $this->sOpts, - $this->lOpts - ] = Flags::parseArgv($args); - } -} diff --git a/src/IO/Input/ArrayInput.php b/src/IO/Input/ArrayInput.php index 138b2da..8d372d3 100644 --- a/src/IO/Input/ArrayInput.php +++ b/src/IO/Input/ArrayInput.php @@ -11,6 +11,7 @@ use Inhere\Console\IO\Input; use Toolkit\Cli\Flags; +use function is_int; /** * Class ArrayInput @@ -22,10 +23,19 @@ class ArrayInput extends Input /** * Input constructor. * - * @param null|array $args + * @param array $arr */ - public function __construct(array $args = null) + public function __construct(array $arr = []) { + $args = []; + foreach ($arr as $key => $val) { + if (!is_int($key)) { + $args[] = $key; + } + + $args[] = $val; + } + parent::__construct($args); } } diff --git a/src/IO/Input/StreamInput.php b/src/IO/Input/StreamInput.php index 8f4c8f8..5e0e24c 100644 --- a/src/IO/Input/StreamInput.php +++ b/src/IO/Input/StreamInput.php @@ -39,11 +39,6 @@ public function __construct($stream = STDIN) $this->setStream($stream); } - public function toString(): string - { - return ''; - } - /** * @return string */ @@ -98,7 +93,7 @@ protected function setStream($stream): void File::assertStream($stream); $meta = stream_get_meta_data($stream); - if (strpos($meta['mode'], 'r') === false && strpos($meta['mode'], '+') === false) { + if (!str_contains($meta['mode'], 'r') && !str_contains($meta['mode'], '+')) { throw new InvalidArgumentException('Expected a readable stream'); } diff --git a/src/IO/Input/StringInput.php b/src/IO/Input/StringInput.php index fad5d0b..ea777c1 100644 --- a/src/IO/Input/StringInput.php +++ b/src/IO/Input/StringInput.php @@ -10,7 +10,6 @@ namespace Inhere\Console\IO\Input; use Inhere\Console\IO\Input; -use Toolkit\Cli\Flags; use Toolkit\Cli\Util\LineParser; /** @@ -28,6 +27,7 @@ class StringInput extends Input public function __construct(string $line) { $flags = LineParser::parseIt($line); + parent::__construct($flags); } } diff --git a/src/IO/Output.php b/src/IO/Output.php index bcd18c8..9aabb7f 100644 --- a/src/IO/Output.php +++ b/src/IO/Output.php @@ -9,28 +9,21 @@ namespace Inhere\Console\IO; -use Toolkit\Cli\Style; -use Inhere\Console\Console; use Inhere\Console\Concern\FormatOutputAwareTrait; +use Inhere\Console\Console; +use Inhere\Console\IO\Output\StreamOutput; use Toolkit\Cli\Cli; -use Toolkit\FsUtil\File; +use Toolkit\Cli\Style; /** * Class Output * * @package Inhere\Console\IO */ -class Output extends AbstractOutput +class Output extends StreamOutput { use FormatOutputAwareTrait; - /** - * Normal output stream. Default is STDOUT - * - * @var resource - */ - protected $outputStream; - /** * Error output stream. Default is STDERR * @@ -48,25 +41,14 @@ class Output extends AbstractOutput /** * Output constructor. - * - * @param null|resource $outputStream */ - public function __construct($outputStream = null) + public function __construct() { - if ($outputStream) { - $this->outputStream = $outputStream; - } else { - $this->outputStream = Cli::getOutputStream(); - } + parent::__construct(Cli::getOutputStream()); $this->getStyle(); } - public function resetOutputStream(): void - { - $this->outputStream = Cli::getOutputStream(); - } - /*************************************************************************** * Output buffer ***************************************************************************/ @@ -90,14 +72,14 @@ public function clearBuffer(): void /** * stop buffering and flush buffer text * - * @param bool $flush - * @param bool $nl - * @param bool $quit + * @param bool $flush + * @param bool $nl + * @param bool|int $quit * @param array $opts * * @see Console::stopBuffer() */ - public function stopBuffer(bool $flush = true, $nl = false, $quit = false, array $opts = []): void + public function stopBuffer(bool $flush = true, bool $nl = false, $quit = false, array $opts = []): void { Console::stopBuffer($flush, $nl, $quit, $opts); } @@ -105,8 +87,8 @@ public function stopBuffer(bool $flush = true, $nl = false, $quit = false, array /** * stop buffering and flush buffer text * - * @param bool $nl - * @param bool $quit + * @param bool $nl + * @param bool|int $quit * @param array $opts */ public function flush(bool $nl = false, $quit = false, array $opts = []): void @@ -122,7 +104,7 @@ public function flush(bool $nl = false, $quit = false, array $opts = []): void * Read input information * * @param string $question 若不为空,则先输出文本 - * @param bool $nl true 会添加换行符 false 原样输出,不添加换行符 + * @param bool $nl true 会添加换行符 false 原样输出,不添加换行符 * * @return string */ @@ -132,17 +114,22 @@ public function read(string $question = '', bool $nl = false): string } /** + * Read input information + * + * @param string $question + * @param bool $nl + * * @return string */ - public function readAll(): string + public function readln(string $question = '', bool $nl = false): string { - return File::streamReadAll($this->outputStream); + return Console::readln($question, $nl); } /** * Write a message to standard error output stream. * - * @param string $text + * @param string $text * @param boolean $nl True (default) to append a new line at the end of the output string. * * @return int @@ -183,7 +170,12 @@ public function supportColor(): bool */ public function getOutputStream() { - return $this->outputStream; + return $this->stream; + } + + public function resetOutputStream(): void + { + $this->stream = Cli::getOutputStream(); } /** @@ -195,8 +187,7 @@ public function getOutputStream() */ public function setOutputStream($outStream): self { - $this->outputStream = $outStream; - + $this->stream = $outStream; return $this; } @@ -218,7 +209,6 @@ public function getErrorStream() public function setErrorStream($errorStream): self { $this->errorStream = $errorStream; - return $this; } } diff --git a/src/IO/Output/MemoryOutput.php b/src/IO/Output/MemoryOutput.php index 1a197f2..ded3af0 100644 --- a/src/IO/Output/MemoryOutput.php +++ b/src/IO/Output/MemoryOutput.php @@ -23,6 +23,9 @@ public function __construct() parent::__construct(fopen('php://memory', 'rwb')); } + /** + * @return string + */ public function getBuffer(): string { return ''; diff --git a/src/IO/Output/StreamOutput.php b/src/IO/Output/StreamOutput.php index 36fdeee..2314371 100644 --- a/src/IO/Output/StreamOutput.php +++ b/src/IO/Output/StreamOutput.php @@ -25,6 +25,8 @@ class StreamOutput extends AbstractOutput { /** + * Normal output stream. Default is STDOUT + * * @var resource */ protected $stream; @@ -75,7 +77,7 @@ protected function setStream($stream): void File::assertStream($stream); $meta = stream_get_meta_data($stream); - if (strpos($meta['mode'], 'w') === false && strpos($meta['mode'], '+') === false) { + if (!str_contains($meta['mode'], 'w') && !str_contains($meta['mode'], '+')) { throw new InvalidArgumentException('Expected a readable stream'); } diff --git a/src/IO/TempStream.php b/src/IO/TempStream.php index cd7093e..e535bdd 100644 --- a/src/IO/TempStream.php +++ b/src/IO/TempStream.php @@ -66,7 +66,7 @@ public static function close(): void } /** - * @param string|int $string + * @param string|int|mixed $string */ public static function write($string): void { diff --git a/test/IO/InputTest.php b/test/IO/InputTest.php index aab85a1..b647980 100644 --- a/test/IO/InputTest.php +++ b/test/IO/InputTest.php @@ -25,6 +25,8 @@ public function testBasic(): void $this->assertSame('./bin/app', $in->getScriptFile()); $this->assertSame('app', $in->getScriptName()); - $this->assertSame('cmd', $in->getCommand()); + // $this->assertSame('cmd', $in->getCommand()); + $this->assertEquals('cmd val0 val1', $in->getFullScript()); + $this->assertEquals("'./bin/app' cmd val0 val1", $in->toString()); } }