Skip to content

Commit 640bb1d

Browse files
author
fenghaikun
committed
初始化提交
1 parent 633fffc commit 640bb1d

File tree

16 files changed

+703
-0
lines changed

16 files changed

+703
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/nbproject/private/
2+
/nbproject/

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "sixdec/sixdec-sdk-php",
3+
"description": "sixdec sixdec-sdk-php",
4+
"type": "library",
5+
"license": "Apache License 2.0",
6+
"authors": [
7+
{
8+
"name": "sixdec",
9+
"homepage": "https://www.sixdec.com"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"sixdec\\SDK\\":"src"
15+
}
16+
}
17+
}

src/AbstractDTO.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of eelly package.
6+
*
7+
* (c) eelly.com
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace sixdec\SDK;
14+
15+
/**
16+
* @author hehui<[email protected]>
17+
*/
18+
class AbstractDTO implements \JsonSerializable
19+
{
20+
final public function __construct()
21+
{
22+
}
23+
24+
/**
25+
* 数组转对象
26+
*
27+
* @param array $array
28+
*/
29+
public static function hydractor(array $array)
30+
{
31+
$class = static::class;
32+
$object = new $class();
33+
foreach ($array as $key => $value) {
34+
$object->$key = $value;
35+
}
36+
37+
return $object;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*
43+
* @see JsonSerializable::jsonSerialize()
44+
*/
45+
public function jsonSerialize()
46+
{
47+
return $this;
48+
}
49+
}

src/EellyClient.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of eelly package.
6+
*
7+
* (c) eelly.com
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace sixdec\SDK;
14+
15+
use Eelly\OAuth2\Client\Provider\EellyProvider;
16+
use GuzzleHttp\Psr7\MultipartStream;
17+
use Psr\Http\Message\UploadedFileInterface;
18+
19+
class EellyClient
20+
{
21+
private const URI = [
22+
'logger' => 'http://api.eelly.dev',
23+
'member' => 'http://api.eelly.dev',
24+
'oauth' => 'http://api.eelly.dev',
25+
];
26+
27+
/**
28+
* @var string
29+
*/
30+
public static $traceId;
31+
32+
private static $services = [];
33+
34+
/**
35+
* @var EellyProvider
36+
*/
37+
private $provider;
38+
39+
/**
40+
* @var EellyClient
41+
*/
42+
private static $self;
43+
44+
/**
45+
* @param array $options
46+
*/
47+
final private function __construct(array $options)
48+
{
49+
$this->provider = new EellyProvider($options);
50+
}
51+
52+
/**
53+
* @param array $options
54+
*
55+
* @return self
56+
*/
57+
public static function init(array $options): self
58+
{
59+
if (null === self::$self) {
60+
self::$self = new self($options);
61+
}
62+
63+
return self::$self;
64+
}
65+
66+
/**
67+
* @return \Eelly\OAuth2\Client\Provider\EellyProvider
68+
*/
69+
public function getProvider()
70+
{
71+
return $this->provider;
72+
}
73+
74+
/**
75+
* @param string $traceId
76+
*/
77+
public function setTraceId(string $traceId): void
78+
{
79+
self::$traceId = $traceId;
80+
}
81+
82+
/**
83+
* @param mixed $grant
84+
*
85+
* @return \League\OAuth2\Client\Token\AccessToken
86+
*/
87+
public function getAccessToken($grant, array $options = [])
88+
{
89+
// TODO cache
90+
return $this->provider->getAccessToken($grant, $options);
91+
}
92+
93+
/**
94+
* @param string $uri
95+
* @param string $method
96+
* @param mixed ...$args
97+
*
98+
* @return mixed
99+
*/
100+
public static function request(string $uri, string $method, ...$args)
101+
{
102+
if (null === self::$self) {
103+
throw new \ErrorException('uninitial eelly client');
104+
}
105+
$client = self::$self;
106+
$accessToken = $client->getAccessToken('client_credentials');
107+
108+
list($serviceName) = explode('/', $uri);
109+
$uri = self::URI[$serviceName].'/'.$uri.'/'.$method;
110+
$stream = new MultipartStream($client->paramsToMultipart($args));
111+
$provider = $client->getProvider();
112+
$options = [
113+
'body' => $stream,
114+
];
115+
if (!empty(self::$traceId)) {
116+
$options['headers'] = [
117+
'traceId' => self::$traceId,
118+
];
119+
}
120+
$request = $provider->getAuthenticatedRequest(EellyProvider::METHOD_POST, $uri, $accessToken->getToken(), $options);
121+
122+
$response = $provider->getResponse($request);
123+
$class = $response->getHeader('ReturnType');
124+
if (!empty($class)) {
125+
$returnType = $class[0];
126+
if (class_exists($returnType)) {
127+
$array = json_decode((string) $response->getBody(), true);
128+
$object = $returnType::hydractor($array);
129+
} elseif ('array' == $returnType) {
130+
$object = json_decode((string) $response->getBody(), true);
131+
} else {
132+
$object = (string) $response->getBody();
133+
settype($object, $returnType);
134+
}
135+
} else {
136+
$object = (string) $response->getBody();
137+
}
138+
139+
return $object;
140+
}
141+
142+
protected function paramsToMultipart($params, $prefix = null)
143+
{
144+
$multipart = [];
145+
foreach ($params as $key => $value) {
146+
$p = null == $prefix ? $key : $prefix.'['.$key.']';
147+
if ($value instanceof UploadedFileInterface) {
148+
$multipart[] = [
149+
'name' => $p,
150+
'contents' => $value->getStream(),
151+
];
152+
} elseif (is_array($value)) {
153+
$parentMultipart = $this->paramsToMultipart($value, $p);
154+
foreach ($parentMultipart as $part) {
155+
$multipart[] = $part;
156+
}
157+
} else {
158+
$multipart[] = [
159+
'name' => $p,
160+
'contents' => $value,
161+
];
162+
}
163+
}
164+
165+
return $multipart;
166+
}
167+
}

src/Logger/Api/ApiLogger.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of eelly package.
6+
*
7+
* (c) eelly.com
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace sixdec\SDK\Logger\Api;
14+
15+
use sixdec\SDK\EellyClient;
16+
use sixdec\SDK\Logger\Service\ApiLoggerInterface;
17+
18+
/**
19+
* @author hehui<[email protected]>
20+
*/
21+
class ApiLogger implements ApiLoggerInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*
26+
* @see \Eelly\SDK\Logger\Service\ApiLoggerInterface::log()
27+
*/
28+
public function log(string $traceId, array $request = [], array $response = [], array $extras = []): void
29+
{
30+
EellyClient::request('logger/apiLogger', __FUNCTION__, $traceId, $request, $response, $extras);
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of eelly package.
6+
*
7+
* (c) eelly.com
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace sixdec\SDK\Logger\Service;
14+
15+
/**
16+
* @author hehui<[email protected]>
17+
*/
18+
interface ApiLoggerInterface
19+
{
20+
/**
21+
* @param string $traceId 跟踪id
22+
* @param array $request 请求信息
23+
* @param array $response 返回信息
24+
* @param array $extras 扩展的信息
25+
*/
26+
public function log(string $traceId, array $request = [], array $response = [], array $extras = []): void;
27+
}

0 commit comments

Comments
 (0)