Skip to content

Commit

Permalink
fix: fix json to php class logic error
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 12, 2022
1 parent a3071c5 commit 468de9d
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 20 deletions.
14 changes: 11 additions & 3 deletions app/Console/Controller/DemoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace Inhere\Kite\Console\Controller;

use Inhere\Console\Controller;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Toolkit\PFlag\FlagsParser;

/**
* Class DemoController
Expand All @@ -27,6 +27,14 @@ public static function isEnabled(): bool
return false;
}

/**
* @return array{string: list<string>}
*/
protected static function commandAliases(): array
{
return [];
}

/**
* run a php built-in server for development(is alias of the command 'server:dev')
*
Expand All @@ -39,10 +47,10 @@ public static function isEnabled(): bool
* -H,--host The server host address. e.g 127.0.0.1
* -p,--port The server host address. e.g 5577
*
* @param Input $input
* @param FlagsParser $fs
* @param Output $output
*/
public function serveCommand(Input $input, Output $output): void
public function serveCommand(FlagsParser $fs, Output $output): void
{
$output->success('Complete');
}
Expand Down
63 changes: 63 additions & 0 deletions app/Console/Controller/HttpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);
/**
* This file is part of Kite.
*
* @link https://github.com/inhere
* @author https://github.com/inhere
* @license MIT
*/

namespace Inhere\Kite\Console\Controller;

use Inhere\Console\Controller;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str\UrlHelper;

/**
* Class HttpController
*/
class HttpController extends Controller
{
protected static string $name = 'http';

protected static string $desc = 'Some useful http tool commands';

/**
* @return array{string: list<string>}
*/
protected static function commandAliases(): array
{
return [
'bulk2query' => ['2query', 'to-query'],
];
}

/**
* convert k-v map text to http url query string.
*
* @options
* -s,--source string;The source k-v map text. allow: FILEPATH, @clipboard;true
* --not-enc,--not-encode bool;Not apply url-encode for generated string
*
* @param FlagsParser $fs
* @param Output $output
*/
public function bulk2queryCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
$source = ContentsAutoReader::readFrom($source);

$output->colored('SOURCE:');
$output->writeRaw($source);

$query = str_replace([': ', "\n", ':'], ['=', '&', '='], $source);
if (!$fs->getOpt('not-encode')) {
$query = UrlHelper::encode($query);
}

$output->colored('RESULT:');
$output->writeRaw($query);
}
}
14 changes: 8 additions & 6 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
use function is_scalar;
use function json_decode;
use function str_contains;
use function str_replace;
use function trim;
use function vdump;
use const JSON_THROW_ON_ERROR;

/**
Expand Down Expand Up @@ -292,8 +292,6 @@ public function ml2lineCommand(): void
*/
public function toClassCommand(FlagsParser $fs, Output $output): void
{
$config = Kite::cliApp()->getArrayParam('json_toClass');

$type = $fs->getOpt('type');
$json = $fs->getArg('source');
$json = ContentsAutoReader::readFrom($json, [
Expand All @@ -304,18 +302,22 @@ public function toClassCommand(FlagsParser $fs, Output $output): void
throw new InvalidArgumentException('empty input json(5) text for handle');
}

$tplDir = $fs->getOpt('tpl-dir');
$config = Kite::cliApp()->getArrayParam('json_toClass');
$tplDir = $fs->getOpt('tpl-dir', $config['tplDir'] ?? '');
$tplDir = str_replace('{type}', $type, $tplDir);

$tplFile = $fs->getOpt('tpl-file');
if (!$tplFile) {
// $tplFile = "@resource-tpl/dto-class/$type-data-dto.tpl";
$tplFile = "@user-custom/template/$type-code-tpl/req-resp-dto.tpl";
$tplFile = "$tplDir/req-resp-dto.tpl";
}

$config = array_merge($config, array_filter([
'tplDir' => $tplDir,
'tplFile' => $tplFile,
]));

$output->aList($config);

// @user-custom/template/java-service-tpl/req-resp-dto.tpl
$gen = JsonToCode::create($type)
->setSource($json)
Expand Down
2 changes: 1 addition & 1 deletion app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static function userConfigDir(string $path = ''): string
}

/**
* @param array $config
* @param array{tplDir: string, allowExt: array, globalVars: array} $config
*
* @return EasyTemplate
*/
Expand Down
37 changes: 31 additions & 6 deletions app/Lib/Generate/AbstractJsonToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,24 @@ abstract class AbstractJsonToCode
*/
// private array $jsonData = [];

/**
* @var string
*/
public string $className = 'YourClass';

/**
* @var array
*/
private array $fields = [];

/**
* @return string
*/
public function getLang(): string
{
return 'java';
}

/**
* @return string
*/
Expand All @@ -88,6 +101,9 @@ public function prepare(): self
throw new InvalidArgumentException('empty source json(5) data for generate');
}

// defaults
$this->contexts['className'] = $this->className;

$jd = Json5Data::new()->loadFrom($json);

$this->fields = $jd->getFields();
Expand All @@ -103,18 +119,16 @@ protected function renderTplText(): string
{
$tplText = $this->readSourceFromFile();
$settings = array_merge([
'lang' => 'java',
'lang' => $this->getLang(),
'user' => OS::getUserName(),
'date' => date('Y-m-d'),
], $this->contexts);

$settings['fields'] = $this->fields;
// $tplVars = [
// 'ctx' => $settings,
// 'fields' => $this->fields,
// ];

return KiteUtil::newTplEngine()->renderString($tplText, $settings);
return KiteUtil::newTplEngine([
'tplDir' => $this->tplDir,
])->renderString($tplText, $settings);
}

/**
Expand Down Expand Up @@ -228,4 +242,15 @@ public function setPathResolver(callable $pathResolver): self
$this->pathResolver = $pathResolver;
return $this;
}

/**
* @param string $className
*
* @return AbstractJsonToCode
*/
public function setClassName(string $className): self
{
$this->className = $className;
return $this;
}
}
2 changes: 0 additions & 2 deletions app/Lib/Generate/JsonToJavaClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public function generate(): string
// todo
$this->contexts['package'] = 'YOUR.PKG.NAME';
$this->contexts['pkgName'] = 'PKG_NAME';
$this->contexts['className'] = 'YourClass';

return parent::generate();
}

}
9 changes: 7 additions & 2 deletions app/Lib/Generate/JsonToPHPClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Inhere\Kite\Lib\Generate;

use function random_int;

/**
* class JsonToPHPClass
*/
class JsonToPHPClass extends AbstractJsonToCode
{
public const TYPE = 'php';

/**
* @return string
*/
public function getLang(): string
{
return self::TYPE;
}
}
29 changes: 29 additions & 0 deletions script/intall-go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# install go on linux

GO_VER=1.17.6
GO_TAR=go$GO_VER.linux-amd64.tar.gz
echo "Will install the $GO_TAR"

cd /tmp || exit 2
# mac https://studygolang.com/dl/golang/go1.17.6.darwin-amd64.pkg
# linux https://studygolang.com/dl/golang/go1.17.6.linux-amd64.tar.gz
wget https://studygolang.com/dl/golang/$GO_TAR

# decompress
tar -C /usr/local -xzf $GO_TAR

cat <<EOF
The go $GO_VER download ok and decompress to /usr/local/go.
please config something for enable it.
# config:
# .bashrc OR /etc/profile 最后一行添加
export PATH=\$PATH:/usr/local/go/bin
# check install
go version
# go env -w GOPROXY=https://goproxy.io,direct
go env -w GOPROXY=https://goproxy.cn,direct
go env
EOF

0 comments on commit 468de9d

Please sign in to comment.