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
+1 -1
View File
@@ -86,7 +86,7 @@ abstract class Callback extends Application
* @throws \PHPMailer\PHPMailer\Exception
* @throws ConfigException
*/
private function createEmail()
private function createEmail(): PHPMailer
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
+1 -1
View File
@@ -25,7 +25,7 @@ abstract class ServerBase extends Application
/**
* @return Server
*/
public function getServer()
public function getServer(): Server
{
return $this->server;
}
+4 -3
View File
@@ -5,6 +5,7 @@ namespace HttpServer;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Input;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
@@ -87,7 +88,7 @@ trait Action
/**
* WorkerId Iterator
*/
private function masterIdCheck()
private function masterIdCheck(): bool
{
echo '.';
$files = new \DirectoryIterator($this->getWorkerPath());
@@ -110,7 +111,7 @@ trait Action
/**
* @return string
*/
private function getWorkerPath()
#[Pure] private function getWorkerPath(): string
{
return "glob://" . ltrim(APP_PATH, '/') . '/storage/worker/*.sock';
}
@@ -120,7 +121,7 @@ trait Action
* @param $port
* @return bool|array
*/
private function isUse($port)
private function isUse($port): bool|array
{
if (empty($port)) {
return false;
+8 -8
View File
@@ -28,23 +28,23 @@ class Application extends HttpService
}
/**
* @param $methods
* @param $name
* @return mixed
* @throws Exception
*/
public function __get($methods)
public function __get($name): mixed
{
if (method_exists($this, $methods)) {
return $this->{$methods}();
if (method_exists($this, $name)) {
return $this->{$name}();
}
$handler = 'get' . ucfirst($methods);
$handler = 'get' . ucfirst($name);
if (method_exists($this, $handler)) {
return $this->{$handler}();
}
if (property_exists($this, $methods)) {
return $this->$methods;
if (property_exists($this, $name)) {
return $this->$name;
}
$message = sprintf('method %s::%s not exists.', get_called_class(), $methods);
$message = sprintf('method %s::%s not exists.', get_called_class(), $name);
throw new Exception($message);
}
+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;
}
+3 -3
View File
@@ -27,11 +27,11 @@ class Command extends \Console\Command
/**
* @param Input $dtl
* @return mixed|void
* @return string
* @throws Exception
* @throws ConfigException
*/
public function onHandler(Input $dtl)
public function onHandler(Input $dtl): string
{
$manager = Snowflake::app()->server;
$manager->setDaemon($dtl->get('daemon', 0));
@@ -48,7 +48,7 @@ class Command extends \Console\Command
if ($dtl->get('action') == 'stop') {
return 'shutdown success.';
}
$manager->start();
return $manager->start();
}
}
+6 -10
View File
@@ -15,8 +15,6 @@ use Exception;
use HttpServer\Route\Router;
use Kafka\Producer;
use Snowflake\Abstracts\BaseGoto;
use Snowflake\Annotation\Annotation;
use Snowflake\Cache\Memcached;
use Snowflake\Cache\Redis;
use Snowflake\Error\Logger;
use Snowflake\Event;
@@ -30,7 +28,6 @@ use Snowflake\Snowflake;
* Class WebController
* @package Snowflake\Snowflake\Web
* @property BaseGoto $goto
* @property Annotation $annotation
* @property Event $event
* @property Router $router
* @property SPool $pool
@@ -38,7 +35,6 @@ use Snowflake\Snowflake;
* @property Server $server
* @property DatabasesProviders $db
* @property Connection $connections
* @property Memcached $memcached
* @property Logger $logger
* @property Jwt $jwt
* @property Client $client
@@ -131,23 +127,23 @@ class Controller extends Application
/**
* @param $methods
* @param $name
* @return mixed
* @throws ComponentException
*/
public function __get($methods): mixed
public function __get($name): mixed
{
// TODO: Change the autogenerated stub
if (property_exists($this, $methods)) {
return $this->$methods;
if (property_exists($this, $name)) {
return $this->$name;
}
$method = 'get' . ucfirst($methods);
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->{$method}();
}
return Snowflake::app()->get($methods);
return Snowflake::app()->get($name);
}
-5
View File
@@ -6,10 +6,6 @@ namespace HttpServer\Events;
use Annotation\Route\Socket;
use HttpServer\Abstracts\Callback;
use HttpServer\Route\Annotation\Http;
use HttpServer\Route\Annotation\Tcp;
use HttpServer\Route\Annotation\Websocket;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use HttpServer\Route\Node;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
@@ -17,7 +13,6 @@ use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Server;
use Exception;
use Swoole\Http\Server as HServer;
use Swoole\WebSocket\Server as WServer;
/**
+4
View File
@@ -8,6 +8,10 @@ use Exception;
use HttpServer\Abstracts\Callback;
use Swoole\Server;
/**
* Class OnFinish
* @package HttpServer\Events
*/
class OnFinish extends Callback
{
/**
+4 -5
View File
@@ -7,7 +7,6 @@ namespace HttpServer\Events;
use Annotation\Route\Socket;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
@@ -62,7 +61,7 @@ class OnHandshake extends Callback
* @param int $code
* @return false
*/
private function disconnect($response, $code = 500)
private function disconnect($response, $code = 500): bool
{
$response->status($code);
$response->end();
@@ -73,10 +72,10 @@ class OnHandshake extends Callback
/**
* @param SRequest $request
* @param SResponse $response
* @return mixed|void
* @throws Exception
* @return void
* @throws ComponentException
*/
public function onHandler(SRequest $request, SResponse $response)
public function onHandler(SRequest $request, SResponse $response): void
{
Coroutine::defer(function () {
fire(Event::EVENT_AFTER_REQUEST);
+2 -3
View File
@@ -4,12 +4,11 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Coroutine\System;
use Swoole\Process;
use Swoole\Server;
class OnManagerStart extends Callback
@@ -17,7 +16,7 @@ class OnManagerStart extends Callback
/**
* @param Server $server
* @throws \Exception
* @throws Exception
*/
public function onHandler(Server $server)
{
+2 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use Snowflake\Event;
use Snowflake\Snowflake;
@@ -18,7 +19,7 @@ class OnManagerStop extends Callback
/**
* @param $server
* @throws \Exception
* @throws Exception
*/
public function onHandler(Server $server)
{
+1 -2
View File
@@ -6,7 +6,6 @@ namespace HttpServer\Events;
use Closure;
use HttpServer\Abstracts\Callback;
use Snowflake\Core\JSON;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Server;
@@ -36,7 +35,7 @@ class OnPacket extends Callback
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, string $data, array $clientInfo)
public function onHandler(Server $server, string $data, array $clientInfo): mixed
{
try {
$data = DataResolve::unpack($this->unpack, $clientInfo['address'], $clientInfo['port'], $data);
+1 -1
View File
@@ -36,7 +36,7 @@ class OnReceive extends Callback
* @return mixed
* @throws Exception
*/
public function onHandler(\Swoole\Server $server, int $fd, int $reID, string $data)
public function onHandler(\Swoole\Server $server, int $fd, int $reID, string $data): mixed
{
try {
$client = $server->getClientInfo($fd, $reID);
-4
View File
@@ -7,13 +7,9 @@ namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Exception\ExitException;
use HttpServer\Http\Context;
use HttpServer\Http\Request as HRequest;
use HttpServer\Http\Response as HResponse;
use HttpServer\Route\Node;
use HttpServer\Service\Http;
use ReflectionException;
use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
-8
View File
@@ -6,18 +6,10 @@ namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Server;
use Closure;
/**
* Class OnShutdown
+2 -3
View File
@@ -4,12 +4,11 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use Snowflake\Abstracts\Config;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Coroutine\System;
use Swoole\Process;
use Swoole\Server;
class OnStart extends Callback
@@ -17,7 +16,7 @@ class OnStart extends Callback
/**
* @param Server $server
* @throws \Exception
* @throws Exception
*/
public function onHandler(Server $server)
{
-5
View File
@@ -6,15 +6,10 @@ namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use HttpServer\Service\Http;
use HttpServer\Service\Websocket;
use Snowflake\Abstracts\Config;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Coroutine\System;
use Swoole\Server;
/**
+2 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Exception;
use HttpServer\Abstracts\Callback;
/**
@@ -17,7 +18,7 @@ class OnWorkerStop extends Callback
/**
* @param $server
* @param $worker_id
* @throws \Exception
* @throws Exception
*/
public function onHandler($server, $worker_id)
{
+2 -2
View File
@@ -65,11 +65,11 @@ class DataResolve
* @param $address
* @param $port
* @param $data
* @return array|mixed
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private static function callbackResolve($callback, $address, $port, $data)
private static function callbackResolve($callback, $address, $port, $data): mixed
{
if ($callback instanceof Closure) {
if (empty($address) && empty($port)) {
+10 -2
View File
@@ -9,6 +9,7 @@ declare(strict_types=1);
namespace HttpServer\Exception;
use JetBrains\PhpStorm\Pure;
use Throwable;
/**
@@ -17,8 +18,15 @@ use Throwable;
*/
class AuthException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = NULL)
/**
* AuthException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
#[Pure] public function __construct($message = "", $code = 0, Throwable $previous = NULL)
{
parent::__construct($message, 7000, $previous);
}
+2 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace HttpServer\Exception;
use JetBrains\PhpStorm\Pure;
use Throwable;
/**
@@ -19,7 +20,7 @@ class ExitException extends \Exception
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = "", $code = 0, Throwable $previous = null)
#[Pure] public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
+3 -3
View File
@@ -145,10 +145,10 @@ class Context extends BaseContext
}
/**
* @param $id
* @param null $key
* @param string $id
* @param string|null $key
*/
public static function deleteId($id, $key = null)
public static function deleteId(string $id,string $key = null)
{
if (!static::hasContext($id, $key)) {
return;
+2 -1
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace HttpServer\Http\Formatter;
use Exception;
use Snowflake\Core\JSON;
use HttpServer\Application;
use Swoole\Http\Response;
@@ -32,7 +33,7 @@ class HtmlFormatter extends Application implements IFormatter
/**
* @param $context
* @return $this
* @throws \Exception
* @throws Exception
*/
public function send($context): static
{
+2 -2
View File
@@ -49,9 +49,9 @@ class XmlFormatter extends Application implements IFormatter
}
/**
* @return mixed
* @return string|null
*/
public function getData(): mixed
public function getData(): ?string
{
$data = $this->data;
$this->clear();
+38 -35
View File
@@ -6,7 +6,10 @@ namespace HttpServer\Http;
use Exception;
use HttpServer\Application;
use HttpServer\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Core\JSON;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use function router;
@@ -77,7 +80,7 @@ class Request extends Application
/**
* @return bool
*/
public function isFavicon()
public function isFavicon(): bool
{
return $this->getUri() === 'favicon.ico';
}
@@ -85,7 +88,7 @@ class Request extends Application
/**
* @return mixed
*/
public function getIdentity()
public function getIdentity(): mixed
{
return $this->_grant;
}
@@ -93,7 +96,7 @@ class Request extends Application
/**
* @return bool
*/
public function isHead()
public function isHead(): bool
{
$result = $this->headers->getHeader('request_method') == 'head';
if ($result) {
@@ -108,7 +111,7 @@ class Request extends Application
* @param $status
* @return mixed
*/
public function setStatus($status)
public function setStatus($status): mixed
{
return $this->statusCode = $status;
}
@@ -116,7 +119,7 @@ class Request extends Application
/**
* @return int
*/
public function getStatus()
public function getStatus(): int
{
return $this->statusCode;
}
@@ -124,7 +127,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsPackage()
public function getIsPackage(): bool
{
return $this->headers->getHeader('request_method') == 'package';
}
@@ -132,7 +135,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsReceive()
public function getIsReceive(): bool
{
return $this->headers->getHeader('request_method') == 'receive';
}
@@ -150,7 +153,7 @@ class Request extends Application
/**
* @return bool
*/
public function hasGrant()
public function hasGrant(): bool
{
return $this->_grant !== null;
}
@@ -159,7 +162,7 @@ class Request extends Application
/**
* @return string
*/
public function parseUri()
public function parseUri(): string
{
$array = [];
$explode = explode('/', $this->headers->getHeader('request_uri'));
@@ -175,15 +178,15 @@ class Request extends Application
/**
* @return string[]
*/
public function getExplode()
public function getExplode(): array
{
return $this->explode;
}
/**
* @return mixed|string
* @return string
*/
public function getCurrent()
#[Pure] public function getCurrent(): string
{
return current($this->explode);
}
@@ -191,7 +194,7 @@ class Request extends Application
/**
* @return string
*/
public function getUri()
public function getUri(): string
{
if (!$this->headers) {
return 'command exec.';
@@ -207,10 +210,10 @@ class Request extends Application
/**
* @return mixed|string
* @throws Exception
* @return mixed
* @throws ComponentException
*/
public function adapter()
public function adapter(): mixed
{
if (!$this->isHead()) {
return router()->dispatch();
@@ -222,7 +225,7 @@ class Request extends Application
/**
* @return string|null
*/
public function getPlatform()
public function getPlatform(): ?string
{
$user = $this->headers->getHeader('user-agent');
$match = preg_match('/\(.*\)?/', $user, $output);
@@ -245,7 +248,7 @@ class Request extends Application
/**
* @return bool
*/
public function isIos()
public function isIos(): bool
{
return $this->getPlatform() == static::PLATFORM_IPHONE;
}
@@ -253,7 +256,7 @@ class Request extends Application
/**
* @return bool
*/
public function isAndroid()
public function isAndroid(): bool
{
return $this->getPlatform() == static::PLATFORM_ANDROID;
}
@@ -261,7 +264,7 @@ class Request extends Application
/**
* @return bool
*/
public function isMacOs()
public function isMacOs(): bool
{
return $this->getPlatform() == static::PLATFORM_MAC_OX;
}
@@ -269,7 +272,7 @@ class Request extends Application
/**
* @return bool
*/
public function isWindows()
public function isWindows(): bool
{
return $this->getPlatform() == static::PLATFORM_WINDOWS;
}
@@ -277,7 +280,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsPost()
public function getIsPost(): bool
{
return $this->getMethod() == 'post';
}
@@ -286,7 +289,7 @@ class Request extends Application
* @return bool
* @throws Exception
*/
public function getIsHttp()
public function getIsHttp(): bool
{
return true;
}
@@ -294,7 +297,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsOption()
public function getIsOption(): bool
{
return $this->getMethod() == 'options';
}
@@ -302,7 +305,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsGet()
public function getIsGet(): bool
{
return $this->getMethod() == 'get';
}
@@ -310,7 +313,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsDelete()
public function getIsDelete(): bool
{
return $this->getMethod() == 'delete';
}
@@ -320,7 +323,7 @@ class Request extends Application
*
* 获取请求类型
*/
public function getMethod()
public function getMethod(): string
{
$method = $this->headers->get('request_method');
if (empty($method)) {
@@ -332,7 +335,7 @@ class Request extends Application
/**
* @return bool
*/
public function getIsCli()
public function getIsCli(): bool
{
return $this->isCli === TRUE;
}
@@ -357,7 +360,7 @@ class Request extends Application
/**
* @return mixed|null
*/
public function getIp()
#[Pure] public function getIp()
{
$headers = $this->headers->getHeaders();
if (!empty($headers['x-forwarded-for'])) return $headers['x-forwarded-for'];
@@ -369,7 +372,7 @@ class Request extends Application
/**
* @return string
*/
public function getRuntime()
#[Pure] public function getRuntime(): string
{
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
}
@@ -377,7 +380,7 @@ class Request extends Application
/**
* @return string
*/
public function getDebug()
public function getDebug(): string
{
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
@@ -402,7 +405,7 @@ class Request extends Application
* @param $router
* @return bool
*/
public function is($router)
public function is($router): bool
{
return $this->getUri() == trim($router, '/');
}
@@ -410,7 +413,7 @@ class Request extends Application
/**
* @return bool
*/
public function isNotFound()
public function isNotFound(): bool
{
return JSON::to(404, 'Page ' . $this->getUri() . ' not found.');
}
@@ -419,10 +422,10 @@ class Request extends Application
/**
* @param $request
* @return mixed
* @throws \ReflectionException
* @throws ReflectionException
* @throws NotFindClassException
*/
public static function create($request)
public static function create($request): Request
{
$sRequest = Context::setContext('request', Snowflake::createObject(Request::class));
$sRequest->fd = $request->fd;
-257
View File
@@ -1,257 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\Route\Node;
use ReflectionClass;
use ReflectionException;
use Snowflake\Annotation\Annotation;
use Snowflake\Snowflake;
/**
* Class Annotation
*/
class Http extends Annotation
{
const HTTP_EVENT = 'http:event:';
const CLOSE = 'Close';
/**
* @var string
* @Interceptor(LoginInterceptor)
*/
private string $Interceptor = 'required|not empty';
/**
* @var string
*/
private string $Limits = 'required|not empty';
private string $Method = 'post';
private string $Middleware = '';
private string $After = '';
protected array $_annotations = [];
/**
* @param Node $node
* @param ReflectionClass $reflect
* @param $method
* @param $annotations
* @throws ReflectionException
*/
public function read(Node $node, ReflectionClass $reflect, $method, $annotations)
{
$method = $reflect->getMethod($method);
$_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment());
foreach ($_annotations as $keyName => $annotation) {
if (!in_array($keyName, $annotations)) {
continue;
}
$this->bind($keyName, $node, $annotation);
}
}
/**
* @param $keyName
* @param $node
* @param $annotation
*/
private function bind($keyName, $node, $annotation)
{
switch ($keyName) {
case 'Method':
$this->bindMethod($node, $annotation);
break;
case'Interceptor':
$this->bindInterceptors($node, $annotation);
break;
case 'Middleware':
$this->bindMiddleware($node, $annotation);
break;
case 'Limits':
$this->bindLimits($node, $annotation);
break;
case 'After':
$this->bindAfter($node, $annotation);
break;
}
}
/**
* @param $node
* @param $annotation
*/
private function bindMethod($node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
if (in_array('any', $explode)) {
$explode = ['*'];
}
$node->method = $explode;
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindMiddleware(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Middleware\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$node->addMiddleware($middleware);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindInterceptors(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Interceptor\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Interceptor)) {
continue;
}
$node->addInterceptor([$middleware, 'Interceptor']);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindAfter(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\After\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof After)) {
continue;
}
$node->addAfter([$middleware, 'onHandler']);
}
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
private function bindLimits(Node $node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Limits\\' . $middleware;
}
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Limits)) {
continue;
}
$node->addLimits([$middleware, 'next']);
}
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
* @throws
*/
public function createHandler($controller, $methodName, $events)
{
return Snowflake::createObject($events[2]);
}
/**
* @param $events
* @return bool
*/
public function isLegitimate($events)
{
return isset($events[2]) && !empty($events[2]);
}
/**
* @param $name
* @param $comment
* @return false|string
*/
public function getName($name, $comment = [])
{
$prefix = self::HTTP_EVENT . ltrim($name, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
-62
View File
@@ -1,62 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use Snowflake\Annotation\Annotation;
/**
* Class Tcp
* @package HttpServer\Route\Annotation
*/
class Tcp extends Annotation
{
const CONNECT = 'Connect';
const PACKET = 'Packet';
const RECEIVE = 'Receive';
const CLOSE = 'Close';
private string $Message = 'required|not empty';
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment = [])
{
$prefix = 'TCP:ANNOTATION:' . ltrim($events, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
-65
View File
@@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
namespace HttpServer\Route\Annotation;
use Snowflake\Annotation\Annotation;
/**
* Class Websocket
* @package Snowflake\Annotation
*/
class Websocket extends Annotation
{
const MESSAGE = 'Message';
const HANDSHAKE = 'Handshake';
const CLOSE = 'Close';
private string $Message = 'required|not empty';
private string $Handshake;
private string $Close;
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment = [])
{
$prefix = 'WEBSOCKET:ANNOTATION:' . ltrim($events, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}
+2 -2
View File
@@ -12,7 +12,7 @@ namespace HttpServer\Route;
class Any
{
private $nodes = [];
private array $nodes = [];
/**
* Any constructor.
@@ -29,7 +29,7 @@ class Any
* @param $arguments
* @return $this
*/
public function __call($name, $arguments)
public function __call($name, $arguments): static
{
foreach ($this->nodes as $node) {
$node->{$name}(...$arguments);
+7 -7
View File
@@ -32,7 +32,7 @@ class Filter extends Application
* @return BodyFilter|bool
* @throws Exception
*/
public function setBody(array $value)
public function setBody(array $value): bool|BodyFilter
{
if (empty($value)) {
return true;
@@ -52,7 +52,7 @@ class Filter extends Application
* @return HeaderFilter|bool
* @throws Exception
*/
public function setHeader(array $value)
public function setHeader(array $value): HeaderFilter|bool
{
if (empty($value)) {
return true;
@@ -72,7 +72,7 @@ class Filter extends Application
* @return QueryFilter|bool
* @throws Exception
*/
public function setQuery(array $value)
public function setQuery(array $value): QueryFilter|bool
{
if (empty($value)) {
return true;
@@ -90,7 +90,7 @@ class Filter extends Application
/**
* @throws Exception
*/
public function handler()
public function handler(): bool
{
if (($error = $this->filters()) !== true) {
throw new FilterException($error);
@@ -104,7 +104,7 @@ class Filter extends Application
/**
* @return bool
*/
private function filters()
private function filters(): bool
{
if (empty($this->_filters)) {
return true;
@@ -118,9 +118,9 @@ class Filter extends Application
}
/**
* @return bool|mixed
* @return mixed
*/
private function grant()
private function grant(): mixed
{
if (!is_callable($this->grant, true)) {
return true;
+1 -1
View File
@@ -18,7 +18,7 @@ class BodyFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+1 -1
View File
@@ -27,7 +27,7 @@ abstract class Filter extends Application
* @return bool
* @throws Exception
*/
protected function validator()
protected function validator(): bool
{
$validator = Validator::getInstance();
$validator->setParams($this->params);
+9 -1
View File
@@ -5,12 +5,20 @@ declare(strict_types=1);
namespace HttpServer\Route\Filter;
use JetBrains\PhpStorm\Pure;
use Throwable;
class FilterException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
/**
* FilterException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
#[Pure] public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
+1 -1
View File
@@ -18,7 +18,7 @@ class HeaderFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+1 -1
View File
@@ -18,7 +18,7 @@ class QueryFilter extends Filter
* @return bool
* @throws Exception
*/
public function check()
public function check(): bool
{
return $this->validator();
}
+4 -2
View File
@@ -7,6 +7,7 @@ namespace HttpServer\Route;
use Exception;
use HttpServer\Application;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
/**
@@ -43,9 +44,10 @@ class Handler extends Application
/**
* @param $route
* @param $handler
* @return Handler
* @return Handler|Node|null
* @throws ConfigException
*/
public function handler($route, $handler)
public function handler($route, $handler): Handler|Node|null
{
return $this->router->addRoute($route, $handler, 'receive');
}
-1
View File
@@ -15,7 +15,6 @@ use Annotation\Route\Middleware as RMiddleware;
use Exception;
use Annotation\Route\Limits;
use HttpServer\Route\Dispatch\Dispatch;
use Snowflake\Core\JSON;
use Snowflake\Snowflake;
/**
+26 -34
View File
@@ -5,18 +5,14 @@ namespace HttpServer\Route;
use Closure;
use Exception;
use HttpServer\Exception\ExitException;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
use HttpServer\IInterface\RouterInterface;
use HttpServer\Application;
use HttpServer\Route\Annotation\Http;
use JetBrains\PhpStorm\Pure;
use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
defined('ROUTER_TREE') or define('ROUTER_TREE', 1);
defined('ROUTER_HASH') or define('ROUTER_HASH', 2);
@@ -42,8 +38,6 @@ class Router extends Application implements RouterInterface
public bool $useTree = false;
private bool $reading = false;
/**
* @param Closure $middleware
@@ -115,7 +109,7 @@ class Router extends Application implements RouterInterface
* @param $path
* @return string
*/
private function resolve($path)
#[Pure] private function resolve($path): string
{
$paths = array_column($this->groupTacks, 'prefix');
if (empty($paths)) {
@@ -182,10 +176,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed
* @return Node|null
* @throws ConfigException
*/
public function socket($route, $handler): mixed
public function socket($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'socket');
}
@@ -205,10 +199,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function get($route, $handler)
public function get($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'get');
}
@@ -219,7 +213,7 @@ class Router extends Application implements RouterInterface
* @return mixed|Node|null
* @throws
*/
public function options($route, $handler)
public function options($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'options');
}
@@ -240,8 +234,9 @@ class Router extends Application implements RouterInterface
* @param $route
* @param $handler
* @return Any
* @throws ConfigException
*/
public function any($route, $handler)
public function any($route, $handler): Any
{
$nodes = [];
foreach (['get', 'post', 'options', 'put', 'delete'] as $method) {
@@ -253,10 +248,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function delete($route, $handler)
public function delete($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'delete');
}
@@ -264,10 +259,10 @@ class Router extends Application implements RouterInterface
/**
* @param $route
* @param $handler
* @return mixed|Node|null
* @throws
* @return Node|null
* @throws ConfigException
*/
public function put($route, $handler)
public function put($route, $handler): ?Node
{
return $this->addRoute($route, $handler, 'put');
}
@@ -380,7 +375,7 @@ class Router extends Application implements RouterInterface
* @return array
* '*'
*/
public function split($path)
public function split($path): array
{
$prefix = $this->addPrefix();
$path = ltrim($path, '/');
@@ -403,7 +398,7 @@ class Router extends Application implements RouterInterface
/**
* @return array
*/
public function each()
public function each(): array
{
$paths = [];
foreach ($this->nodes as $node) {
@@ -427,7 +422,7 @@ class Router extends Application implements RouterInterface
* @param array $returns
* @return array
*/
private function readByArray($array, $returns = [])
private function readByArray($array, $returns = []): array
{
foreach ($array as $value) {
if (empty($value)) {
@@ -450,7 +445,7 @@ class Router extends Application implements RouterInterface
* @param string $paths
* @return array
*/
private function readByChild($child, $paths = '')
private function readByChild($child, $paths = ''): array
{
$newPath = [];
/** @var Node $item */
@@ -490,11 +485,10 @@ class Router extends Application implements RouterInterface
/**
* @param $exception
* @return false|int|mixed|string
* @return mixed
* @throws ComponentException
* @throws Exception
*/
private function exception($exception)
private function exception($exception): mixed
{
return Snowflake::app()->getLogger()->exception($exception);
}
@@ -502,10 +496,10 @@ class Router extends Application implements RouterInterface
/**
* @param Request $request
* @return Node|false|int|mixed|string|null
* @return Node|null 树干搜索
* 树干搜索
*/
private function find_path(Request $request)
private function find_path(Request $request): ?Node
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
@@ -545,7 +539,7 @@ class Router extends Application implements RouterInterface
* @param $request
* @return Node|null
*/
private function search_options($request)
private function search_options($request): ?Node
{
$method = $request->getMethod();
if (!isset($this->nodes[$method])) {
@@ -563,7 +557,7 @@ class Router extends Application implements RouterInterface
* @return Node|null
* 树杈搜索
*/
private function Branch_search($request)
private function Branch_search($request): ?Node
{
$node = $this->tree_search($request->getExplode(), $request->getMethod());
if ($node instanceof Node) {
@@ -596,7 +590,6 @@ class Router extends Application implements RouterInterface
private function loadRouteDir($path)
{
$files = glob($path . '/*');
$this->reading = true;
for ($i = 0; $i < count($files); $i++) {
if (is_dir($files[$i])) {
$this->loadRouteDir($files[$i]);
@@ -604,7 +597,6 @@ class Router extends Application implements RouterInterface
$this->loadRouterFile($files[$i]);
}
}
$this->reading = false;
}
+4 -7
View File
@@ -9,8 +9,6 @@ use HttpServer\Events\OnConnect;
use HttpServer\Events\OnPacket;
use HttpServer\Events\OnReceive;
use HttpServer\Events\OnRequest;
use HttpServer\Route\Annotation\Http as AnnotationHttp;
use HttpServer\Route\Annotation\Tcp;
use HttpServer\Service\Http;
use HttpServer\Service\Receive;
use HttpServer\Service\Packet;
@@ -22,7 +20,6 @@ use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
use Swoole\Runtime;
/**
@@ -111,21 +108,21 @@ class Server extends Application
/**
* @return void
* @return string start server
*
* start server
* @throws ConfigException
* @throws Exception
*/
public function start()
public function start(): string
{
$configs = Config::get('servers', true);
Snowflake::clearWorkerId();
$baseServer = $this->initCore($configs);
if (!$baseServer) {
return;
return 'ok';
}
$baseServer->start();
return $baseServer->start();
}
+3 -3
View File
@@ -43,11 +43,11 @@ trait Server
/**
* @return mixed|void
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onHandlerListener()
public function onHandlerListener(): mixed
{
$this->on('WorkerStop', $this->createHandler('workerStop'));
$this->on('WorkerExit', $this->createHandler('workerExit'));
@@ -83,7 +83,7 @@ trait Server
* @throws ReflectionException
* @throws Exception
*/
protected function createHandler($eventName)
protected function createHandler($eventName): array
{
$classPrefix = 'HttpServer\Events\On' . ucfirst($eventName);
if (!class_exists($classPrefix)) {