diff --git a/core/Application.php b/core/Application.php index 438b4512..b81970c0 100644 --- a/core/Application.php +++ b/core/Application.php @@ -42,221 +42,218 @@ use Swoole\Timer; class Application extends BaseApplication { - /** - * @var string - */ - public string $id = 'uniqueId'; + /** + * @var string + */ + public string $id = 'uniqueId'; - public string $state = ''; + public string $state = ''; - /** @var array> */ - private array $_process = []; + /** @var array> */ + private array $_process = []; - /** - * @throws NotFindClassException - */ - public function init() - { - $this->import(ConsoleProviders::class); - $this->import(ServerProviders::class); - } + /** + * @throws NotFindClassException + */ + public function init() + { + $this->import(ConsoleProviders::class); + $this->import(ServerProviders::class); + } - /** - * @throws NotFindClassException - */ - public function withDatabase() - { - $this->import(DatabasesProviders::class); - } + /** + * @throws NotFindClassException + */ + public function withDatabase() + { + $this->import(DatabasesProviders::class); + } - /** - * @throws NotFindClassException - */ - public function withCrontab() - { - $this->import(CrontabProviders::class); - } + /** + * @throws NotFindClassException + */ + public function withCrontab() + { + $this->import(CrontabProviders::class); + } - /** - * @param string $class - * @param Process $process - */ - public function addProcess(string $class, Process $process) - { - if (!isset($this->_process[$class])) { - $this->_process[$class] = []; - } - $this->_process[$class][] = $process; - } - - - /** - * @return Process[] - */ - public function getProcess(): array - { - return $this->_process; - } - - - /** - * @param string $class - * @return Process|null - */ - public function getProcessName(string $class): ?Process - { - return $this->_process[$class] ?? null; - } - - - /** - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - public function withFileChangeListen() - { - $manager = $this->getServer(); - $manager->addProcess(FileChangeCustomProcess::class); - - putenv('enableFileChange=on'); - } - - - /** - * @param Closure|array $closure - * @return $this - * @throws Exception - */ - public function middleware(Closure|array $closure): static - { - $this->getRouter()->setMiddleware($closure); - return $this; - } - - - /** - * @param bool $useTree - * @return $this - * @throws Exception - */ - public function setUseTree(bool $useTree): static - { - $this->getRouter()->setUseTree($useTree); - return $this; - } - - - /** - * @param string $service - * @return $this - * @throws - */ - public function import(string $service): static - { - if (!class_exists($service)) { - throw new NotFindClassException($service); - } - $class = Kiri::getDi()->get($service); - if (method_exists($class, 'onImport')) { - $class->onImport($this); - } - return $this; - } - - - /** - * @param Kernel $kernel - * @return $this - */ - public function commands(Kernel $kernel): static - { - foreach ($kernel->getCommands() as $command) { - $this->register($command); - } - return $this; - } - - - /** - * @param string $command - * @throws - */ - public function register(string $command) - { - /** @var Console $abstracts */ - $abstracts = $this->get('console'); - $abstracts->register($command); - } - - - /** - * @param Input $argv - * @return void - * @throws Exception - */ - public function execute(Input $argv): void - { - try { - $this->register(Runtime::class); - - $manager = Kiri::app()->get('console'); - $class = $manager->setParameters($argv)->search(); - - $this->enableFileChange($class); - - $data = $this->getBuilder($manager->exec($class)); - } catch (\Throwable $exception) { - $data = $this->getBuilder(jTraceEx($exception)); - } finally { - print_r($data); - Timer::clearAll(); - } - } - - - /** - * @throws NotFindClassException - * @throws ReflectionException - */ - private function enableFileChange($class): void - { - if (env('enableFileChange', 'off') == 'off' || !($class instanceof Command)) { - scan_directory(directory('app'), 'App'); - - $loader = Kiri::app()->getRouter(); - $loader->_loader(); + /** + * @param string $class + * @param Process $process + */ + public function addProcess(string $class, Process $process) + { + if (!isset($this->_process[$class])) { + $this->_process[$class] = []; } - } + $this->_process[$class][] = $process; + } - /** - * @param $data - * @return Response|ResponseInterface - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - private function getBuilder($data): Response|ResponseInterface - { - return di(Response::class)->getBuilder($data); - } + /** + * @return Process[] + */ + public function getProcess(): array + { + return $this->_process; + } - /** - * @param $className - * @param null $abstracts - * @return stdClass - * @throws Exception - */ - public function make($className, $abstracts = null): stdClass - { - return make($className, $abstracts); - } + /** + * @param string $class + * @return Process|null + */ + public function getProcessName(string $class): ?Process + { + return $this->_process[$class] ?? null; + } + + + /** + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + public function withFileChangeListen() + { + $manager = $this->getServer(); + $manager->addProcess(FileChangeCustomProcess::class); + + enable_file_modification_listening(); + } + + + /** + * @param Closure|array $closure + * @return $this + * @throws Exception + */ + public function middleware(Closure|array $closure): static + { + $this->getRouter()->setMiddleware($closure); + return $this; + } + + + /** + * @param bool $useTree + * @return $this + * @throws Exception + */ + public function setUseTree(bool $useTree): static + { + $this->getRouter()->setUseTree($useTree); + return $this; + } + + + /** + * @param string $service + * @return $this + * @throws + */ + public function import(string $service): static + { + if (!class_exists($service)) { + throw new NotFindClassException($service); + } + $class = Kiri::getDi()->get($service); + if (method_exists($class, 'onImport')) { + $class->onImport($this); + } + return $this; + } + + + /** + * @param Kernel $kernel + * @return $this + */ + public function commands(Kernel $kernel): static + { + foreach ($kernel->getCommands() as $command) { + $this->register($command); + } + return $this; + } + + + /** + * @param string $command + * @throws + */ + public function register(string $command) + { + /** @var Console $abstracts */ + $abstracts = $this->get('console'); + $abstracts->register($command); + } + + + /** + * @param Input $argv + * @return void + * @throws Exception + */ + public function execute(Input $argv): void + { + try { + $this->register(Runtime::class); + + $manager = Kiri::app()->get('console'); + $class = $manager->setParameters($argv)->search(); + + $this->enableFileChange($class); + + $data = $this->getBuilder($manager->exec($class)); + } catch (\Throwable $exception) { + $data = $this->getBuilder(jTraceEx($exception)); + } finally { + print_r($data); + Timer::clearAll(); + } + } + + + /** + * @throws NotFindClassException + * @throws ReflectionException + */ + private function enableFileChange($class): void + { + if (!($class instanceof Command)) { + scan_directory(directory('app'), 'App'); + } + } + + + /** + * @param $data + * @return Response|ResponseInterface + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function getBuilder($data): Response|ResponseInterface + { + return di(Response::class)->getBuilder($data); + } + + + /** + * @param $className + * @param null $abstracts + * @return stdClass + * @throws Exception + */ + public function make($className, $abstracts = null): stdClass + { + return make($className, $abstracts); + } } diff --git a/function.php b/function.php index 6ddb9c94..d4eb4ca5 100644 --- a/function.php +++ b/function.php @@ -27,30 +27,68 @@ 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 Kiri::createObject($name); - } - if (Kiri::has($name)) { - return Kiri::app()->get($name); - } - if (empty($default)) { - throw new Exception("Unknown component ID: $name"); - } - if (Kiri::has($default)) { - return Kiri::app()->get($default); - } - $class = Kiri::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 Kiri::createObject($name); + } + if (Kiri::has($name)) { + return Kiri::app()->get($name); + } + if (empty($default)) { + throw new Exception("Unknown component ID: $name"); + } + if (Kiri::has($default)) { + return Kiri::app()->get($default); + } + $class = Kiri::createObject($default); + class_alias($name, $default, true); + return $class; + } + + +} + + +if (!function_exists('enable_file_modification_listening')) { + + + function enable_file_modification_listening(): void + { + putenv('enable_file_modification_listening=on'); + } + + +} + + +if (!function_exists('is_enable_file_modification_listening')) { + + + /** + * @return bool + */ + function is_enable_file_modification_listening(): bool + { + return env('enable_file_modification_listening', 'off') == 'off'; + } + + +} + +if (!function_exists('disable_file_modification_listening')) { + + + function disable_file_modification_listening() + { + putenv('enable_file_modification_listening=off'); + } } @@ -58,27 +96,27 @@ if (!function_exists('make')) { if (!function_exists('now')) { - /** - * @return string - */ - function now(): string - { - return date('Y-m-d H:i:s') . '.' . str_replace('0.', '', (string)microtime(true)); - } + /** + * @return string + */ + function now(): string + { + return date('Y-m-d H:i:s') . '.' . str_replace('0.', '', (string)microtime(true)); + } } if (!function_exists('workerName')) { - /** - * @param $worker_id - * @return string - */ - function workerName($worker_id) - { - return $worker_id >= Kiri::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker'; - } + /** + * @param $worker_id + * @return string + */ + function workerName($worker_id) + { + return $worker_id >= Kiri::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker'; + } } @@ -86,14 +124,14 @@ if (!function_exists('workerName')) { if (!function_exists('annotation')) { - /** - * @return Annotation - * @throws Exception - */ - function annotation(): Annotation - { - return Kiri::getAnnotation(); - } + /** + * @return Annotation + * @throws Exception + */ + function annotation(): Annotation + { + return Kiri::getAnnotation(); + } } @@ -102,21 +140,21 @@ if (!function_exists('annotation')) { if (!function_exists('scan_directory')) { - /** - * @param $dir - * @param $namespace - * @param array $exclude - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - function scan_directory($dir, $namespace, array $exclude = []) - { - $annotation = Kiri::app()->getAnnotation(); - $annotation->read($dir, $namespace, $exclude); + /** + * @param $dir + * @param $namespace + * @param array $exclude + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + function scan_directory($dir, $namespace, array $exclude = []) + { + $annotation = Kiri::app()->getAnnotation(); + $annotation->read($dir, $namespace, $exclude); - injectRuntime($dir, $exclude); - } + injectRuntime($dir, $exclude); + } } @@ -124,32 +162,32 @@ if (!function_exists('scan_directory')) { if (!function_exists('injectRuntime')) { - /** - * @param string $path - * @param array $exclude - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - function injectRuntime(string $path, array $exclude = []) - { - $fileLists = Kiri::getAnnotation()->runtime($path, $exclude); - $di = Kiri::getDi(); - foreach ($fileLists as $class) { - foreach ($di->getTargetNote($class) as $value) { - $value->execute($class); - } - $methods = $di->getMethodAttribute($class); - foreach ($methods as $method => $attribute) { - if (empty($attribute)) { - continue; - } - foreach ($attribute as $item) { - $item->execute($class, $method); - } - } - } - } + /** + * @param string $path + * @param array $exclude + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + function injectRuntime(string $path, array $exclude = []) + { + $fileLists = Kiri::getAnnotation()->runtime($path, $exclude); + $di = Kiri::getDi(); + foreach ($fileLists as $class) { + foreach ($di->getTargetNote($class) as $value) { + $value->execute($class); + } + $methods = $di->getMethodAttribute($class); + foreach ($methods as $method => $attribute) { + if (empty($attribute)) { + continue; + } + foreach ($attribute as $item) { + $item->execute($class, $method); + } + } + } + } } @@ -157,14 +195,14 @@ if (!function_exists('injectRuntime')) { if (!function_exists('swoole')) { - /** - * @return Server|null - * @throws Exception - */ - function swoole(): ?Server - { - return Kiri::getWebSocket(); - } + /** + * @return Server|null + * @throws Exception + */ + function swoole(): ?Server + { + return Kiri::getWebSocket(); + } } @@ -172,14 +210,14 @@ if (!function_exists('swoole')) { 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); + } } @@ -188,28 +226,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]; + } } @@ -217,23 +255,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]; + } } @@ -241,15 +279,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]; + } } @@ -257,27 +295,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); + } } @@ -285,32 +323,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); - Kiri::setAutoload($namespace . '\\' . implode('\\', $first), $value); - } - } - } + Kiri::setAutoload($namespace . '\\' . implode('\\', $first), $value); + } + } + } } @@ -319,523 +357,523 @@ if (!function_exists('loadByDir')) { if (!function_exists('write')) { - /** - * @param string $messages - * @param string $category - * @throws Exception - */ - function write(string $messages, string $category = 'app') - { - $logger = Kiri::app()->getLogger(); - $logger->write($messages, $category); - } + /** + * @param string $messages + * @param string $category + * @throws Exception + */ + function write(string $messages, string $category = 'app') + { + $logger = Kiri::app()->getLogger(); + $logger->write($messages, $category); + } } if (!function_exists('redis')) { - /** - * @return \Kiri\Cache\Redis|Redis - * @throws Exception - */ - function redis(): \Kiri\Cache\Redis|Redis - { - return Kiri::app()->getRedis(); - } + /** + * @return \Kiri\Cache\Redis|Redis + * @throws Exception + */ + function redis(): \Kiri\Cache\Redis|Redis + { + return Kiri::app()->getRedis(); + } } if (!function_exists('fire')) { - /** - * @param object $event - * @throws NotFindClassException - * @throws ReflectionException - */ - function fire(object $event) - { - di(EventDispatch::class)->dispatch($event); - } + /** + * @param object $event + * @throws NotFindClassException + * @throws ReflectionException + */ + function fire(object $event) + { + di(EventDispatch::class)->dispatch($event); + } } if (!function_exists('aop')) { - /** - * @param mixed $handler - * @param array $params - * @return mixed - * @throws Exception - */ - function aop(mixed $handler, array $params = []): mixed - { - return Kiri::getDi()->get(AspectManager::class)->dispatch($handler, $params); - } + /** + * @param mixed $handler + * @param array $params + * @return mixed + * @throws Exception + */ + function aop(mixed $handler, array $params = []): mixed + { + return Kiri::getDi()->get(AspectManager::class)->dispatch($handler, $params); + } } if (!function_exists('app')) { - /** - * @return Application|null - */ - #[Pure] function app(): ?Application - { - return Kiri::app(); - } + /** + * @return Application|null + */ + #[Pure] function app(): ?Application + { + return Kiri::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 Kiri::app()->getLogger(); - } + /** + * @return Logger + * @throws Exception + */ + function logger(): Logger + { + return Kiri::app()->getLogger(); + } } if (!function_exists('trim_blank')) { - /** - * 空白字符替换 - * @param string $content 内容 - * @param int $len 截取长度 - * @param string $encode 编码 - * @param bool $htmlTags - * @return array|string|null - */ - function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = true): array|string|null - { - $str = trim($content); - if ($htmlTags) { - $str = strip_tags($str); - } - $str = preg_replace('/[\n|\r|\t]+/', '', $str); - $str = preg_replace("/(\s|\ \;| |\xc2\xa0)/", '', $str); - if ($len > 0) { - return mb_substr($str, 0, $len, $encode); - } else { - return $str; - } - } + /** + * 空白字符替换 + * @param string $content 内容 + * @param int $len 截取长度 + * @param string $encode 编码 + * @param bool $htmlTags + * @return array|string|null + */ + function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = true): array|string|null + { + $str = trim($content); + if ($htmlTags) { + $str = strip_tags($str); + } + $str = preg_replace('/[\n|\r|\t]+/', '', $str); + $str = preg_replace("/(\s|\ \;| |\xc2\xa0)/", '', $str); + if ($len > 0) { + return mb_substr($str, 0, $len, $encode); + } else { + return $str; + } + } } if (!function_exists('get_file_extension')) { - function get_file_extension($filename) - { - $mime_types = [ - '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 = [ + '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', - '323' => 'text / h323', - 'acx' => 'application/internet-property-stream', - 'aif' => 'audio/x-aiff', - 'aifc' => 'audio/x-aiff', - 'aiff' => 'audio/x-aiff', - 'asf' => 'video/x-ms-asf', - 'asr' => 'video/x-ms-asf', - 'asx' => 'video/x-ms-asf', - 'au' => 'audio/basic', - 'avi' => 'video/x-msvideo', - 'axs' => 'application/olescript', - 'bas' => 'text/plain', - 'bcpio' => 'application/x-bcpio', - 'bin' => 'application/octet-stream', - 'c' => 'text/plain', - 'cat' => 'application/vnd.ms-pkiseccat', - 'cdf' => 'application/x-cdf', - 'cer' => 'application/x-x509-ca-cert', - 'class' => 'application/octet-stream', - 'clp' => 'application/x-msclip', - 'cmx' => 'image/x-cmx', - 'cod' => 'image/cis-cod', - 'cpio' => 'application/x-cpio', - 'crd' => 'application/x-mscardfile', - 'crl' => 'application/pkix-crl', - 'crt' => 'application/x-x509-ca-cert', - 'csh' => 'application/x-csh', - 'dcr' => 'application/x-director', - 'der' => 'application/x-x509-ca-cert', - 'dir' => 'application/x-director', - 'dll' => 'application/x-msdownload', - 'dms' => 'application/octet-stream', - 'dot' => 'application/msword', - 'dvi' => 'application/x-dvi', - 'dxr' => 'application/x-director', - 'etx' => 'text/x-setext', - 'evy' => 'application/envoy', - 'fif' => 'application/fractals', - 'flr' => 'x-world/x-vrml', - 'gtar' => 'application/x-gtar', - 'gz' => 'application/x-gzip', - 'h' => 'text/plain', - 'hdf' => 'application/x-hdf', - 'hlp' => 'application/winhlp', - 'hqx' => 'application/mac-binhex40', - 'hta' => 'application/hta', - 'htc' => 'text/x-component', - 'htt' => 'text/webviewhtml', - 'ief' => 'image/ief', - 'iii' => 'application/x-iphone', - 'ins' => 'application/x-internet-signup', - 'isp' => 'application/x-internet-signup', - 'jfif' => 'image/pipeg', - 'jpe' => 'image/jpeg', - 'jpg' => 'image/jpeg', - 'latex' => 'application/x-latex', - 'lha' => 'application/octet-stream', - 'lsf' => 'video/x-la-asf', - 'lsx' => 'video/x-la-asf', - 'lzh' => 'application/octet-stream', - 'm13' => 'application/x-msmediaview', - 'm14' => 'application/x-msmediaview', - 'm3u' => 'audio/x-mpegurl', - 'man' => 'application/x-troff-man', - 'mdb' => 'application/x-msaccess', - 'me' => 'application/x-troff-me', - 'mht' => 'message/rfc822', - 'mhtml' => 'message/rfc822', - 'mid' => 'audio/mid', - 'mny' => 'application/x-msmoney', - 'movie' => 'video/x-sgi-movie', - 'mp2' => 'video/mpeg', - 'mpa' => 'video/mpeg', - 'mpe' => 'video/mpeg', - 'mpeg' => 'video/mpeg', - 'mpg' => 'video/mpeg', - 'mpp' => 'application/vnd.ms-project', - 'mpv2' => 'video/mpeg', - 'ms' => 'application/x-troff-ms', - 'mvb' => 'application/x-msmediaview', - 'nws' => 'message/rfc822', - 'oda' => 'application/oda', - 'p10' => 'application/pkcs10', - 'p12' => 'application/x-pkcs12', - 'p7b' => 'application/x-pkcs7-certificates', - 'p7c' => 'application/x-pkcs7-mime', - 'p7m' => 'application/x-pkcs7-mime', - 'p7r' => 'application/x-pkcs7-certreqresp', - 'p7s' => 'application/x-pkcs7-signature', - 'pbm' => 'image/x-portable-bitmap', - 'pfx' => 'application/x-pkcs12', - 'pgm' => 'image/x-portable-graymap', - 'pko' => 'application/ynd.ms-pkipko', - 'pma' => 'application/x-perfmon', - 'pmc' => 'application/x-perfmon', - 'pml' => 'application/x-perfmon', - 'pmr' => 'application/x-perfmon', - 'pmw' => 'application/x-perfmon', - 'pnm' => 'image/x-portable-anymap', - 'pot' => 'application/vnd.ms-powerpoint', - 'ppm' => 'image/x-portable-pixmap', - 'pps' => 'application/vnd.ms-powerpoint', - 'prf' => 'application/pics-rules', - 'pub' => 'application/x-mspublisher', - 'ra' => 'audio/x-pn-realaudio', - 'ram' => 'audio/x-pn-realaudio', - 'ras' => 'image/x-cmu-raster', - 'rgb' => 'image/x-rgb', - 'rmi' => 'audio/mid', - 'roff' => 'application/x-troff', - 'rtx' => 'text/richtext', - 'scd' => 'application/x-msschedule', - 'sct' => 'text/scriptlet', - 'setpay' => 'application/set-payment-initiation', - 'setreg' => 'application/set-registration-initiation', - 'sh' => 'application/x-sh', - 'shar' => 'application/x-shar', - 'sit' => 'application/x-stuffit', - 'snd' => 'audio/basic', - 'spc' => 'application/x-pkcs7-certificates', - 'spl' => 'application/futuresplash', - 'src' => 'application/x-wais-source', - 'sst' => 'application/vnd.ms-pkicertstore', - 'stl' => 'application/vnd.ms-pkistl', - 'stm' => 'text/html', - 'sv4cpio' => 'application/x-sv4cpio', - 'sv4crc' => 'application/x-sv4crc', - 't' => 'application/x-troff', - 'tar' => 'application/x-tar', - 'tcl' => 'application/x-tcl', - 'tex' => 'application/x-tex', - 'texi' => 'application/x-texinfo', - 'texinfo' => 'application/x-texinfo', - 'tgz' => 'application/x-compressed', - 'tif' => 'image/tiff', - 'tr' => 'application/x-troff', - 'trm' => 'application/x-msterminal', - 'tsv' => 'text/tab-separated-values', - 'uls' => 'text/iuls', - 'ustar' => 'application/x-ustar', - 'vcf' => 'text/x-vcard', - 'vrml' => 'x-world/x-vrml', - 'wav' => 'audio/x-wav', - 'wcm' => 'application/vnd.ms-works', - 'wdb' => 'application/vnd.ms-works', - 'wks' => 'application/vnd.ms-works', - 'wmf' => 'application/x-msmetafile', - 'wps' => 'application/vnd.ms-works', - 'wri' => 'application/x-mswrite', - 'wrl' => 'x-world/x-vrml', - 'wrz' => 'x-world/x-vrml', - 'xaf' => 'x-world/x-vrml', - 'xbm' => 'image/x-xbitmap', - 'xla' => 'application/vnd.ms-excel', - 'xlc' => 'application/vnd.ms-excel', - 'xlm' => 'application/vnd.ms-excel', - 'xlt' => 'application/vnd.ms-excel', - 'xlw' => 'application/vnd.ms-excel', - 'xof' => 'x-world/x-vrml', - 'xpm' => 'image/x-xpixmap', - 'xwd' => 'image/x-xwindowdump', - 'z' => 'application/x-compress', - ]; + '323' => 'text / h323', + 'acx' => 'application/internet-property-stream', + 'aif' => 'audio/x-aiff', + 'aifc' => 'audio/x-aiff', + 'aiff' => 'audio/x-aiff', + 'asf' => 'video/x-ms-asf', + 'asr' => 'video/x-ms-asf', + 'asx' => 'video/x-ms-asf', + 'au' => 'audio/basic', + 'avi' => 'video/x-msvideo', + 'axs' => 'application/olescript', + 'bas' => 'text/plain', + 'bcpio' => 'application/x-bcpio', + 'bin' => 'application/octet-stream', + 'c' => 'text/plain', + 'cat' => 'application/vnd.ms-pkiseccat', + 'cdf' => 'application/x-cdf', + 'cer' => 'application/x-x509-ca-cert', + 'class' => 'application/octet-stream', + 'clp' => 'application/x-msclip', + 'cmx' => 'image/x-cmx', + 'cod' => 'image/cis-cod', + 'cpio' => 'application/x-cpio', + 'crd' => 'application/x-mscardfile', + 'crl' => 'application/pkix-crl', + 'crt' => 'application/x-x509-ca-cert', + 'csh' => 'application/x-csh', + 'dcr' => 'application/x-director', + 'der' => 'application/x-x509-ca-cert', + 'dir' => 'application/x-director', + 'dll' => 'application/x-msdownload', + 'dms' => 'application/octet-stream', + 'dot' => 'application/msword', + 'dvi' => 'application/x-dvi', + 'dxr' => 'application/x-director', + 'etx' => 'text/x-setext', + 'evy' => 'application/envoy', + 'fif' => 'application/fractals', + 'flr' => 'x-world/x-vrml', + 'gtar' => 'application/x-gtar', + 'gz' => 'application/x-gzip', + 'h' => 'text/plain', + 'hdf' => 'application/x-hdf', + 'hlp' => 'application/winhlp', + 'hqx' => 'application/mac-binhex40', + 'hta' => 'application/hta', + 'htc' => 'text/x-component', + 'htt' => 'text/webviewhtml', + 'ief' => 'image/ief', + 'iii' => 'application/x-iphone', + 'ins' => 'application/x-internet-signup', + 'isp' => 'application/x-internet-signup', + 'jfif' => 'image/pipeg', + 'jpe' => 'image/jpeg', + 'jpg' => 'image/jpeg', + 'latex' => 'application/x-latex', + 'lha' => 'application/octet-stream', + 'lsf' => 'video/x-la-asf', + 'lsx' => 'video/x-la-asf', + 'lzh' => 'application/octet-stream', + 'm13' => 'application/x-msmediaview', + 'm14' => 'application/x-msmediaview', + 'm3u' => 'audio/x-mpegurl', + 'man' => 'application/x-troff-man', + 'mdb' => 'application/x-msaccess', + 'me' => 'application/x-troff-me', + 'mht' => 'message/rfc822', + 'mhtml' => 'message/rfc822', + 'mid' => 'audio/mid', + 'mny' => 'application/x-msmoney', + 'movie' => 'video/x-sgi-movie', + 'mp2' => 'video/mpeg', + 'mpa' => 'video/mpeg', + 'mpe' => 'video/mpeg', + 'mpeg' => 'video/mpeg', + 'mpg' => 'video/mpeg', + 'mpp' => 'application/vnd.ms-project', + 'mpv2' => 'video/mpeg', + 'ms' => 'application/x-troff-ms', + 'mvb' => 'application/x-msmediaview', + 'nws' => 'message/rfc822', + 'oda' => 'application/oda', + 'p10' => 'application/pkcs10', + 'p12' => 'application/x-pkcs12', + 'p7b' => 'application/x-pkcs7-certificates', + 'p7c' => 'application/x-pkcs7-mime', + 'p7m' => 'application/x-pkcs7-mime', + 'p7r' => 'application/x-pkcs7-certreqresp', + 'p7s' => 'application/x-pkcs7-signature', + 'pbm' => 'image/x-portable-bitmap', + 'pfx' => 'application/x-pkcs12', + 'pgm' => 'image/x-portable-graymap', + 'pko' => 'application/ynd.ms-pkipko', + 'pma' => 'application/x-perfmon', + 'pmc' => 'application/x-perfmon', + 'pml' => 'application/x-perfmon', + 'pmr' => 'application/x-perfmon', + 'pmw' => 'application/x-perfmon', + 'pnm' => 'image/x-portable-anymap', + 'pot' => 'application/vnd.ms-powerpoint', + 'ppm' => 'image/x-portable-pixmap', + 'pps' => 'application/vnd.ms-powerpoint', + 'prf' => 'application/pics-rules', + 'pub' => 'application/x-mspublisher', + 'ra' => 'audio/x-pn-realaudio', + 'ram' => 'audio/x-pn-realaudio', + 'ras' => 'image/x-cmu-raster', + 'rgb' => 'image/x-rgb', + 'rmi' => 'audio/mid', + 'roff' => 'application/x-troff', + 'rtx' => 'text/richtext', + 'scd' => 'application/x-msschedule', + 'sct' => 'text/scriptlet', + 'setpay' => 'application/set-payment-initiation', + 'setreg' => 'application/set-registration-initiation', + 'sh' => 'application/x-sh', + 'shar' => 'application/x-shar', + 'sit' => 'application/x-stuffit', + 'snd' => 'audio/basic', + 'spc' => 'application/x-pkcs7-certificates', + 'spl' => 'application/futuresplash', + 'src' => 'application/x-wais-source', + 'sst' => 'application/vnd.ms-pkicertstore', + 'stl' => 'application/vnd.ms-pkistl', + 'stm' => 'text/html', + 'sv4cpio' => 'application/x-sv4cpio', + 'sv4crc' => 'application/x-sv4crc', + 't' => 'application/x-troff', + 'tar' => 'application/x-tar', + 'tcl' => 'application/x-tcl', + 'tex' => 'application/x-tex', + 'texi' => 'application/x-texinfo', + 'texinfo' => 'application/x-texinfo', + 'tgz' => 'application/x-compressed', + 'tif' => 'image/tiff', + 'tr' => 'application/x-troff', + 'trm' => 'application/x-msterminal', + 'tsv' => 'text/tab-separated-values', + 'uls' => 'text/iuls', + 'ustar' => 'application/x-ustar', + 'vcf' => 'text/x-vcard', + 'vrml' => 'x-world/x-vrml', + 'wav' => 'audio/x-wav', + 'wcm' => 'application/vnd.ms-works', + 'wdb' => 'application/vnd.ms-works', + 'wks' => 'application/vnd.ms-works', + 'wmf' => 'application/x-msmetafile', + 'wps' => 'application/vnd.ms-works', + 'wri' => 'application/x-mswrite', + 'wrl' => 'x-world/x-vrml', + 'wrz' => 'x-world/x-vrml', + 'xaf' => 'x-world/x-vrml', + 'xbm' => 'image/x-xbitmap', + 'xla' => 'application/vnd.ms-excel', + 'xlc' => 'application/vnd.ms-excel', + 'xlm' => 'application/vnd.ms-excel', + 'xlt' => 'application/vnd.ms-excel', + 'xlw' => 'application/vnd.ms-excel', + 'xof' => 'x-world/x-vrml', + 'xpm' => 'image/x-xpixmap', + 'xwd' => 'image/x-xwindowdump', + 'z' => 'application/x-compress', + ]; - $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 - * @throws Exception - */ - function request(): Request - { - return Kiri::getDi()->get(Request::class); - } + /** + * @return Request + * @throws Exception + */ + function request(): Request + { + return Kiri::getDi()->get(Request::class); + } } if (!function_exists('storage')) { - /** - * @param string|null $fileName - * @param string|null $path - * @return string - * @throws Exception - */ - function storage(?string $fileName = '', ?string $path = ''): string - { + /** + * @param string|null $fileName + * @param string|null $path + * @return string + * @throws Exception + */ + function storage(?string $fileName = '', ?string $path = ''): string + { - $basePath = rtrim(Kiri::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(Kiri::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; + } } if (!function_exists('event')) { - /** - * @param $name - * @param $callback - * @param bool $isAppend - * @throws Exception - */ - function event($name, $callback, bool $isAppend = true) - { - $pro = di(EventProvider::class); - $pro->on($name, $callback, 0); - } + /** + * @param $name + * @param $callback + * @param bool $isAppend + * @throws Exception + */ + function event($name, $callback, bool $isAppend = true) + { + $pro = di(EventProvider::class); + $pro->on($name, $callback, 0); + } } if (!function_exists('name')) { - /** - * @param int $pid - * @param string|null $prefix - * @throws ConfigException - * @throws Exception - */ - function name(int $pid, string $prefix = null) - { - if (Kiri::getPlatform()->isMac()) { - return; - } + /** + * @param int $pid + * @param string|null $prefix + * @throws ConfigException + * @throws Exception + */ + function name(int $pid, string $prefix = null) + { + if (Kiri::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 - * @throws - */ - function response(): Response - { - if (!Context::hasContext(HttpResponse::class)) { - Context::setContext(HttpResponse::class, new HttpResponse()); - } - return di(Response::class); - } + /** + * @return Response + * @throws + */ + function response(): Response + { + if (!Context::hasContext(HttpResponse::class)) { + Context::setContext(HttpResponse::class, new HttpResponse()); + } + return di(Response::class); + } } 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; + } } @@ -843,50 +881,50 @@ if (!function_exists('env')) { if (!function_exists('di')) { - /** - * @param string $className - * @return mixed - * @throws ReflectionException - * @throws NotFindClassException - */ - function di(string $className): mixed - { - return Kiri::getDi()->get($className); - } + /** + * @param string $className + * @return mixed + * @throws ReflectionException + * @throws NotFindClassException + */ + function di(string $className): mixed + { + return Kiri::getDi()->get($className); + } } if (!function_exists('duplicate')) { - /** - * @param string $className - * @return mixed - * @throws ReflectionException - * @throws NotFindClassException - */ - function duplicate(string $className): mixed - { - $class = di($className); - return clone $class; - } + /** + * @param string $className + * @return mixed + * @throws ReflectionException + * @throws NotFindClassException + */ + function duplicate(string $className): mixed + { + $class = di($className); + return clone $class; + } } if (!function_exists('sweep')) { - /** - * @param string $configPath - * @return array|false|string|null - */ - function sweep(string $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(string $configPath = APP_PATH . 'config'): bool|array|string|null + { + $array = []; + foreach (glob($configPath . '/*') as $config) { + $array = array_merge(require_once "$config", $array); + } + return $array; + } } @@ -894,18 +932,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); // } - } + } } @@ -913,21 +951,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); // } - } + } } @@ -935,15 +973,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); + } } @@ -951,14 +989,14 @@ if (!function_exists('merge')) { if (!function_exists('router')) { - /** - * @return Router - * @throws Exception - */ - function router(): Router - { - return Kiri::app()->getRouter(); - } + /** + * @return Router + * @throws Exception + */ + function router(): Router + { + return Kiri::app()->getRouter(); + } } @@ -966,73 +1004,73 @@ if (!function_exists('router')) { if (!function_exists('isService')) { - /** - * @param string $name - * @return bool - * @throws NotFindClassException - * @throws ReflectionException - */ - function isService(string $name): bool - { - return Kiri::app()->has($name); - } + /** + * @param string $name + * @return bool + * @throws NotFindClassException + * @throws ReflectionException + */ + function isService(string $name): bool + { + return Kiri::app()->has($name); + } } if (!function_exists('getService')) { - /** - * @param string $name - * @return mixed - * @throws Exception - */ - function getService(string $name): mixed - { - return Kiri::app()->get($name); - } + /** + * @param string $name + * @return mixed + * @throws Exception + */ + function getService(string $name): mixed + { + return Kiri::app()->get($name); + } } if (!function_exists('jTraceEx')) { - /** - * @param $e - * @param null $seen - * @param bool $toHtml - * @return string - */ - function jTraceEx($e, $seen = null, bool $toHtml = false): string - { - $starter = $seen ? 'Caused by: ' : ''; - $result = array(); - if (!$seen) $seen = array(); - $trace = $e->getTrace(); - $prev = $e->getPrevious(); - $result[] = sprintf('%s%s: %s', $starter, $e::class, $e->getMessage()); - $file = $e->getFile(); - $line = $e->getLine(); + /** + * @param $e + * @param null $seen + * @param bool $toHtml + * @return string + */ + function jTraceEx($e, $seen = null, bool $toHtml = false): string + { + $starter = $seen ? 'Caused by: ' : ''; + $result = array(); + if (!$seen) $seen = array(); + $trace = $e->getTrace(); + $prev = $e->getPrevious(); + $result[] = sprintf('%s%s: %s', $starter, $e::class, $e->getMessage()); + $file = $e->getFile(); + $line = $e->getLine(); - foreach ($trace as $value) { - $result[] = sprintf(' at %s%s%s(%s%s%s)', - count($value) && array_key_exists('class', $value) ? str_replace('\\', '.', $value['class']) : '', - count($value) && array_key_exists('class', $value) && array_key_exists('function', $value) ? '.' : '', - count($value) && array_key_exists('function', $value) ? str_replace('\\', '.', $value['function']) : '(main)', - $line === null ? $file : basename($file), - $line === null ? '' : ':', - $line === null ? '' : $line); + foreach ($trace as $value) { + $result[] = sprintf(' at %s%s%s(%s%s%s)', + count($value) && array_key_exists('class', $value) ? str_replace('\\', '.', $value['class']) : '', + count($value) && array_key_exists('class', $value) && array_key_exists('function', $value) ? '.' : '', + count($value) && array_key_exists('function', $value) ? str_replace('\\', '.', $value['function']) : '(main)', + $line === null ? $file : basename($file), + $line === null ? '' : ':', + $line === null ? '' : $line); - $file = array_key_exists('file', $value) ? $value['file'] : 'Unknown Source'; - $line = array_key_exists('file', $value) && array_key_exists('line', $value) && $value['line'] ? $value['line'] : null; - } - $result = join($toHtml ? "
" : "\n", $result); - if ($prev) { - $result .= ($toHtml ? "
" : "\n") . jTraceEx($prev, $seen, $toHtml); - } + $file = array_key_exists('file', $value) ? $value['file'] : 'Unknown Source'; + $line = array_key_exists('file', $value) && array_key_exists('line', $value) && $value['line'] ? $value['line'] : null; + } + $result = join($toHtml ? "
" : "\n", $result); + if ($prev) { + $result .= ($toHtml ? "
" : "\n") . jTraceEx($prev, $seen, $toHtml); + } - return $result; - } + return $result; + } } @@ -1041,85 +1079,85 @@ if (!function_exists('jTraceEx')) { if (!function_exists('swoole_substr_json_decode')) { - /** - * @param $packet - * @param int $length - * @return mixed - */ - function swoole_substr_json_decode($packet, int $length = 0): mixed - { - return json_decode($packet, true); - } + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_json_decode($packet, int $length = 0): mixed + { + return json_decode($packet, true); + } } if (!function_exists('swoole_substr_unserialize')) { - /** - * @param $packet - * @param int $length - * @return mixed - */ - function swoole_substr_unserialize($packet, int $length = 0): mixed - { - return unserialize($packet); - } + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_unserialize($packet, int $length = 0): mixed + { + return unserialize($packet); + } } if (!function_exists('debug')) { - /** - * @param mixed $message - * @param string $method - * @throws Exception - */ - function debug(mixed $message, string $method = 'app') - { - Kiri::app()->debug($message, $method); - } + /** + * @param mixed $message + * @param string $method + * @throws Exception + */ + function debug(mixed $message, string $method = 'app') + { + Kiri::app()->debug($message, $method); + } } if (!function_exists('info')) { - /** - * @param mixed $message - * @param string $method - * @throws Exception - */ - function info(mixed $message, string $method = 'app') - { - Kiri::app()->info($message, $method); - } + /** + * @param mixed $message + * @param string $method + * @throws Exception + */ + function info(mixed $message, string $method = 'app') + { + Kiri::app()->info($message, $method); + } } if (!function_exists('error')) { - /** - * @param mixed $message - * @param string $method - * @throws Exception - */ - function error(mixed $message, string $method = 'error') - { - Kiri::getDi()->get(LoggerInterface::class)->error($method, [$message]); - } + /** + * @param mixed $message + * @param string $method + * @throws Exception + */ + function error(mixed $message, string $method = 'error') + { + Kiri::getDi()->get(LoggerInterface::class)->error($method, [$message]); + } } if (!function_exists('success')) { - /** - * @param mixed $message - * @param string $method - * @throws Exception - */ - function success(mixed $message, string $method = 'app') - { - Kiri::app()->success($message, $method); - } + /** + * @param mixed $message + * @param string $method + * @throws Exception + */ + function success(mixed $message, string $method = 'app') + { + Kiri::app()->success($message, $method); + } } diff --git a/http-helper/Command.php b/http-helper/Command.php index b4e6654e..2785bcd8 100644 --- a/http-helper/Command.php +++ b/http-helper/Command.php @@ -16,48 +16,54 @@ use Kiri\Kiri; class Command extends \Console\Command { - public string $command = 'sw:server'; + public string $command = 'sw:server'; - public string $description = 'server start|stop|reload|restart'; + public string $description = 'server start|stop|reload|restart'; - const ACTIONS = ['start', 'stop', 'restart']; + const ACTIONS = ['start', 'stop', 'restart']; - /** - * @param Input $dtl - * @return string - * @throws Exception - * @throws ConfigException - */ - public function onHandler(Input $dtl): string - { - $manager = Kiri::app()->getServer(); - $manager->setDaemon($dtl->get('daemon', 0)); - if (!in_array($dtl->get('action'), self::ACTIONS)) { - return 'I don\'t know what I want to do.'; - } - if ($manager->isRunner() && $dtl->get('action') == 'start') { - return 'Service is running. Please use restart.'; - } - - $manager->shutdown(); - if ($dtl->get('action') == 'stop') { - return 'shutdown success.'; - } - - $this->generate_runtime_builder(); - return $manager->start(); - } + /** + * @param Input $dtl + * @return string + * @throws Exception + * @throws ConfigException + */ + public function onHandler(Input $dtl): string + { + $manager = Kiri::app()->getServer(); + $manager->setDaemon($dtl->get('daemon', 0)); + if (!in_array($dtl->get('action'), self::ACTIONS)) { + return 'I don\'t know what I want to do.'; + } + if ($manager->isRunner() && $dtl->get('action') == 'start') { + return 'Service is running. Please use restart.'; + } + $manager->shutdown(); + if ($dtl->get('action') == 'stop') { + return 'shutdown success.'; + } + return $this->generate_runtime_builder($manager); + } - /** - * - */ - private function generate_runtime_builder() - { - exec(PHP_BINARY . ' ' . APP_PATH . 'snowflake runtime:builder'); - } + /** + * @param $manager + * @return mixed + * @throws \Kiri\Exception\NotFindClassException + * @throws \ReflectionException + */ + private function generate_runtime_builder($manager) + { + exec(PHP_BINARY . ' ' . APP_PATH . 'kiri.php runtime:builder'); + if (is_enable_file_modification_listening()) { + scan_directory(directory('app'), 'App'); + $loader = Kiri::app()->getRouter(); + $loader->_loader(); + } + return $manager->start(); + } } diff --git a/http-server/Worker/OnServerWorker.php b/http-server/Worker/OnServerWorker.php index fb4a4ee2..dcb7f582 100644 --- a/http-server/Worker/OnServerWorker.php +++ b/http-server/Worker/OnServerWorker.php @@ -69,7 +69,7 @@ class OnServerWorker extends \Server\Abstracts\Server Config::sets(unserialize($serialize)); } $this->workerInitExecutor($server, $workerId); - if (env('enableFileChange', 'off') == 'off') { + if (is_enable_file_modification_listening()) { $annotation = Kiri::app()->getAnnotation(); $annotation->read(APP_PATH . 'app', 'App', $workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH] @@ -115,15 +115,15 @@ class OnServerWorker extends \Server\Abstracts\Server private function workerInitExecutor(Server $server, int $workerId) { if ($workerId < $server->setting['worker_num']) { - if (env('enableFileChange', 'off') == 'off') { - $loader = Kiri::app()->getRouter(); - $loader->_loader(); - } - putenv('environmental=' . Kiri::WORKER); echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m Worker[%d].%d start.", $server->worker_pid, $workerId) . PHP_EOL; $this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId)); + + if (is_enable_file_modification_listening()) { + $loader = Kiri::app()->getRouter(); + $loader->_loader(); + } } else { putenv('environmental=' . Kiri::TASK);