Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Easy Http SDK
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Back End
Easy Http SDK
Commits
c5446821
Commit
c5446821
authored
Feb 21, 2023
by
谢宇轩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 支持PHP8版本的模板
parent
f649e58e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
4 deletions
+96
-4
easysdk
bin/easysdk
+10
-2
SdkEndpointBuilder.php
src/Generation/SdkEndpointBuilder.php
+7
-2
Endpoint70000.temp
template/Endpoint70000.temp
+0
-0
Endpoint80000.temp
template/Endpoint80000.temp
+79
-0
No files found.
bin/easysdk
View file @
c5446821
...
...
@@ -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
();
...
...
src/Generation/SdkEndpointBuilder.php
View file @
c5446821
...
...
@@ -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
)
{
...
...
template/Endpoint.temp
→
template/Endpoint
70000
.temp
View file @
c5446821
File moved
template/Endpoint80000.temp
0 → 100644
View file @
c5446821
{% 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
%
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment