Skip to content

Commit

Permalink
add more str,php,json util commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 3, 2021
1 parent 066c785 commit cfe192b
Show file tree
Hide file tree
Showing 19 changed files with 564 additions and 39 deletions.
20 changes: 2 additions & 18 deletions app/Common/IdeaHttp/BodyData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Inhere\Kite\Http\ContentType;
use RuntimeException;
use Toolkit\Stdlib\Obj\ConfigObject;
use Toolkit\Stdlib\Obj\DataObject;
use Toolkit\Stdlib\Str\UrlHelper;
use Toolkit\Stdlib\Type;
use function count;
Expand All @@ -19,7 +19,7 @@
*
* @package Inhere\Kite\Common\IdeaHttp
*/
class BodyData extends ConfigObject
class BodyData extends DataObject
{
/**
* @var string
Expand Down Expand Up @@ -190,22 +190,6 @@ public function genMethodParams(int $limitParam = 3): string
return implode(', ', $params);
}

/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->count() === 0;
}

/**
* @return string
*/
public function __toString(): string
{
return $this->toString();
}

/**
* @param string $cType
*
Expand Down
4 changes: 2 additions & 2 deletions app/Common/IdeaHttp/UrlInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Inhere\Kite\Common\IdeaHttp;

use Toolkit\Stdlib\Obj\ConfigObject;
use Toolkit\Stdlib\Obj\DataObject;
use Toolkit\Stdlib\Str;
use function basename;
use function strpos;
Expand All @@ -14,7 +14,7 @@
* @see \parse_url()
* @package Inhere\Kite\Common\IdeaHttp
*/
class UrlInfo extends ConfigObject
class UrlInfo extends DataObject
{
/**
* @param bool $newline
Expand Down
13 changes: 13 additions & 0 deletions app/Component/ClassGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Component;

use Toolkit\Stdlib\Obj\AbstractObj;

/**
* class ClassGenerator
*/
class ClassGenerator extends AbstractObj
{

}
20 changes: 20 additions & 0 deletions app/Console/Component/ContentsAutoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
use Toolkit\Cli\Cli;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj\AbstractObj;
use function fclose;
use function fopen;
use function is_file;
use function stream_get_contents;
use function stream_set_blocking;
use function substr;
use function vdump;

Expand Down Expand Up @@ -103,6 +107,22 @@ public function read(string $source, array $opts = []): string
return $str;
}

/**
* @return string
*/
protected function readFromStdin(): string
{
$text = '';
$stdin = fopen('php://stdin', 'r');

if (stream_set_blocking($stdin, false)) {
$text = stream_get_contents($stdin);
}

fclose($stdin);
return $text;
}

/**
* @return string
*/
Expand Down
4 changes: 3 additions & 1 deletion app/Console/Controller/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Output;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Template\TextTemplate;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
Expand Down Expand Up @@ -88,6 +89,7 @@ public function templateCommand(FlagsParser $fs, Output $output): void
public function parseCommand(FlagsParser $fs, Output $output): void
{
$tplFile = $fs->getArg('tpl');
$tplFile = Kite::alias($tplFile);
$content = file_get_contents($tplFile);

[$varDefine, $template] = explode('###', $content);
Expand All @@ -97,7 +99,7 @@ public function parseCommand(FlagsParser $fs, Output $output): void
if (is_string($var)) {
$str = trim($var);
// is array
if (strpos($str, '[') === 0) {
if (str_starts_with($str, '[')) {
$vars[$k] = Str::explode(trim($str, '[]'), ',');
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/Console/Controller/GitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use PhpGit\Repo;
use Throwable;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Obj\ConfigObject;
use Toolkit\Stdlib\Obj\DataObject;
use Toolkit\Stdlib\Str;
use function abs;
use function array_keys;
Expand All @@ -54,9 +54,9 @@ class GitController extends Controller
protected static $description = 'Provide useful tool commands for quick use git';

/**
* @var ConfigObject
* @var DataObject
*/
private ConfigObject $settings;
private DataObject $settings;

public static function aliases(): array
{
Expand Down Expand Up @@ -113,7 +113,7 @@ protected function options(): array
protected function beforeRun(): void
{
if ($this->app && !isset($this->settings)) {
$this->settings = ConfigObject::new($this->app->getArrayParam('git'));
$this->settings = DataObject::new($this->app->getArrayParam('git'));
}

if ($workdir = $this->flags->getOpt('workdir')) {
Expand Down
27 changes: 22 additions & 5 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ protected static function commandAliases(): array
];
}


protected function init(): void
{
parent::init();
Expand Down Expand Up @@ -292,18 +291,36 @@ public function ml2lineCommand(): void
}

/**
* convert JSON object string to PHP class.
* convert JSON object string to PHP/JAVA DTO class.
*
* @options
* --cb bool;read input from clipboard
* -f,--file The source markdown code
* -o,--output The output target. default is stdout.
* -s, --source The source json contents
* -o, --output The output target. default is STDOUT.
* -t, --type string;the generate code language type, allow: java, php;;php
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws JsonException
*/
public function toClassCommand(FlagsParser $fs, Output $output): void
{
$json = $fs->getArg('json');
$json = ContentsAutoReader::readFrom($json, [
'loadedFile' => $this->dumpfile,
]);

if (!$json = trim($json)) {
throw new InvalidArgumentException('empty input json(5) text for handle');
}

if ($json[0] !== '{') {
$json = '{' . $json . '}';
}

$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);


$output->success('Complete');
}

Expand Down
84 changes: 79 additions & 5 deletions app/Console/Controller/PhpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\PhpDevServe;
use Inhere\Kite\Common\Cmd;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Common\GitLocal\GitHub;
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\KiteUtil;
use InvalidArgumentException;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Json;
Expand All @@ -26,9 +28,11 @@
use function addslashes;
use function array_filter;
use function array_merge;
use function dirname;
use function function_exists;
use function implode;
use function is_dir;
use function is_file;
use function is_numeric;
use function ob_get_clean;
use function ob_start;
Expand Down Expand Up @@ -58,6 +62,7 @@ protected static function commandAliases(): array
'pkgOpen' => ['open', 'pkg-open'],
'runCode' => ['eval', 'run-code', 'run-codes'],
'runFunc' => ['run', 'exec', 'run-func'],
'runUnit' => ['run-unit', 'unit', 'run-test', 'phpunit'],
];
}

Expand All @@ -82,8 +87,37 @@ public function str2arrCommand(FlagsParser $fs, Output $output): void
* convert create mysql table SQL to PHP class
*
* @options
* --cb bool;read source code from clipboard
* -f,--file The source code file
* -s,--source The source code string or file
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function text2classCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
}

/**
* convert create mysql table SQL to PHP class
*
* @options
* -s,--source The source code string or file
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function arr2classCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
}

/**
* convert create mysql table SQL to PHP class
*
* @options
* -s,--source The source code string or file
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
Expand All @@ -98,8 +132,7 @@ public function json2classCommand(FlagsParser $fs, Output $output): void
* convert create mysql table SQL to PHP class
*
* @options
* --cb bool;read source code from clipboard
* -f,--file The source code file
* -s,--source The source code string or file
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
Expand All @@ -114,7 +147,7 @@ public function sql2classCommand(FlagsParser $fs, Output $output): void
* convert an mysql INSERT SQL to php k-v array
*
* @options
* -f,--file The source markdown code
* -s,--source The source code string or file
* -o,--output The output target. default is stdout.
*
* @param FlagsParser $fs
Expand All @@ -125,6 +158,46 @@ public function sql2arrCommand(FlagsParser $fs, Output $output): void
$output->success('Complete');
}

/**
* auto find the phpunit.xml dir and run phpunit tests
*
* @arguments
* dir The php unit tests code dir or file path
*
* @options
* --no-debug bool;not set the --debug option on run test
* -f, --filter Set the --filter option for phpunit
* --php-bin manual set the php bin file path.
* --phpunit manual set the phpunit(.phar) file path.
*
* @param FlagsParser $fs
* @param Output $output
*/
public function runUnitCommand(FlagsParser $fs, Output $output): void
{
$dir = $fs->getOpt('dir', $this->getInput()->getWorkDir());
if (is_file($dir)) {
$dir = dirname($dir);
}

$runDir = KiteUtil::findPhpUnitConfigFile($dir);
if (!$runDir) {
throw new InvalidArgumentException("not found the phpunit.xml(.dist) in $dir or any parent dir");
}

// phpunit --debug --filter KEYWORDS
$cmd = Cmd::new('phpunit');
$cmd->addIf('--debug', !$fs->getOpt('no-debug'));

if ($filter = $fs->getOpt('filter')) {
$cmd->addArgs('--filter', $filter);
}

$cmd->runAndPrint();

$output->success('Complete');
}

/**
* run php-cs-fixer for an dir, and auto add git commit message
*
Expand Down Expand Up @@ -268,6 +341,7 @@ public function serveCommand(FlagsParser $fs, Output $output): void
*
* @example
* {binWithCmd} strlen "inhere" # output: 6
* {binWithCmd} basename refs/heads/master # output: master
*
*/
public function runFuncCommand(FlagsParser $fs, Output $output): void
Expand Down
16 changes: 16 additions & 0 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function explode;
use function implode;
use function is_file;
use function parse_str;
use function preg_match;
use function str_contains;
use function str_replace;
Expand Down Expand Up @@ -354,6 +355,21 @@ public function fieldsCommand(FlagsParser $fs, Output $output): void
$output->aList($fields);
}

/**
* decode query string and print data.
*
* @arguments
* query The URI query string.
*/
public function dequeryCommand(FlagsParser $fs, Output $output): void
{
$str = $fs->getArg('query');

parse_str($str, $ret);

$output->aList($ret);
}

/**
* Change case for input string.
*
Expand Down

0 comments on commit cfe192b

Please sign in to comment.