From 466ddff55e2fd1f438d3269df157f89dc1154ffc Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 5 Nov 2021 19:17:57 +0800 Subject: [PATCH] chore: update some style, add some util methods --- app/Console/Command/ExprCommand.php | 10 +++++----- app/Helper/KiteUtil.php | 25 +++++++++++++++++++++++++ app/Lib/Generate/AbstractJsonToCode.php | 8 ++++++-- app/Lib/Stream/ListStream.php | 15 +++++++++++++++ config/config.cli.php | 2 +- 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/app/Console/Command/ExprCommand.php b/app/Console/Command/ExprCommand.php index 30b632b..882caab 100644 --- a/app/Console/Command/ExprCommand.php +++ b/app/Console/Command/ExprCommand.php @@ -103,6 +103,11 @@ public function getFunctions(): array * @options * -i, --interactive bool;Start an interactive shell environment * + * @param Input $input + * @param Output $output + * + * @return int + * @throws Throwable * @example * $ {binWithCmd} -i * Expr > 45 + 45 @@ -110,11 +115,6 @@ public function getFunctions(): array * Expr > ret * 2 * 180 * - * @param Input $input - * @param Output $output - * - * @return int - * @throws Throwable */ protected function execute(Input $input, Output $output): int { diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index 69bcc21..1349e42 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -7,6 +7,7 @@ use Toolkit\Stdlib\OS; use Toolkit\Stdlib\Str; use function dirname; +use function in_array; use function is_file; /** @@ -14,6 +15,30 @@ */ class KiteUtil { + public const STDIN_ALIAS = ['@i','@stdin',]; + public const STDOUT_ALIAS = ['@o','@stdout',]; + + public const CLIPBOARD_ALIAS = ['@c','@cb','@clip','@clipboard',]; + + /** + * @param string $str + * + * @return bool + */ + public static function isStdinAlias(string $str): bool + { + return in_array($str, self::CLIPBOARD_ALIAS, true); + } + + /** + * @param string $str + * + * @return bool + */ + public static function isClipboardAlias(string $str): bool + { + return in_array($str, self::CLIPBOARD_ALIAS, true); + } /** * @param string $path diff --git a/app/Lib/Generate/AbstractJsonToCode.php b/app/Lib/Generate/AbstractJsonToCode.php index f8cadf2..eb0c42f 100644 --- a/app/Lib/Generate/AbstractJsonToCode.php +++ b/app/Lib/Generate/AbstractJsonToCode.php @@ -234,9 +234,13 @@ protected function readSourceFromFile(): string // check from tplDir if ($this->tplDir && !is_file($tplFile)) { $tplDir = $this->resolvePath($this->tplDir); - $tplFile = File::joinPath($tplDir, $tplFile); + $dirFile = File::joinPath($tplDir, $tplFile); - File::assertIsFile($tplFile); + if (!is_file($dirFile)) { + throw new InvalidArgumentException("No such file: $tplFile"); + } + + $tplFile = $dirFile; } return File::readAll($tplFile); diff --git a/app/Lib/Stream/ListStream.php b/app/Lib/Stream/ListStream.php index ac2ac3e..b10bec0 100644 --- a/app/Lib/Stream/ListStream.php +++ b/app/Lib/Stream/ListStream.php @@ -65,6 +65,21 @@ public function eachTo(callable $func, BaseStream $new): BaseStream return $new; } + /** + * @param callable(array): array $func + * + * @return array + */ + public function eachToArray(callable $func): array + { + $arr = []; + foreach ($this as $item) { + $arr[] = $func($item); + } + + return $arr; + } + /** * @param callable(array): array $func * @param MapStream $new diff --git a/config/config.cli.php b/config/config.cli.php index 5cbf901..484cea1 100644 --- a/config/config.cli.php +++ b/config/config.cli.php @@ -30,7 +30,7 @@ ], // @see app/Console/Controller/PhpController.php 'php:serve' => [ - 'hce-file' => 'test/clienttest/http-client.env.json', + 'hce-file' => 'test/httptest/http-client.env.json', 'hce-env' => getenv('APP_ENV') ?: 'development', // 'entry' => 'public/index.php', // document root