diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php index cf9309c9..c2ee7f7b 100644 --- a/HttpServer/Abstracts/Callback.php +++ b/HttpServer/Abstracts/Callback.php @@ -6,14 +6,17 @@ namespace HttpServer\Abstracts; use Database\Connection; use Exception; +use HttpServer\Http\Request; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; +use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Core\Json; use Snowflake\Error\LoggerProcess; use Snowflake\Event; use Snowflake\Exception\ComponentException; use Snowflake\Exception\ConfigException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\Process; use Swoole\Server; @@ -64,6 +67,19 @@ abstract class Callback extends HttpService } + /** + * @param $fd + * @param $data + * @param $reID + * @return Request + * @throws ReflectionException + * @throws NotFindClassException + */ + protected function _request($fd, $data, $reID): Request + { + return Request::createListenRequest($fd, $data, $reID); + } + /** * @param $message * @throws diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index f3697688..35b0ecc9 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -4,13 +4,12 @@ declare(strict_types=1); namespace HttpServer\Events; +use Exception; use HttpServer\Abstracts\Callback; -use HttpServer\Http\Request; use Snowflake\Core\Json; use Snowflake\Event; use Snowflake\Snowflake; use Swoole\Server; -use Exception; /** * Class OnPacket @@ -35,10 +34,13 @@ class OnPacket extends Callback */ public function onHandler(Server $server, string $data, array $clientInfo): mixed { + [$host, $port] = [$clientInfo['address'], $clientInfo['port']]; try { - $request = Request::createListenRequest($clientInfo, $server, $data); - - [$host, $port] = [$clientInfo['address'], $clientInfo['port']]; + \Swoole\Coroutine\defer(function () { + fire(Event::SYSTEM_RESOURCE_RELEASES); + \logger_insert(); + }); + $request = $this->_request($clientInfo, $server, $data); $router = Snowflake::app()->getRouter(); if (($node = $router->find_path($request)) === null) { @@ -56,10 +58,7 @@ class OnPacket extends Callback $response = Json::encode(['state' => 500, 'message' => $exception->getMessage()]); - return $server->sendto($clientInfo['address'], $clientInfo['port'], $response); - } finally { - fire(Event::SYSTEM_RESOURCE_RELEASES); - logger()->insert(); + return $server->sendto($host, $port, $response); } } diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php index d528c23e..90863e96 100644 --- a/HttpServer/Events/OnReceive.php +++ b/HttpServer/Events/OnReceive.php @@ -49,9 +49,8 @@ class OnReceive extends Callback { try { \Swoole\Coroutine\defer(function (){ - $event = Snowflake::app()->getEvent(); - $event->trigger(Event::SYSTEM_RESOURCE_RELEASES); - \logger()->insert(); + fire(Event::SYSTEM_RESOURCE_RELEASES); + \logger_insert(); }); $request = $this->_request($fd, $data, $reID); if (($node = $this->router->find_path($request)) === null) { @@ -71,18 +70,4 @@ class OnReceive extends Callback } - /** - * @param $fd - * @param $data - * @param $reID - * @return Request - * @throws ReflectionException - * @throws NotFindClassException - */ - private function _request($fd, $data, $reID): Request - { - return Request::createListenRequest($fd, $data, $reID); - } - - } diff --git a/function.php b/function.php index fc68bb2c..7326ca5d 100644 --- a/function.php +++ b/function.php @@ -5,6 +5,7 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../')); use Annotation\Annotation; use Annotation\Attribute; +use HttpServer\Http\Context; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; use HttpServer\Http\Response; @@ -16,50 +17,49 @@ use HttpServer\Service\Websocket; use JetBrains\PhpStorm\Pure; use Snowflake\Abstracts\Config; use Snowflake\Application; +use Snowflake\Core\ArrayAccess; use Snowflake\Error\Logger; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; -use HttpServer\Http\Context; -use Snowflake\Core\ArrayAccess; use Swoole\WebSocket\Server; if (!function_exists('make')) { - /** - * @param $name - * @param $default - * @return mixed - * @throws - */ - function make($name, $default = null): mixed - { - if (class_exists($name)) { - return Snowflake::createObject($name); - } - if (Snowflake::has($name)) { - return Snowflake::app()->get($name); - } - if (empty($default)) { - throw new Exception("Unknown component ID: $name"); - } - if (Snowflake::has($default)) { - return Snowflake::app()->get($default); - } - $class = Snowflake::createObject($default); - class_alias($name, $default, true); - return $class; - } + /** + * @param $name + * @param $default + * @return mixed + * @throws + */ + function make($name, $default = null): mixed + { + if (class_exists($name)) { + return Snowflake::createObject($name); + } + if (Snowflake::has($name)) { + return Snowflake::app()->get($name); + } + if (empty($default)) { + throw new Exception("Unknown component ID: $name"); + } + if (Snowflake::has($default)) { + return Snowflake::app()->get($default); + } + $class = Snowflake::createObject($default); + class_alias($name, $default, true); + return $class; + } } if (!function_exists('workerName')) { - function workerName($worker_id) - { - return $worker_id >= Snowflake::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker'; - } + function workerName($worker_id) + { + return $worker_id >= Snowflake::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker'; + } } @@ -67,14 +67,14 @@ if (!function_exists('workerName')) { if (!function_exists('annotation')) { - /** - * @return Annotation - * @throws Exception - */ - function annotation(): Annotation - { - return Snowflake::getAnnotation(); - } + /** + * @return Annotation + * @throws Exception + */ + function annotation(): Annotation + { + return Snowflake::getAnnotation(); + } } @@ -83,14 +83,14 @@ if (!function_exists('annotation')) { if (!function_exists('swoole')) { - /** - * @return Server|null - * @throws Exception - */ - function swoole(): ?Server - { - return Snowflake::getWebSocket(); - } + /** + * @return Server|null + * @throws Exception + */ + function swoole(): ?Server + { + return Snowflake::getWebSocket(); + } } @@ -99,48 +99,48 @@ if (!function_exists('swoole')) { if (!function_exists('recursive_directory')) { - /** - * @param DirectoryIterator $file - * @throws Exception - */ - function recursive_callback(DirectoryIterator $file) - { - $attributes = Snowflake::getAnnotation(); + /** + * @param DirectoryIterator $file + * @throws Exception + */ + function recursive_callback(DirectoryIterator $file) + { + $attributes = Snowflake::getAnnotation(); - $annotations = $attributes->getFilename($file->getRealPath()); - if (empty($annotations)) { - return; - } + $annotations = $attributes->getFilename($file->getRealPath()); + if (empty($annotations)) { + return; + } - /** @var Attribute $value */ - foreach ($annotations['methods'] as $name => $attribute) { - foreach ($attribute as $value) { - if (!($value instanceof Attribute)) { - continue; - } - $value->execute([$annotations['handler'], $name]); - } - } - } + /** @var Attribute $value */ + foreach ($annotations['methods'] as $name => $attribute) { + foreach ($attribute as $value) { + if (!($value instanceof Attribute)) { + continue; + } + $value->execute([$annotations['handler'], $name]); + } + } + } - /** - * @param string $path - */ - function recursive_directory(string $path) - { - $directoryIterators = new \DirectoryIterator($path); - foreach ($directoryIterators as $directoryIterator) { - if ($directoryIterator->getFilename() === '.' || $directoryIterator->getFilename() === '..') { - continue; - } - if ($directoryIterator->isDir()) { - Recursive_directory($directoryIterator->getRealPath()); - } else { - call_user_func('recursive_callback', $directoryIterator); - } - } - unset($directoryIterators); - } + /** + * @param string $path + */ + function recursive_directory(string $path) + { + $directoryIterators = new \DirectoryIterator($path); + foreach ($directoryIterators as $directoryIterator) { + if ($directoryIterator->getFilename() === '.' || $directoryIterator->getFilename() === '..') { + continue; + } + if ($directoryIterator->isDir()) { + Recursive_directory($directoryIterator->getRealPath()); + } else { + call_user_func('recursive_callback', $directoryIterator); + } + } + unset($directoryIterators); + } } @@ -148,14 +148,14 @@ if (!function_exists('recursive_directory')) { if (!function_exists('directory')) { - /** - * @param $name - * @return string - */ - #[Pure] function directory($name): string - { - return realpath(APP_PATH . $name); - } + /** + * @param $name + * @return string + */ + #[Pure] function directory($name): string + { + return realpath(APP_PATH . $name); + } } @@ -164,28 +164,28 @@ if (!function_exists('directory')) { if (!function_exists('isUrl')) { - /** - * @param $url - * @param bool $get_info - * @return false|array - */ - function isUrl($url, $get_info = true): bool|array - { - $queryMatch = '/((http[s]?):\/\/)?(([\w\-\_]+\.)+\w+(:\d+)?)(\/.*)?/'; - if (!preg_match($queryMatch, $url, $outPut)) { - return false; - } - $port = str_replace(':', '', $outPut[5]); + /** + * @param $url + * @param bool $get_info + * @return false|array + */ + function isUrl($url, $get_info = true): bool|array + { + $queryMatch = '/((http[s]?):\/\/)?(([\w\-\_]+\.)+\w+(:\d+)?)(\/.*)?/'; + if (!preg_match($queryMatch, $url, $outPut)) { + return false; + } + $port = str_replace(':', '', $outPut[5]); - [$isHttps, $domain, $port, $path] = [$outPut[2] == 'https', $outPut[3], $port, $outPut[6] ?? '']; - if ($isHttps && empty($port)) { - $port = 443; - } + [$isHttps, $domain, $port, $path] = [$outPut[2] == 'https', $outPut[3], $port, $outPut[6] ?? '']; + if ($isHttps && empty($port)) { + $port = 443; + } - unset($outPut); + unset($outPut); - return [$isHttps == 'https', $domain, $port, $path]; - } + return [$isHttps == 'https', $domain, $port, $path]; + } } @@ -193,23 +193,23 @@ if (!function_exists('isUrl')) { if (!function_exists('split_request_uri')) { - /** - * @param $url - * @return false|array - */ - function split_request_uri($url): bool|array - { - if (($parse = isUrl($url, null)) === false) { - return false; - } + /** + * @param $url + * @return false|array + */ + function split_request_uri($url): bool|array + { + if (($parse = isUrl($url, null)) === false) { + return false; + } - [$isHttps, $domain, $port, $path] = $parse; - $uri = $isHttps ? 'https://' . $domain : 'http://' . $domain; - if (!empty($port)) { - $uri .= ':' . $port; - } - return [$uri, $path]; - } + [$isHttps, $domain, $port, $path] = $parse; + $uri = $isHttps ? 'https://' . $domain : 'http://' . $domain; + if (!empty($port)) { + $uri .= ':' . $port; + } + return [$uri, $path]; + } } @@ -217,15 +217,15 @@ if (!function_exists('split_request_uri')) { if (!function_exists('hadDomain')) { - /** - * @param $url - * @return false|array - */ - function hadDomain($url): bool|array - { - $param = split_request_uri($url); - return !is_array($param) ? false : $param[0]; - } + /** + * @param $url + * @return false|array + */ + function hadDomain($url): bool|array + { + $param = split_request_uri($url); + return !is_array($param) ? false : $param[0]; + } } @@ -233,27 +233,27 @@ if (!function_exists('hadDomain')) { if (!function_exists('isDomain')) { - /** - * @param $url - * @return false|array - */ - function isDomain($url): array|bool - { - return !isIp($url); - } + /** + * @param $url + * @return false|array + */ + function isDomain($url): array|bool + { + return !isIp($url); + } } if (!function_exists('isIp')) { - /** - * @param $url - * @return false|array - */ - function isIp($url): bool|array - { - return preg_match('/(\d{1,3}\.){3}\.\d{1,3}(:\d{1,5})?/', $url); - } + /** + * @param $url + * @return false|array + */ + function isIp($url): bool|array + { + return preg_match('/(\d{1,3}\.){3}\.\d{1,3}(:\d{1,5})?/', $url); + } } @@ -261,32 +261,32 @@ if (!function_exists('isIp')) { if (!function_exists('loadByDir')) { - /** - * @param $namespace - * @param $dirname - */ - function classAutoload($namespace, $dirname) - { - foreach (glob(rtrim($dirname, '/') . '/*') as $value) { - $value = realpath($value); - if (is_dir($value)) { - classAutoload($namespace, $value); - } else { - $pos = strpos($value, '.php'); - if ($pos === false || strlen($value) - 4 != $pos) { - continue; - } + /** + * @param $namespace + * @param $dirname + */ + function classAutoload($namespace, $dirname) + { + foreach (glob(rtrim($dirname, '/') . '/*') as $value) { + $value = realpath($value); + if (is_dir($value)) { + classAutoload($namespace, $value); + } else { + $pos = strpos($value, '.php'); + if ($pos === false || strlen($value) - 4 != $pos) { + continue; + } - $replace = ltrim(str_replace(__DIR__, '', $value), '/'); - $replace = str_replace('.php', '', $replace); + $replace = ltrim(str_replace(__DIR__, '', $value), '/'); + $replace = str_replace('.php', '', $replace); - $first = explode(DIRECTORY_SEPARATOR, $replace); - array_shift($first); + $first = explode(DIRECTORY_SEPARATOR, $replace); + array_shift($first); - Snowflake::setAutoload($namespace . '\\' . implode('\\', $first), $value); - } - } - } + Snowflake::setAutoload($namespace . '\\' . implode('\\', $first), $value); + } + } + } } @@ -295,274 +295,286 @@ if (!function_exists('loadByDir')) { if (!function_exists('write')) { - /** - * @param string $messages - * @param string $category - * @throws Exception - */ - function write(string $messages, $category = 'app') - { - $logger = Snowflake::app()->getLogger(); - $logger->write($messages, $category); - } + /** + * @param string $messages + * @param string $category + * @throws Exception + */ + function write(string $messages, $category = 'app') + { + $logger = Snowflake::app()->getLogger(); + $logger->write($messages, $category); + } } if (!function_exists('redis')) { - /** - * @param string $messages - * @param string $category - * @throws Exception - */ - function redis(): \Snowflake\Cache\Redis|Redis - { - return Snowflake::app()->getRedis(); - } + /** + * @param string $messages + * @param string $category + * @throws Exception + */ + function redis(): \Snowflake\Cache\Redis|Redis + { + return Snowflake::app()->getRedis(); + } } if (!function_exists('fire')) { - /** - * @param string $event - * @param array $params - * @throws Exception - * @throws Exception - */ - function fire(string $event, array $params = []) - { - $logger = Snowflake::app()->getEvent(); - $logger->trigger($event, $params); - } + /** + * @param string $event + * @param array $params + * @throws Exception + * @throws Exception + */ + function fire(string $event, array $params = []) + { + $logger = Snowflake::app()->getEvent(); + $logger->trigger($event, $params); + } } if (!function_exists('aop')) { - /** - * @param mixed $handler - * @param array $params - * @return mixed - * @throws Exception - */ - function aop(mixed $handler, array $params = []): mixed - { - return Snowflake::app()->get('aop')->dispatch($handler, ...$params); - } + /** + * @param mixed $handler + * @param array $params + * @return mixed + * @throws Exception + */ + function aop(mixed $handler, array $params = []): mixed + { + return Snowflake::app()->get('aop')->dispatch($handler, ...$params); + } } if (!function_exists('app')) { - /** - * @return Application|null - */ - #[Pure] function app(): ?Application - { - return Snowflake::app(); - } + /** + * @return Application|null + */ + #[Pure] function app(): ?Application + { + return Snowflake::app(); + } } if (!function_exists('instance_load')) { - function instance_load() - { - $content = json_decode(file_get_contents(__DIR__ . '/composer.json'), true); - if (isset($content['autoload']) && isset($content['autoload']['psr-4'])) { - $psr4 = $content['autoload']['psr-4']; - foreach ($psr4 as $namespace => $dirname) { - classAutoload($namespace, __DIR__ . '/' . $dirname); - } - } - } + function instance_load() + { + $content = json_decode(file_get_contents(__DIR__ . '/composer.json'), true); + if (isset($content['autoload']) && isset($content['autoload']['psr-4'])) { + $psr4 = $content['autoload']['psr-4']; + foreach ($psr4 as $namespace => $dirname) { + classAutoload($namespace, __DIR__ . '/' . $dirname); + } + } + } } if (!function_exists('exif_imagetype')) { - /** - * @param $name - * @return string - */ - function exif_imagetype($name): string - { - return get_file_extension($name); - } + /** + * @param $name + * @return string + */ + function exif_imagetype($name): string + { + return get_file_extension($name); + } } if (!function_exists('logger')) { - /** - * @return Logger - * @throws Exception - */ - function logger(): Logger - { - return Snowflake::app()->getLogger(); - } + /** + * @return Logger + * @throws Exception + */ + function logger(): Logger + { + return Snowflake::app()->getLogger(); + } +} + +if (!function_exists('logger_insert')) { + + + /** + * @throws Exception + */ + function logger_insert(): void + { + Snowflake::app()->getLogger()->insert(); + } } if (!function_exists('get_file_extension')) { - function get_file_extension($filename) - { - $mime_types = array( - 'txt' => 'text/plain', - 'htm' => 'text/html', - 'html' => 'text/html', - 'php' => 'text/html', - 'css' => 'text/css', - 'js' => 'application/javascript', - 'json' => 'application/json', - 'xml' => 'application/xml', - 'swf' => 'application/x-shockwave-flash', - 'flv' => 'video/x-flv', + function get_file_extension($filename) + { + $mime_types = array( + 'txt' => 'text/plain', + 'htm' => 'text/html', + 'html' => 'text/html', + 'php' => 'text/html', + 'css' => 'text/css', + 'js' => 'application/javascript', + 'json' => 'application/json', + 'xml' => 'application/xml', + 'swf' => 'application/x-shockwave-flash', + 'flv' => 'video/x-flv', - // images - 'png' => 'image/png', - 'jpeg' => 'image/jpeg', - 'gif' => 'image/gif', - 'bmp' => 'image/bmp', - 'ico' => 'image/vnd.microsoft.icon', - 'tiff' => 'image/tiff', - 'svg' => 'image/svg+xml', + // images + 'png' => 'image/png', + 'jpeg' => 'image/jpeg', + 'gif' => 'image/gif', + 'bmp' => 'image/bmp', + 'ico' => 'image/vnd.microsoft.icon', + 'tiff' => 'image/tiff', + 'svg' => 'image/svg+xml', - // archives - 'zip' => 'application/zip', - 'rar' => 'application/x-rar-compressed', - 'exe' => 'application/x-msdownload', - 'msi' => 'application/x-msdownload', - 'cab' => 'application/vnd.ms-cab-compressed', + // archives + 'zip' => 'application/zip', + 'rar' => 'application/x-rar-compressed', + 'exe' => 'application/x-msdownload', + 'msi' => 'application/x-msdownload', + 'cab' => 'application/vnd.ms-cab-compressed', - // audio/video - 'mp3' => 'audio/mpeg', - 'qt' => 'video/quicktime', - 'mov' => 'video/quicktime', + // audio/video + 'mp3' => 'audio/mpeg', + 'qt' => 'video/quicktime', + 'mov' => 'video/quicktime', - // adobe - 'pdf' => 'application/pdf', - 'psd' => 'image/vnd.adobe.photoshop', - 'ai' => 'application/postscript', - 'eps' => 'application/postscript', - 'ps' => 'application/postscript', + // adobe + 'pdf' => 'application/pdf', + 'psd' => 'image/vnd.adobe.photoshop', + 'ai' => 'application/postscript', + 'eps' => 'application/postscript', + 'ps' => 'application/postscript', - // ms office - 'doc' => 'application/msword', - 'rtf' => 'application/rtf', - 'xls' => 'application/vnd.ms-excel', - 'ppt' => 'application/vnd.ms-powerpoint', + // ms office + 'doc' => 'application/msword', + 'rtf' => 'application/rtf', + 'xls' => 'application/vnd.ms-excel', + 'ppt' => 'application/vnd.ms-powerpoint', - // open office - 'odt' => 'application/vnd.oasis.opendocument.text', - 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', - ); + // open office + 'odt' => 'application/vnd.oasis.opendocument.text', + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', + ); - $explode = explode('.', $filename); - $ext = strtolower(array_pop($explode)); - if (array_key_exists($ext, $mime_types)) { - return $ext; - } elseif (function_exists('finfo_open')) { - $fInfo = finfo_open(FILEINFO_MIME); - $mimeType = finfo_file($fInfo, $filename); - finfo_close($fInfo); - $mimeType = current(explode('; ', $mimeType)); - if (($search = array_search($mimeType, $mime_types)) == false) { - return $mimeType; - } - return $search; - } else { - return 'application/octet-stream'; - } - } + $explode = explode('.', $filename); + $ext = strtolower(array_pop($explode)); + if (array_key_exists($ext, $mime_types)) { + return $ext; + } elseif (function_exists('finfo_open')) { + $fInfo = finfo_open(FILEINFO_MIME); + $mimeType = finfo_file($fInfo, $filename); + finfo_close($fInfo); + $mimeType = current(explode('; ', $mimeType)); + if (($search = array_search($mimeType, $mime_types)) == false) { + return $mimeType; + } + return $search; + } else { + return 'application/octet-stream'; + } + } } if (!function_exists('request')) { - /** - * @return Request - */ - function request(): Request - { - if (!Context::hasContext('request')) { - return make('request', Request::class); - } - return Context::getContext('request'); - } + /** + * @return Request + */ + function request(): Request + { + if (!Context::hasContext('request')) { + return make('request', Request::class); + } + return Context::getContext('request'); + } } if (!function_exists('Input')) { - /** - * @return HttpParams - */ - function Input(): HttpParams - { - return request()->params; - } + /** + * @return HttpParams + */ + function Input(): HttpParams + { + return request()->params; + } } if (!function_exists('Server')) { - /** - * @return Http|Packet|Receive|Websocket|null - * @throws Exception - */ - function Server(): Http|Packet|Receive|Websocket|null - { - return Snowflake::app()->getSwoole(); - } + /** + * @return Http|Packet|Receive|Websocket|null + * @throws Exception + */ + function Server(): Http|Packet|Receive|Websocket|null + { + return Snowflake::app()->getSwoole(); + } } if (!function_exists('storage')) { - /** - * @param string $fileName - * @param string $path - * @return string - * @throws Exception - */ - function storage($fileName = '', $path = ''): string - { + /** + * @param string $fileName + * @param string $path + * @return string + * @throws Exception + */ + function storage($fileName = '', $path = ''): string + { - $basePath = rtrim(Snowflake::getStoragePath(), '/'); - if (!empty($path)) { - $path = ltrim($path, '/'); - if (!is_dir($basePath . '/' . $path)) { - mkdir($basePath . '/' . $path, 0777, true); - } - } - if (empty($fileName)) { - return $basePath . '/' . $path . '/'; - } - $fileName = $basePath . '/' . $path . '/' . $fileName; - if (!file_exists($fileName)) { - touch($fileName); - } - return $fileName; - } + $basePath = rtrim(Snowflake::getStoragePath(), '/'); + if (!empty($path)) { + $path = ltrim($path, '/'); + if (!is_dir($basePath . '/' . $path)) { + mkdir($basePath . '/' . $path, 0777, true); + } + } + if (empty($fileName)) { + return $basePath . '/' . $path . '/'; + } + $fileName = $basePath . '/' . $path . '/' . $fileName; + if (!file_exists($fileName)) { + touch($fileName); + } + return $fileName; + } - /** - * @param $basePath - * @param $path - * @return false|string - * @throws Exception - */ - function initDir($path): bool|string - { - return mkdir($path, 0777, true); - } + /** + * @param $basePath + * @param $path + * @return false|string + * @throws Exception + */ + function initDir($path): bool|string + { + return mkdir($path, 0777, true); + } } @@ -571,19 +583,19 @@ if (!function_exists('storage')) { if (!function_exists('listen')) { - /** - * @param $name - * @param $callback - * @param $params - * @param $isAppend - * @throws Exception - * @throws Exception - */ - function listen($name, $callback, $params = [], $isAppend = true) - { - $event = Snowflake::app()->getEvent(); - $event->on($name, $callback, $params, $isAppend); - } + /** + * @param $name + * @param $callback + * @param $params + * @param $isAppend + * @throws Exception + * @throws Exception + */ + function listen($name, $callback, $params = [], $isAppend = true) + { + $event = Snowflake::app()->getEvent(); + $event->on($name, $callback, $params, $isAppend); + } } @@ -591,147 +603,147 @@ if (!function_exists('listen')) { if (!function_exists('event')) { - /** - * @param $name - * @param $callback - * @param $params - * @param $isAppend - * @throws Exception - * @throws Exception - */ - function event($name, $callback, $params = [], $isAppend = true) - { - $event = Snowflake::app()->getEvent(); - $event->on($name, $callback, $params, $isAppend); - } + /** + * @param $name + * @param $callback + * @param $params + * @param $isAppend + * @throws Exception + * @throws Exception + */ + function event($name, $callback, $params = [], $isAppend = true) + { + $event = Snowflake::app()->getEvent(); + $event->on($name, $callback, $params, $isAppend); + } } if (!function_exists('alias')) { - /** - * @param $class - * @param $name - */ - function alias($class, $name) - { - Snowflake::setAlias($class, $name); - } + /** + * @param $class + * @param $name + */ + function alias($class, $name) + { + Snowflake::setAlias($class, $name); + } } if (!function_exists('name')) { - /** - * @param int $pid - * @param string|null $prefix - * @throws ConfigException - * @throws Exception - */ - function name(int $pid, string $prefix = null) - { - if (Snowflake::getPlatform()->isMac()) { - return; - } + /** + * @param int $pid + * @param string|null $prefix + * @throws ConfigException + * @throws Exception + */ + function name(int $pid, string $prefix = null) + { + if (Snowflake::getPlatform()->isMac()) { + return; + } - $name = Config::get('id', 'system') . '[' . $pid . ']'; - if (!empty($prefix)) { - $name .= '.' . $prefix; - } - swoole_set_process_name($name); - } + $name = Config::get('id', 'system') . '[' . $pid . ']'; + if (!empty($prefix)) { + $name .= '.' . $prefix; + } + swoole_set_process_name($name); + } } if (!function_exists('response')) { - /** - * @return Response|stdClass - * @throws - */ - function response(): Response|stdClass - { - if (!Context::hasContext('response')) { - return make('response', Response::class); - } - return Context::getContext('response'); - } + /** + * @return Response|stdClass + * @throws + */ + function response(): Response|stdClass + { + if (!Context::hasContext('response')) { + return make('response', Response::class); + } + return Context::getContext('response'); + } } if (!function_exists('send')) { - /** - * @param $context - * @param $statusCode - * @return mixed - * @throws Exception - */ - function send($context, $statusCode = 404): mixed - { - return \response()->send($context, $statusCode); - } + /** + * @param $context + * @param $statusCode + * @return mixed + * @throws Exception + */ + function send($context, $statusCode = 404): mixed + { + return \response()->send($context, $statusCode); + } } if (!function_exists('zero_full')) { - function zero_full(int $data = 1, int $length = 10): string - { - return sprintf('%0' . $length . 'd', $data); - } + function zero_full(int $data = 1, int $length = 10): string + { + return sprintf('%0' . $length . 'd', $data); + } } if (!function_exists('redirect')) { - /** - * @param $url - * @return int - */ - function redirect($url): int - { - return response()->redirect($url); - } + /** + * @param $url + * @return int + */ + function redirect($url): int + { + return response()->redirect($url); + } } if (!function_exists('env')) { - /** - * @param $key - * @param null $default - * @return array|string|null - */ - #[Pure] function env($key, $default = null): null|array|string - { - $env = getenv($key); - if ($env === false) { - return $default; - } - return $env; - } + /** + * @param $key + * @param null $default + * @return array|string|null + */ + #[Pure] function env($key, $default = null): null|array|string + { + $env = getenv($key); + if ($env === false) { + return $default; + } + return $env; + } } if (!function_exists('sweep')) { - /** - * @param string $configPath - * @return array|false|string|null - */ - function sweep($configPath = APP_PATH . 'config'): bool|array|string|null - { - $array = []; - foreach (glob($configPath . '/*') as $config) { - $array = array_merge(require_once "$config", $array); - } - return $array; - } + /** + * @param string $configPath + * @return array|false|string|null + */ + function sweep($configPath = APP_PATH . 'config'): bool|array|string|null + { + $array = []; + foreach (glob($configPath . '/*') as $config) { + $array = array_merge(require_once "$config", $array); + } + return $array; + } } @@ -739,18 +751,18 @@ if (!function_exists('sweep')) { if (!function_exists('swoole_serialize')) { - /** - * @param $data - * @return string - */ - function swoole_serialize($data): string - { + /** + * @param $data + * @return string + */ + function swoole_serialize($data): string + { // if (class_exists('swoole_serialize')) { // return \swoole_serialize::pack($data); // } else { - return serialize($data); + return serialize($data); // } - } + } } @@ -758,21 +770,21 @@ if (!function_exists('swoole_serialize')) { if (!function_exists('swoole_unserialize')) { - /** - * @param $data - * @return string - */ - function swoole_unserialize($data): mixed - { - if (empty($data)) { - return null; - } + /** + * @param $data + * @return string + */ + function swoole_unserialize($data): mixed + { + if (empty($data)) { + return null; + } // if (class_exists('swoole_serialize')) { // return \swoole_serialize::unpack($data); // } else { - return unserialize($data); + return unserialize($data); // } - } + } } @@ -780,15 +792,15 @@ if (!function_exists('swoole_unserialize')) { if (!function_exists('merge')) { - /** - * @param $param - * @param $param1 - * @return array - */ - function merge($param, $param1): array - { - return ArrayAccess::merge($param, $param1); - } + /** + * @param $param + * @param $param1 + * @return array + */ + function merge($param, $param1): array + { + return ArrayAccess::merge($param, $param1); + } } @@ -796,62 +808,62 @@ if (!function_exists('merge')) { if (!function_exists('router')) { - /** - * @return Router - * @throws Exception - */ - function router(): Router - { - return Snowflake::app()->getRouter(); - } + /** + * @return Router + * @throws Exception + */ + function router(): Router + { + return Snowflake::app()->getRouter(); + } } if (!function_exists('jTraceEx')) { - /** - * @param $e - * @param null $seen - * @return string - */ - function jTraceEx($e, $seen = null): string - { - $starter = $seen ? 'Caused by: ' : ''; - $result = array(); - if (!$seen) $seen = array(); - $trace = $e->getTrace(); - $prev = $e->getPrevious(); - $result[] = sprintf('%s%s: %s', $starter, get_class($e), $e->getMessage()); - $file = $e->getFile(); - $line = $e->getLine(); - while (true) { - $current = "$file:$line"; - if (is_array($seen) && in_array($current, $seen)) { - $result[] = sprintf(' ... %d more', count($trace) + 1); - break; - } - $result[] = sprintf(' at %s%s%s(%s%s%s)', - count($trace) && array_key_exists('class', $trace[0]) ? str_replace('\\', '.', $trace[0]['class']) : '', - count($trace) && array_key_exists('class', $trace[0]) && array_key_exists('function', $trace[0]) ? '.' : '', - count($trace) && array_key_exists('function', $trace[0]) ? str_replace('\\', '.', $trace[0]['function']) : '(main)', - $line === null ? $file : basename($file), - $line === null ? '' : ':', - $line === null ? '' : $line); - if (is_array($seen)) - $seen[] = "$file:$line"; - if (!count($trace)) - break; - $file = array_key_exists('file', $trace[0]) ? $trace[0]['file'] : 'Unknown Source'; - $line = array_key_exists('file', $trace[0]) && array_key_exists('line', $trace[0]) && $trace[0]['line'] ? $trace[0]['line'] : null; - array_shift($trace); - } - $result = join("\n", $result); - if ($prev) - $result .= "\n" . jTraceEx($prev, $seen); + /** + * @param $e + * @param null $seen + * @return string + */ + function jTraceEx($e, $seen = null): string + { + $starter = $seen ? 'Caused by: ' : ''; + $result = array(); + if (!$seen) $seen = array(); + $trace = $e->getTrace(); + $prev = $e->getPrevious(); + $result[] = sprintf('%s%s: %s', $starter, get_class($e), $e->getMessage()); + $file = $e->getFile(); + $line = $e->getLine(); + while (true) { + $current = "$file:$line"; + if (is_array($seen) && in_array($current, $seen)) { + $result[] = sprintf(' ... %d more', count($trace) + 1); + break; + } + $result[] = sprintf(' at %s%s%s(%s%s%s)', + count($trace) && array_key_exists('class', $trace[0]) ? str_replace('\\', '.', $trace[0]['class']) : '', + count($trace) && array_key_exists('class', $trace[0]) && array_key_exists('function', $trace[0]) ? '.' : '', + count($trace) && array_key_exists('function', $trace[0]) ? str_replace('\\', '.', $trace[0]['function']) : '(main)', + $line === null ? $file : basename($file), + $line === null ? '' : ':', + $line === null ? '' : $line); + if (is_array($seen)) + $seen[] = "$file:$line"; + if (!count($trace)) + break; + $file = array_key_exists('file', $trace[0]) ? $trace[0]['file'] : 'Unknown Source'; + $line = array_key_exists('file', $trace[0]) && array_key_exists('line', $trace[0]) && $trace[0]['line'] ? $trace[0]['line'] : null; + array_shift($trace); + } + $result = join("\n", $result); + if ($prev) + $result .= "\n" . jTraceEx($prev, $seen); - return $result; - } + return $result; + } } @@ -860,15 +872,15 @@ if (!function_exists('jTraceEx')) { if (!function_exists('swoole_substr_json_decode')) { - /** - * @param $packet - * @param int $length - * @return mixed - */ - function swoole_substr_json_decode($packet, $length = 0): mixed - { - return json_decode($packet, true); - } + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_json_decode($packet, $length = 0): mixed + { + return json_decode($packet, true); + } } @@ -876,14 +888,14 @@ if (!function_exists('swoole_substr_json_decode')) { if (!function_exists('swoole_substr_unserialize')) { - /** - * @param $packet - * @param int $length - * @return mixed - */ - function swoole_substr_unserialize($packet, $length = 0): mixed - { - return unserialize($packet); - } + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_unserialize($packet, $length = 0): mixed + { + return unserialize($packet); + } }