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

refactor: 新增配置option生成工具

parent d6f878ac
#!/usr/bin/env php
<?php
include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';
use Jiwei\EasyHttpSdk\Generation\SdkOptionBuilder;
$params = getopt("p:d:h", [
"dist:",
"package:",
"help"
]);
if (isset($params['h']) || isset($params['help'])) {
echo "SDK Option 生成工具🧸ver:0.1\n
-d --dist
输出SDK Option 类的目录 默认为 ./src/
-p--package
SDK Option 类所使用的包名 " . PHP_EOL;
exit;
}
$packageName = $params['p'] ?? $params['package'] ?? "";
$distPath = $params['d'] ?? $params['dist'] ?? __DIR__ . "./src/";
if ("" === $packageName) {
echo "请输入的包名" . PHP_EOL;
exit;
}
if (substr($distPath, -1) != "/") {
$distPath .= "/";
}
echo "正在创建Jiwei\\{$packageName}\\Sdk\\Option配置文件" . PHP_EOL;
echo "目标目录{$distPath}" . PHP_EOL;
$builder = new SdkOptionBuilder(
$distPath,
__DIR__ . '/../template/',
$packageName
);
$builder->build();
echo "成功生成Jiwei\\{$packageName}\\Sdk\\Option类" . PHP_EOL;
...@@ -35,7 +35,8 @@ ...@@ -35,7 +35,8 @@
} }
}, },
"bin": [ "bin": [
"bin/easysdk" "bin/easysdk",
"bin/easysdk-option"
], ],
"scripts": { "scripts": {
"phpstan": "vendor/bin/phpstan analyse -c phpstan.neon", "phpstan": "vendor/bin/phpstan analyse -c phpstan.neon",
......
<?php
declare(strict_types=1);
namespace Jiwei\EasyHttpSdk\Generation;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
class SdkOptionBuilder
{
/** @var string 目标Endpoint目录 */
private $endpointDstPath;
/** @var string twig模板目录 */
private $twigPath;
/** @var Environment twig生成器 */
protected $twig;
/** @var string 包命名空间 */
protected $package;
/**
* @param string $endpointDstPath
* @param string $twigPath
* @param string $package
*/
public function __construct(string $endpointDstPath, string $twigPath, string $package)
{
$this->endpointDstPath = $endpointDstPath;
$this->twigPath = $twigPath;
$this->package = $package;
$loader = new FilesystemLoader($this->twigPath);
$this->twig = new Environment($loader);
}
/**
* 根据配置创建Endpoint类
*
* @return void
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function build(): void
{
$phpCode = $this->twig->render('Option.temp', [
"Name" => $this->package
]);
file_put_contents($this->endpointDstPath . $this->package . "Option.php", $phpCode);
}
}
{% autoescape false %}
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Jiwei\EasyHttpSdk; namespace Jiwei\{{ Name }}\SDK;
use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Request;
use Jiwei\EasyHttpSdk\Option;
use Jiwei\EasyHttpSdk\Policy\DefaultErrorHandlingPolicy; use Jiwei\EasyHttpSdk\Policy\DefaultErrorHandlingPolicy;
use Jiwei\EasyHttpSdk\Policy\HandlingPolicyInterface; use Jiwei\EasyHttpSdk\Policy\HandlingPolicyInterface;
class XXXSDKOption extends Option class {{ Name }}Option extends Option
{ {
/** @var string API鉴权地址 */
private const AUTH_API_ROUTE = "/api/access/token"; private const AUTH_API_ROUTE = "/api/access/token";
/** @var int API鉴权过期时间 */
const AUTH_CACHE_EXPIRES_AT = 30000; const AUTH_CACHE_EXPIRES_AT = 30000;
/** @var array<string, string> API HOST */
const ENDPONIT_HOSTS = [ const ENDPONIT_HOSTS = [
'local' => 'localhost:8080', 'local' => '',
'development' => 'localhost:8080', 'development' => '',
'production' => 'localhost:8080' 'production' => ''
]; ];
/** /**
...@@ -38,6 +43,7 @@ class XXXSDKOption extends Option ...@@ -38,6 +43,7 @@ class XXXSDKOption extends Option
/** /**
* 错误处理策略 * 错误处理策略
*
* @return HandlingPolicyInterface * @return HandlingPolicyInterface
*/ */
public function errorHandlingPolicy(): HandlingPolicyInterface public function errorHandlingPolicy(): HandlingPolicyInterface
......
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