This commit is contained in:
2020-12-17 14:09:14 +08:00
parent 672a719dbd
commit 36c1d0502a
151 changed files with 1937 additions and 2848 deletions
+11 -11
View File
@@ -10,7 +10,7 @@ declare(strict_types=1);
namespace HttpServer\Client;
use Exception;
use Swoole\Coroutine;
use JetBrains\PhpStorm\Pure;
use Swoole\Coroutine\Http\Client as SClient;
/**
@@ -22,17 +22,17 @@ class Client extends ClientAbstracts
/**
* @param string $method
* @param $url
* @param array $data
* @return array|mixed|Result
* @param $path
* @param array $params
* @return array|string|Result
* @throws Exception
*/
public function request(string $method, $url, $data = [])
public function request(string $method, $path, $params = []): array|string|Result
{
return $this->setMethod($method)
->coroutine(
$this->matchHost($url),
$this->paramEncode($data)
$this->matchHost($path),
$this->paramEncode($params)
);
}
@@ -40,11 +40,11 @@ class Client extends ClientAbstracts
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @return array|string|Result
* @throws Exception
* 使用swoole协程方式请求
*/
private function coroutine($url, $data = [])
private function coroutine($url, $data = []): array|string|Result
{
try {
$client = $this->generate_client($data, ...$url);
@@ -77,7 +77,7 @@ class Client extends ClientAbstracts
* @param $path
* @return SClient
*/
private function generate_client($data, $host, $isHttps, $path)
private function generate_client($data, $host, $isHttps, $path): SClient
{
if ($isHttps || $this->isSSL()) {
$client = new SClient($host, 443, $this->isSSL());
@@ -112,7 +112,7 @@ class Client extends ClientAbstracts
/**
* @return array
*/
private function settings()
#[Pure] private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
+95 -104
View File
@@ -6,6 +6,7 @@ namespace HttpServer\Client;
use Closure;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Snowflake\Core\Help;
use Snowflake\Core\JSON;
@@ -57,33 +58,32 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @return static
*/
public static function NewRequest()
#[Pure] public static function NewRequest(): static
{
return new static();
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @param $path
* @param array $params
* @return array|int|string|Result
* @throws
*/
public function post($url, $data = [])
public function post(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::POST, $url, $data);
return $this->request(self::POST, $path, $params);
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws
* @param string $path
* @param array $params
* @return array|int|string|Result
*/
public function put($url, $data = [])
public function put(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::PUT, $url, $data);
return $this->request(self::PUT, $path, $params);
}
@@ -99,54 +99,50 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param string $path
* @param array $params
* @return mixed
* @return array|int|string|Result
*/
public function head(string $path, array $params = [])
public function head(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::HEAD, $path, $params);
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws
* @param string $path
* @param array $params
* @return array|int|string|Result
*/
public function get($url, $data = [])
public function get(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::GET, $url, $data);
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws Exception
*/
public function option($url, $data = [])
{
return $this->request(self::OPTIONS, $url, $data);
}
/**
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws Exception
*/
public function delete($url, $data = [])
{
return $this->request(self::DELETE, $url, $data);
return $this->request(self::GET, $path, $params);
}
/**
* @param string $path
* @param array $params
* @return mixed
* @throws Exception
* @return array|int|string|Result
*/
public function options(string $path, array $params = [])
public function option(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::OPTIONS, $path, $params);
}
/**
* @param string $path
* @param array $params
* @return array|int|string|Result
*/
public function delete(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::DELETE, $path, $params);
}
/**
* @param string $path
* @param array $params
* @return array|int|string|Result
*/
public function options(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::OPTIONS, $path, $params);
@@ -155,10 +151,9 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param string $path
* @param array $params
* @return mixed
* @throws Exception
* @return array|int|string|Result
*/
public function upload(string $path, array $params = [])
public function upload(string $path, array $params = []): array|int|string|Result
{
return $this->request(self::UPLOAD, $path, $params);
}
@@ -175,7 +170,7 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @return int
*/
protected function getHostPort()
protected function getHostPort(): int
{
if (!empty($this->getPort())) {
return $this->getPort();
@@ -196,10 +191,6 @@ abstract class ClientAbstracts extends Component implements IClient
$this->host = System::gethostbyname($host);
}
$this->addHeader('Host', $host);
//
// if (!preg_match('/(\d{1,3}\.){4}/', $host . '.')) {
// } else {
// }
}
/**
@@ -220,15 +211,15 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param array $headers
* @param array $header
* @return array
*/
public function setHeaders(array $headers)
public function setHeaders(array $header): array
{
if (empty($headers)) {
if (empty($header)) {
return [];
}
foreach ($headers as $key => $val) {
foreach ($header as $key => $val) {
$this->header[$key] = $val;
}
return $this->header;
@@ -252,11 +243,11 @@ abstract class ClientAbstracts extends Component implements IClient
}
/**
* @param int $timeout
* @param int $value
*/
public function setTimeout(int $timeout): void
public function setTimeout(int $value): void
{
$this->timeout = $timeout;
$this->timeout = $value;
}
/**
@@ -268,11 +259,11 @@ abstract class ClientAbstracts extends Component implements IClient
}
/**
* @param Closure|null $callback
* @param Closure|null $value
*/
public function setCallback(?Closure $callback): void
public function setCallback(?Closure $value): void
{
$this->callback = $callback;
$this->callback = $value;
}
/**
@@ -284,12 +275,12 @@ abstract class ClientAbstracts extends Component implements IClient
}
/**
* @param string $method
* @param string $value
* @return $this
*/
public function setMethod(string $method): self
public function setMethod(string $value): self
{
$this->method = $method;
$this->method = $value;
return $this;
}
@@ -414,17 +405,17 @@ abstract class ClientAbstracts extends Component implements IClient
}
/**
* @param string $ca
* @param string $ssl_key_file
*/
public function setCa(string $ca): void
public function setCa(string $ssl_key_file): void
{
$this->ca = $ca;
$this->ca = $ssl_key_file;
}
/**
* @return int
*/
public function getPort(): int
#[Pure] public function getPort(): int
{
if ($this->isSSL()) {
return 443;
@@ -496,7 +487,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $host
* @return string|string[]
*/
protected function replaceHost($host)
protected function replaceHost($host): array|string
{
if ($this->isHttp($host)) {
return str_replace('http://', '', $host);
@@ -512,7 +503,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url
* @return false|int
*/
protected function checkIsIp($url)
protected function checkIsIp($url): bool|int
{
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
}
@@ -521,18 +512,18 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url
* @return bool
*/
protected function isHttp($url)
#[Pure] protected function isHttp($url): bool
{
return strpos($url, 'http://') === 0;
return str_starts_with($url, 'http://');
}
/**
* @param $url
* @return bool
*/
protected function isHttps($url)
#[Pure] protected function isHttps($url): bool
{
return strpos($url, 'https://') === 0;
return str_starts_with($url, 'https://');
}
@@ -540,7 +531,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $newData
* @return mixed
*/
protected function mergeParams($newData)
protected function mergeParams($newData): mixed
{
if (empty($this->getData())) {
return $this->toRequest($newData);
@@ -559,9 +550,9 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param $data
* @return false|mixed|string
* @return string
*/
protected function toRequest($data)
protected function toRequest($data): string
{
if (is_string($data)) {
return $data;
@@ -572,9 +563,9 @@ abstract class ClientAbstracts extends Component implements IClient
} else if (isset($this->header['content-type'])) {
$contentType = $this->header['content-type'];
}
if (strpos($contentType, 'json') !== false) {
if (str_contains($contentType, 'json')) {
return Help::toJson($data);
} else if (strpos($contentType, 'xml') !== false) {
} else if (str_contains($contentType, 'xml')) {
return Help::toXml($data);
} else {
return http_build_query($data);
@@ -585,21 +576,21 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param $data
* @param $body
* @return mixed
* @return array
*/
protected function resolve($data, $body)
protected function resolve($data, $body): array
{
if (is_array($body)) {
return $body;
}
$type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html';
if (strpos($type, 'text/html') !== false) {
if (str_contains($type, 'text/html')) {
return $body;
} else if (strpos($type, 'json') !== false) {
} else if (str_contains($type, 'json')) {
return json_decode($body, true);
} else if (strpos($type, 'xml') !== false) {
} else if (str_contains($type, 'xml')) {
return Help::xmlToArray($body);
} else if (strpos($type, 'plain') !== false) {
} else if (str_contains($type, 'plain')) {
return Help::toArray($body);
}
return $body;
@@ -609,12 +600,12 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param $body
* @param $_data
* @param $header
* @param $statusCode
* @return array|mixed|Result
* @param array $header
* @param int $statusCode
* @return mixed 构建返回体
* 构建返回体
*/
protected function structure($body, $_data, $header = [], $statusCode = 200)
protected function structure($body, $_data, $header = [], $statusCode = 200): mixed
{
if ($this->callback instanceof Closure) {
$result = call_user_func($this->callback, $body, $_data, $header);
@@ -631,7 +622,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $statusCode
* @return Result
*/
private function parseResult($body, $header, $statusCode)
private function parseResult($body, $header, $statusCode): Result
{
if (is_string($body)) {
$result['code'] = 0;
@@ -649,9 +640,9 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param $body
* @return array|mixed|string
* @return mixed
*/
protected function searchMessageByData($body)
protected function searchMessageByData($body): mixed
{
$parent = [];
if (empty($this->errorMsgField)) {
@@ -682,7 +673,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @return bool
* check isPost Request
*/
public function isPost()
#[Pure] public function isPost(): bool
{
return strtolower($this->method) === self::POST;
}
@@ -693,7 +684,7 @@ abstract class ClientAbstracts extends Component implements IClient
*
* check isGet Request
*/
public function isGet()
#[Pure] public function isGet(): bool
{
return strtolower($this->method) === self::GET;
}
@@ -704,7 +695,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @return array|string
* 将请求参数进行编码
*/
protected function paramEncode($arr)
#[Pure] protected function paramEncode($arr): array|string
{
if (!is_array($arr)) {
return $arr;
@@ -722,15 +713,15 @@ abstract class ClientAbstracts extends Component implements IClient
/**
* @param string $string
* @return array|string[]
* @return array
*/
protected function matchHost(string $string)
protected function matchHost(string $string): array
{
if (($parse = isUrl($string, true)) === false) {
return $this->defaultString($string);
}
[$isHttps, $domain, $port, $path] = $parse;
if (strpos($domain, ':' . $port) !== false) {
if (str_contains($domain, ':' . $port)) {
$domain = str_replace(':' . $port, '', $domain);
}
$this->port = $isHttps ? 443 : $this->port;
@@ -753,7 +744,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $string
* @return array
*/
private function defaultString($string)
private function defaultString($string): array
{
$host = $this->getHost();
if ($string == '/') {
@@ -770,7 +761,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $params
* @return string
*/
protected function joinGetParams($path, $params)
#[Pure] protected function joinGetParams($path, $params): string
{
if (empty($params)) {
return $path;
@@ -778,7 +769,7 @@ abstract class ClientAbstracts extends Component implements IClient
if (!is_string($params)) {
$params = http_build_query($params);
}
if (strpos($path, '?') !== false) {
if (str_contains($path, '?')) {
[$path, $getParams] = explode('?', $path);
}
if (!isset($getParams) || empty($getParams)) {
@@ -795,7 +786,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $header
* @return Result
*/
protected function fail($code, $message, $data = [], $header = [])
protected function fail($code, $message, $data = [], $header = []): Result
{
return new Result([
'code' => $code,
+15 -14
View File
@@ -5,6 +5,7 @@ namespace HttpServer\Client;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Curl
@@ -14,13 +15,13 @@ class Curl extends ClientAbstracts
{
/**
* @param $path
* @param $method
* @param $path
* @param array $params
* @return bool|string
* @return Result|bool|array|string
* @throws Exception
*/
public function request($method, $path, $params = [])
public function request($method, $path, $params = []): Result|bool|array|string
{
if ($method == self::GET) {
$path = $this->joinGetParams($path, $params);
@@ -33,10 +34,10 @@ class Curl extends ClientAbstracts
* @param $path
* @param $method
* @param $params
* @return mixed|resource
* @return mixed
* @throws Exception
*/
private function getCurlHandler($path, $method, $params)
private function getCurlHandler($path, $method, $params): mixed
{
[$host, $isHttps, $path] = $this->matchHost($path);
@@ -71,7 +72,7 @@ class Curl extends ClientAbstracts
* @return bool
* @throws Exception
*/
private function curlHandlerSslSet($resource)
private function curlHandlerSslSet($resource): bool
{
if (!empty($this->ssl_key)) {
if (!file_exists($this->ssl_key)) {
@@ -129,10 +130,10 @@ class Curl extends ClientAbstracts
/**
* @param $curl
* @return bool|string
* @return Result|bool|array|string
* @throws Exception
*/
private function execute($curl)
private function execute($curl): Result|bool|array|string
{
$output = curl_exec($curl);
if ($output === false) {
@@ -146,10 +147,10 @@ class Curl extends ClientAbstracts
* @param $curl
* @param $output
* @param array $params
* @return array|Result|mixed
* @return mixed
* @throws Exception
*/
private function parseResponse($curl, $output, $params = [])
private function parseResponse($curl, $output, $params = []): mixed
{
curl_close($curl);
if ($output === FALSE) {
@@ -170,9 +171,9 @@ class Curl extends ClientAbstracts
* @return array
* @throws Exception
*/
private function explode($output)
private function explode($output): array
{
if (empty($output) || strpos($output, "\r\n\r\n") === false) {
if (empty($output) || !str_contains($output, "\r\n\r\n")) {
throw new Exception('Get data null.');
}
@@ -193,7 +194,7 @@ class Curl extends ClientAbstracts
* @param $headers
* @return array
*/
private function headerFormat($headers)
private function headerFormat($headers): array
{
$_tmp = [];
foreach ($headers as $key => $val) {
@@ -208,7 +209,7 @@ class Curl extends ClientAbstracts
/**
* @return array
*/
private function parseHeaderMat()
#[Pure] private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->getHeader() as $key => $val) {
+9 -9
View File
@@ -25,10 +25,10 @@ class Http2 extends Component
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @return Result
* @throws Exception
*/
public function get($domain, $path, $params = [], $timeout = -1)
public function get($domain, $path, $params = [], $timeout = -1): Result
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'GET', $params));
@@ -41,10 +41,10 @@ class Http2 extends Component
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @return Result
* @throws Exception
*/
public function post($domain, $path, $params = [], $timeout = -1)
public function post($domain, $path, $params = [], $timeout = -1): Result
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'POST', $params));
@@ -57,10 +57,10 @@ class Http2 extends Component
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @return Result
* @throws Exception
*/
public function delete($domain, $path, $params = [], $timeout = -1)
public function delete($domain, $path, $params = [], $timeout = -1): Result
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'DELETE', $params));
@@ -76,7 +76,7 @@ class Http2 extends Component
* @return mixed
* @throws Exception
*/
public function put($domain, $path, $params = [], $timeout = -1)
public function put($domain, $path, $params = [], $timeout = -1): Result
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'PUT', $params));
@@ -92,7 +92,7 @@ class Http2 extends Component
* @return Request
* @throws Exception
*/
public function getRequest($domain, $path, $method, $params)
public function getRequest($domain, $path, $method, $params): Request
{
if (Context::hasContext($domain . $path)) {
$req = Context::getContext($domain . $path);
@@ -123,7 +123,7 @@ class Http2 extends Component
* @return H2Client
* @throws Exception
*/
private function getClient($domain, $path, $timeout = -1)
private function getClient($domain, $path, $timeout = -1): H2Client
{
if (Context::hasContext($domain)) {
return Context::getContext($domain);
+2 -1
View File
@@ -4,6 +4,7 @@
namespace HttpServer\Client;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
use Swoole\Coroutine;
@@ -28,7 +29,7 @@ class HttpClient extends Component
/**
* @return Http2
*/
public static function http2()
#[Pure] public static function http2(): Http2
{
return Snowflake::app()->http2;
}
+2 -2
View File
@@ -44,7 +44,7 @@ class HttpParse
* @return string
* @throws Exception
*/
public static function parse($data)
public static function parse($data): string
{
$tmp = [];
if (is_string($data)) {
@@ -65,7 +65,7 @@ class HttpParse
* @return string
* @throws Exception
*/
private static function ifElse($t, $qt)
private static function ifElse($t, $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;
+14 -13
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace HttpServer\Client;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Result
@@ -45,7 +46,7 @@ class Result
* @param $data
* @return $this
*/
public function setAssignment($data)
public function setAssignment($data): static
{
foreach ($data as $key => $val) {
if (!property_exists($this, $key)) {
@@ -59,9 +60,9 @@ class Result
/**
* @param $name
* @return mixed|null
* @return mixed
*/
public function __get($name)
public function __get($name): mixed
{
return $this->$name;
}
@@ -69,9 +70,9 @@ class Result
/**
* @param $name
* @param $value
* @return $this|void
* @return $this
*/
public function __set($name, $value)
public function __set($name, $value): static
{
$this->$name = $value;
@@ -81,7 +82,7 @@ class Result
/**
* @return array
*/
public function getHeaders()
public function getHeaders(): array
{
$_tmp = [];
if (!is_array($this->header)) {
@@ -103,7 +104,7 @@ class Result
/**
* @return array
*/
public function getTime()
public function getTime(): array
{
return [
'startTime' => $this->startTime,
@@ -118,7 +119,7 @@ class Result
* @return $this
* @throws Exception
*/
public function setAttr($key, $data)
public function setAttr($key, $data): static
{
if (!property_exists($this, $key)) {
throw new Exception('未查找到相应对象属性');
@@ -131,7 +132,7 @@ class Result
* @param int $status
* @return bool
*/
public function isResultsOK($status = 0)
public function isResultsOK($status = 0): bool
{
if (!$this->httpIsOk()) {
return false;
@@ -142,7 +143,7 @@ class Result
/**
* @return bool
*/
public function httpIsOk()
#[Pure] public function httpIsOk(): bool
{
return in_array($this->httpStatus, [100, 101, 200, 201, 202, 203, 204, 205, 206]);
}
@@ -150,7 +151,7 @@ class Result
/**
* @return mixed
*/
public function getBody()
public function getBody(): mixed
{
return $this->data;
}
@@ -158,7 +159,7 @@ class Result
/**
* @return mixed
*/
public function getMessage()
public function getMessage(): mixed
{
return $this->message;
}
@@ -166,7 +167,7 @@ class Result
/**
* @return mixed
*/
public function getCode()
public function getCode(): mixed
{
return $this->code;
}