Skip to content

Commit

Permalink
refactor: refacting the input output class.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 27, 2021
1 parent e4a2695 commit 6f97050
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 162 deletions.
5 changes: 5 additions & 0 deletions src/Contract/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
31 changes: 30 additions & 1 deletion src/IO/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down
82 changes: 4 additions & 78 deletions src/IO/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -33,13 +27,6 @@ class Input extends AbstractInput
*/
protected $commandId = '';

/**
* Default is STDIN
*
* @var resource
*/
protected $stream;

/**
* Input constructor.
*
Expand All @@ -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
*/
Expand Down Expand Up @@ -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`
*
Expand Down
33 changes: 0 additions & 33 deletions src/IO/Input/AloneInput.php

This file was deleted.

14 changes: 12 additions & 2 deletions src/IO/Input/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Inhere\Console\IO\Input;
use Toolkit\Cli\Flags;
use function is_int;

/**
* Class ArrayInput
Expand All @@ -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);
}
}
7 changes: 1 addition & 6 deletions src/IO/Input/StreamInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public function __construct($stream = STDIN)
$this->setStream($stream);
}

public function toString(): string
{
return '';
}

/**
* @return string
*/
Expand Down Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/IO/Input/StringInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Inhere\Console\IO\Input;

use Inhere\Console\IO\Input;
use Toolkit\Cli\Flags;
use Toolkit\Cli\Util\LineParser;

/**
Expand All @@ -28,6 +27,7 @@ class StringInput extends Input
public function __construct(string $line)
{
$flags = LineParser::parseIt($line);

parent::__construct($flags);
}
}
Loading

0 comments on commit 6f97050

Please sign in to comment.