Skip to content

Commit 7773467

Browse files
author
子旭 苏
committedAug 16, 2021
PHP8.0 adjustment
1 parent c30127c commit 7773467

5 files changed

+27
-20
lines changed
 

‎composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"rpc"
88
],
99
"require" : {
10-
"php" : ">=7.4",
10+
"php" : ">=8.0",
1111
"swango/environment" : "^1.0",
1212
"swango/stdlib" : "^1.2",
1313
"swango/aliyun-slb" : "^1.0"

‎src/Client.php

+18-11
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ abstract protected function getServiceName(): string;
1111
* @return string
1212
*/
1313
abstract protected function getMethodName(): string;
14-
public function setParameters($parameters): Client {
15-
if (! is_array($parameters) && ! is_object($parameters)) {
16-
throw new \Exception('Only accept array or object');
14+
public function setParameters(array|object $parameters): Client {
15+
$this->parameters = (array)$parameters;
16+
return $this;
17+
}
18+
public function withParameter(string $key, mixed $parameter): Client {
19+
$this->parameters ??= [];
20+
$this->parameters[$key] = $parameter;
21+
return $this;
22+
}
23+
public function withoutParameter(string $key): Client {
24+
if (isset($this->parameters) && isset($this->parameters[$key])) {
25+
unset($this->parameters[$key]);
1726
}
18-
$this->parameters = $parameters;
1927
return $this;
2028
}
2129
protected function getExceptionName(): array {
@@ -82,12 +90,7 @@ public function getResult(): ?object {
8290
}
8391
$response = $this->recv();
8492
if (503 === $response->statusCode) {
85-
try {
86-
\Swango\Aliyun\Slb\Scene\FindServerByServerName::find($this->getServiceName());
87-
throw new Client\Exception\ApiErrorException(static::class . ' api code error :503');
88-
} catch (\Swango\Aliyun\Slb\Exception\ServerNotAvailableException $e) {
89-
throw new Client\Exception\ServerClosedException();
90-
}
93+
throw new Client\Exception\ServerClosedException();
9194
}
9295
$result_object = \Json::decodeAsObject($response->body);
9396
if (! isset($result_object->code) || ! isset($result_object->enmsg) || ! isset($result_object->cnmsg)) {
@@ -97,7 +100,11 @@ public function getResult(): ?object {
97100
throw new Client\Exception\ApiErrorException('Invalid response format');
98101
}
99102
if (200 !== $result_object->code || 'ok' !== $result_object->enmsg) {
100-
throw new Client\Exception\ApiErrorException("[$result_object->code] $result_object->enmsg $result_object->cnmsg");
103+
if (Environment::getSwangoModuleSeeker()->swangoModelExists()) {
104+
throw new \ExceptionToResponse($result_object->enmsg, $result_object->cnmsg, $result_object->code);
105+
} else {
106+
throw new Client\Exception\ApiErrorException("[$result_object->code] $result_object->enmsg $result_object->cnmsg");
107+
}
101108
}
102109
return $result_object->data ?? null;
103110
}

‎src/Exception/ApiErrorException.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<?php
2-
namespace Swango\Rpc\Client\Exception;
3-
class ApiErrorException extends \ApiErrorException {
4-
const supplier = '内部服务';
1+
<?php
2+
namespace Swango\Rpc\Client\Exception;
3+
class ApiErrorException extends \ApiErrorException {
4+
const supplier = '内部服务';
55
}

‎src/Exception/ApiTimeoutException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<?php
2-
namespace Swango\Rpc\Client\Exception;
1+
<?php
2+
namespace Swango\Rpc\Client\Exception;
33
class ApiTimeoutException extends UnknownResultException {}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<?php
2-
namespace Swango\Rpc\Client\Exception;
1+
<?php
2+
namespace Swango\Rpc\Client\Exception;
33
class UnknownResultException extends \Exception {}

0 commit comments

Comments
 (0)
Please sign in to comment.