Skip to content

Commit

Permalink
up(str): move parse url query to a command class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 15, 2022
1 parent d1e6f02 commit 7305450
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 34 deletions.
9 changes: 9 additions & 0 deletions app/Console/Controller/HttpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +32,14 @@ protected static function commandAliases(): array
{
return [
'bulk2query' => ['2query', 'to-query'],
'dequery' => ParseUrlQueryCmd::aliases(),
];
}

protected function subCommands(): array
{
return [
ParseUrlQueryCmd::class,
];
}

Expand Down
50 changes: 16 additions & 34 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -74,6 +73,7 @@ protected static function commandAliases(): array
'process' => ['p', 'filter', 'f'],
'replace' => ['r'],
'parse' => ['fields'],
'dequery' => ParseUrlQueryCmd::aliases(),
];
}

Expand All @@ -84,6 +84,13 @@ protected function init(): void
$this->dumpfile = Kite::getTmpPath('string-loaded.txt');
}

protected function subCommands(): array
{
return [
ParseUrlQueryCmd::class,
];
}

/**
* @throws Throwable
*/
Expand Down Expand Up @@ -215,7 +222,7 @@ protected function applyFilters(string $str, array $filters): string
break;
}

$args = [];
$args = [];
$argStr = '';

// eg 'wrap:,'
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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':
Expand All @@ -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;
Expand All @@ -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.
*
Expand Down
55 changes: 55 additions & 0 deletions app/Console/SubCmd/ParseUrlQueryCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use function parse_str;
use function rawurldecode;

/**
* Class ParseUrlQueryCmd
*/
class ParseUrlQueryCmd extends Command
{
protected static string $name = 'dequery';
protected static string $desc = 'decode URL query string and parse it';

public static function aliases(): array
{
return ['dq'];
}

protected function configure(): void
{
// $this->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;
}
}

0 comments on commit 7305450

Please sign in to comment.