From 7305450a2dd6bfaaa9b5e19d0742957792c4830a Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 15 Apr 2022 11:46:52 +0800 Subject: [PATCH] up(str): move parse url query to a command class --- app/Console/Controller/HttpController.php | 9 ++++ app/Console/Controller/StringController.php | 50 ++++++------------- app/Console/SubCmd/ParseUrlQueryCmd.php | 55 +++++++++++++++++++++ 3 files changed, 80 insertions(+), 34 deletions(-) create mode 100644 app/Console/SubCmd/ParseUrlQueryCmd.php diff --git a/app/Console/Controller/HttpController.php b/app/Console/Controller/HttpController.php index b32520b..eb6b46f 100644 --- a/app/Console/Controller/HttpController.php +++ b/app/Console/Controller/HttpController.php @@ -12,6 +12,7 @@ use Inhere\Console\Controller; use Inhere\Console\IO\Output; use Inhere\Kite\Console\Component\ContentsAutoReader; +use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd; use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Str\UrlHelper; @@ -31,6 +32,14 @@ protected static function commandAliases(): array { return [ 'bulk2query' => ['2query', 'to-query'], + 'dequery' => ParseUrlQueryCmd::aliases(), + ]; + } + + protected function subCommands(): array + { + return [ + ParseUrlQueryCmd::class, ]; } diff --git a/app/Console/Controller/StringController.php b/app/Console/Controller/StringController.php index beba627..25f0b85 100644 --- a/app/Console/Controller/StringController.php +++ b/app/Console/Controller/StringController.php @@ -15,6 +15,7 @@ use Inhere\Kite\Console\Component\Clipboard; use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Console\Component\ContentsAutoWriter; +use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\KiteUtil; use Inhere\Kite\Kite; @@ -34,8 +35,6 @@ use function explode; use function implode; use function is_file; -use function parse_str; -use function rawurldecode; use function str_contains; use function str_replace; use function strlen; @@ -74,6 +73,7 @@ protected static function commandAliases(): array 'process' => ['p', 'filter', 'f'], 'replace' => ['r'], 'parse' => ['fields'], + 'dequery' => ParseUrlQueryCmd::aliases(), ]; } @@ -84,6 +84,13 @@ protected function init(): void $this->dumpfile = Kite::getTmpPath('string-loaded.txt'); } + protected function subCommands(): array + { + return [ + ParseUrlQueryCmd::class, + ]; + } + /** * @throws Throwable */ @@ -215,7 +222,7 @@ protected function applyFilters(string $str, array $filters): string break; } - $args = []; + $args = []; $argStr = ''; // eg 'wrap:,' @@ -326,15 +333,15 @@ public function processCommand(FlagsParser $fs, Output $output): void // filters $filters = $fs->getOpt('filter'); - $cutPos = 'L'; + $cutPos = 'L'; $cutChar = $cut; if (strlen($cut) > 1) { if ($cut[0] === 'L') { // $cutPos = 'L'; - $cutChar= substr($cut, 1); + $cutChar = substr($cut, 1); } elseif ($cut[0] === 'R') { - $cutPos = 'R'; - $cutChar= substr($cut, 1); + $cutPos = 'R'; + $cutChar = substr($cut, 1); } } @@ -446,7 +453,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void $p->setItemParser($itemParser); $p->parse(); - $result = ''; + $result = ''; $doOutput = true; switch ($fs->getOpt('out-fmt')) { case 'mdtable': @@ -459,7 +466,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void $head = array_shift($rows); $line = implode('|', array_pad(['-----'], $p->fieldNum, '-----')); - $result = $head . "\n" . $line . "\n". implode("\n", $rows); + $result = $head . "\n" . $line . "\n" . implode("\n", $rows); break; case 'raw': $result = $text; @@ -479,31 +486,6 @@ public function parseCommand(FlagsParser $fs, Output $output): void } } - /** - * decode query string and print data. - * - * @options - * --full bool;The input query argument is full url - * - * @arguments - * query The URI query string. allow: FILEPATH, @clipboard - */ - public function dequeryCommand(FlagsParser $fs, Output $output): void - { - $query = $fs->getArg('query'); - - $str = ContentsAutoReader::readFrom($query); - $str = rawurldecode($str); - // println($str); - - // if (!$fs->getOpt('full')) { - // $str = 'http://abc.com?' . $str; - // } - parse_str($str, $ret); - - $output->aList($ret); - } - /** * Change case for input string. * diff --git a/app/Console/SubCmd/ParseUrlQueryCmd.php b/app/Console/SubCmd/ParseUrlQueryCmd.php new file mode 100644 index 0000000..52bc7a4 --- /dev/null +++ b/app/Console/SubCmd/ParseUrlQueryCmd.php @@ -0,0 +1,55 @@ +flags->addOptByRule($name, $rule); + $this->flags->addArg('query', 'The URI query string. allow: @clipboard', 'string', true); + } + + /** + * @param Input $input + * @param Output $output + * + * @return int|mixed + */ + protected function execute(Input $input, Output $output): mixed + { + $fs = $this->flags; + + $query = $fs->getArg('query'); + + $str = ContentsAutoReader::readFrom($query); + $str = rawurldecode($str); + // println($str); + + // if (!$fs->getOpt('full')) { + // $str = 'http://abc.com?' . $str; + // } + parse_str($str, $ret); + + $output->aList($ret, 'Query Data:'); + return 0; + } +}