Commit c5446821 authored by 谢宇轩's avatar 谢宇轩

refactor: 支持PHP8版本的模板

parent f649e58e
......@@ -5,15 +5,18 @@ include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
use Jiwei\EasyHttpSdk\Generation\SdkEndpointBuilder;
$params = getopt("d:e:p:h", [
$params = getopt("d:e:p:v:h", [
"dist:",
"endpoints:",
"package:",
"version:",
"help"
]);
if (isset($params['h']) || isset($params['help'])) {
echo "SDK生成工具🧸ver:0.1\n
-v --version
输出SDK Endpoint 类的PHP语法版本,默认兼容 7.2,目前最高支持 8.1
-d --dist
输出SDK Endpoint 类的目录 默认为 ./src/Endpoint/
-e --endpoints
......@@ -25,6 +28,7 @@ SDK Endpoint 类所使用的命名空间名称 " . PHP_EOL;
$packageName = $params['p'] ?? $params['package'] ?? "";
$version = $params['v'] ?? $params['version'] ?? "70000";
$distPath = $params['d'] ?? $params['dist'] ?? $_composer_bin_dir . "/../../src/Endpoint/";
$endpointPath = $params['e'] ?? $params['endpoints'] ?? $_composer_bin_dir . "/../../endpoints/";
......@@ -45,12 +49,16 @@ echo "正在创建Jiwei\\{$packageName}\\Endpoint\\" . PHP_EOL;
echo "目标目录{$distPath}" . PHP_EOL;
echo "配置来源{$endpointPath}" . PHP_EOL;
if(substr($version, 0, 1) == "8"){
$version = 80000;
}
$builder = new SdkEndpointBuilder(
$distPath,
$endpointPath,
__DIR__ . '/../template/',
$packageName
$packageName,
$version
);
$count = $builder->build();
......
......@@ -31,18 +31,23 @@ class SdkEndpointBuilder
/** @var string 包命名空间 */
protected $package;
/** @var string PHP适配版本 70000 80000 */
protected $version;
/**
* @param string $endpointDstPath
* @param string $endpointSrcPath
* @param string $twigPath
* @param string $package
* @param string $version
*/
public function __construct(string $endpointDstPath, string $endpointSrcPath, string $twigPath, string $package)
public function __construct(string $endpointDstPath, string $endpointSrcPath, string $twigPath, string $package, string $version = "70000")
{
$this->endpointDstPath = $endpointDstPath;
$this->endpointSrcPath = $endpointSrcPath;
$this->twigPath = $twigPath;
$this->package = $package;
$this->version = $version;
$loader = new FilesystemLoader($this->twigPath);
$this->twig = new Environment($loader);
......@@ -191,7 +196,7 @@ class SdkEndpointBuilder
"Func" => $endpointFunc,
];
try {
$phpCode = $this->twig->render('Endpoint.temp', $endpointSchema);
$phpCode = $this->twig->render(sprintf('Endpoint%s.temp', $this->version), $endpointSchema);
$renderResult[$name] = $phpCode;
echo sprintf("Render Class[%s] ...", $name) . PHP_EOL;
} catch (LoaderError|RuntimeError|SyntaxError $e) {
......
{% autoescape false %}
<?php
// generated code, do not edit.
declare(strict_types=1);
namespace Jiwei\{{ Package }}\Endpoint;
use Closure;
use Jiwei\EasyHttpSdk\Application;
use Jiwei\EasyHttpSdk\Exception\SdkException;
use Jiwei\EasyHttpSdk\Exception\ApplicationException;
use Jiwei\EasyHttpSdk\Exception\TimeOutExcetpion;
use Jiwei\EasyHttpSdk\Helper\ArgsRecorder;
use Jiwei\EasyHttpSdk\Helper\CallBackHelper;
use Jiwei\EasyHttpSdk\Http\SdkRequest;
class {{ Endpoint }}
{
use ArgsRecorder;
use CallBackHelper;
/** @var Application */
protected Application $application;
/**
* @param Application $application
*/
public function __construct(
protected Application $application
){}
{% for item in Func %}
/**
* {{ item.Description }}
*
{% for param in item.Params %}
* @param {{ param.type }} {{ param.name }}
{% endfor %}
* @param Closure|null $catch
* @param Closure|null $then
* @return mixed
*
* @throws ApplicationException
* @throws SdkException
* @throws TimeOutExcetpion
*/
public function {{ item.Action }}({{ item.Args }}?Closure $catch = null, ?Closure $then = null): mixed
{
$url = "{{ item.Uri }}";
{% if item.Join|length > 0 %}
$url .= "?";
{% endif%}
{% for join in item.Join %}
if ({{ join.name }} != "") {
$url .= {{ join.query }};
}
{% endfor %}
$request = new SdkRequest("{{ item.Method }}", $url, "{{ Endpoint }}", "{{ item.Action }}", []{{ item.Body }});
$request = $request->withArgs($this->ArgsRecoder(__METHOD__, func_get_args()))->withRequestID(){{ item.Middleware }};
try {
$response = $this->application->sendRequest($request);
$decodedData = $this->application->resultDecode($request, $response);
$this->setLastResult($decodedData);
} catch (SdkException $sdkException) {
$this->setLastException($sdkException);
$catch = $catch ?? $this->defaultFailedCallBack();
return $catch($this);
}
$then = $then ?? $this->defaultSuccessCallBack();
return $then($this);
}
{% endfor %}
}
{% endautoescape %}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment