Files
kiri-core/function.php
T

1139 lines
23 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2021-03-05 16:36:18 +08:00
defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
2020-08-31 01:27:08 +08:00
2021-02-22 17:44:24 +08:00
use Annotation\Annotation;
2021-08-17 16:52:50 +08:00
use Http\Context\Context;
use Http\Context\HttpParams;
use Http\Context\Response as HttpResponse;
2021-08-17 16:43:50 +08:00
use Http\Route\Router;
2021-03-05 16:57:12 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-11 01:04:57 +08:00
use Kiri\Abstracts\Config;
use Kiri\Aop;
use Kiri\Application;
use Kiri\Core\ArrayAccess;
use Kiri\Error\Logger;
2021-08-13 14:58:58 +08:00
use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
2021-08-11 01:04:57 +08:00
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
2021-08-11 11:19:32 +08:00
use Server\Constrict\Request;
use Server\Constrict\Response;
2021-03-31 18:19:45 +08:00
use Swoole\WebSocket\Server;
2020-08-31 01:27:08 +08:00
if (!function_exists('make')) {
2021-04-07 14:15:50 +08:00
/**
* @param $name
* @param $default
* @return mixed
* @throws
*/
function make($name, $default = null): mixed
{
if (class_exists($name)) {
2021-08-11 01:04:57 +08:00
return Kiri::createObject($name);
2021-04-07 14:15:50 +08:00
}
2021-08-11 01:04:57 +08:00
if (Kiri::has($name)) {
return Kiri::app()->get($name);
2021-04-07 14:15:50 +08:00
}
if (empty($default)) {
throw new Exception("Unknown component ID: $name");
}
2021-08-11 01:04:57 +08:00
if (Kiri::has($default)) {
return Kiri::app()->get($default);
2021-04-07 14:15:50 +08:00
}
2021-08-11 01:04:57 +08:00
$class = Kiri::createObject($default);
2021-04-07 14:15:50 +08:00
class_alias($name, $default, true);
return $class;
}
2020-08-31 01:27:08 +08:00
2020-09-15 19:26:44 +08:00
}
2021-02-27 04:48:44 +08:00
if (!function_exists('workerName')) {
2021-08-04 15:39:06 +08:00
/**
* @param $worker_id
* @return string
*/
2021-04-07 14:15:50 +08:00
function workerName($worker_id)
{
2021-08-11 01:04:57 +08:00
return $worker_id >= Kiri::app()->getSwoole()->setting['worker_num'] ? 'Task' : 'Worker';
2021-04-07 14:15:50 +08:00
}
2021-02-27 04:48:44 +08:00
}
2020-09-23 16:28:54 +08:00
2021-02-22 17:44:24 +08:00
if (!function_exists('annotation')) {
2021-04-07 14:15:50 +08:00
/**
* @return Annotation
* @throws Exception
*/
function annotation(): Annotation
{
2021-08-11 01:04:57 +08:00
return Kiri::getAnnotation();
2021-04-07 14:15:50 +08:00
}
2021-03-03 19:05:57 +08:00
}
2021-04-08 11:24:31 +08:00
if (!function_exists('scan_directory')) {
/**
* @param $dir
* @param $namespace
2021-08-05 15:55:27 +08:00
* @param array $exclude
* @throws NotFindClassException
* @throws ReflectionException
2021-04-08 11:24:31 +08:00
* @throws Exception
*/
2021-08-05 15:55:27 +08:00
function scan_directory($dir, $namespace, array $exclude = [])
2021-04-08 11:24:31 +08:00
{
2021-08-11 01:04:57 +08:00
$annotation = Kiri::app()->getAnnotation();
2021-08-05 16:30:45 +08:00
$annotation->read($dir, $namespace, $exclude);
2021-08-05 15:55:27 +08:00
injectRuntime($dir, $exclude);
}
}
if (!function_exists('injectRuntime')) {
/**
* @param string $path
* @param array $exclude
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
function injectRuntime(string $path, array $exclude = [])
{
2021-08-11 01:04:57 +08:00
$fileLists = Kiri::getAnnotation()->runtime($path, $exclude);
$di = Kiri::getDi();
2021-08-05 15:55:27 +08:00
foreach ($fileLists as $class) {
2021-08-05 17:18:43 +08:00
foreach ($di->getTargetNote($class) as $value) {
2021-08-13 15:22:11 +08:00
$value->execute($class);
2021-08-05 17:18:43 +08:00
}
2021-08-05 15:55:27 +08:00
$methods = $di->getMethodAttribute($class);
foreach ($methods as $method => $attribute) {
if (empty($attribute)) {
continue;
}
foreach ($attribute as $item) {
2021-08-13 15:22:11 +08:00
$item->execute($class, $method);
2021-08-05 15:55:27 +08:00
}
}
}
2021-04-08 11:24:31 +08:00
}
}
2021-03-31 18:19:45 +08:00
if (!function_exists('swoole')) {
2021-04-07 14:15:50 +08:00
/**
* @return Server|null
* @throws Exception
*/
function swoole(): ?Server
{
2021-08-11 01:04:57 +08:00
return Kiri::getWebSocket();
2021-04-07 14:15:50 +08:00
}
2021-03-31 18:19:45 +08:00
}
2021-03-05 16:57:12 +08:00
if (!function_exists('directory')) {
2021-04-07 14:15:50 +08:00
/**
* @param $name
* @return string
*/
#[Pure] function directory($name): string
{
return realpath(APP_PATH . $name);
}
2021-03-05 16:57:12 +08:00
}
2020-09-23 16:28:54 +08:00
if (!function_exists('isUrl')) {
2021-04-07 14:15:50 +08:00
/**
* @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]);
2020-09-23 16:28:54 +08:00
2021-04-07 14:15:50 +08:00
[$isHttps, $domain, $port, $path] = [$outPut[2] == 'https', $outPut[3], $port, $outPut[6] ?? ''];
if ($isHttps && empty($port)) {
$port = 443;
}
2020-09-23 16:28:54 +08:00
2021-04-07 14:15:50 +08:00
unset($outPut);
2020-09-23 16:28:54 +08:00
2021-04-07 14:15:50 +08:00
return [$isHttps == 'https', $domain, $port, $path];
}
2020-09-23 16:28:54 +08:00
}
if (!function_exists('split_request_uri')) {
2021-04-07 14:15:50 +08:00
/**
* @param $url
* @return false|array
*/
function split_request_uri($url): bool|array
{
if (($parse = isUrl($url, null)) === false) {
return false;
}
2020-09-23 16:28:54 +08:00
2021-04-07 14:15:50 +08:00
[$isHttps, $domain, $port, $path] = $parse;
$uri = $isHttps ? 'https://' . $domain : 'http://' . $domain;
if (!empty($port)) {
$uri .= ':' . $port;
}
return [$uri, $path];
}
2020-09-23 16:28:54 +08:00
}
if (!function_exists('hadDomain')) {
2021-04-07 14:15:50 +08:00
/**
* @param $url
* @return false|array
*/
function hadDomain($url): bool|array
{
$param = split_request_uri($url);
return !is_array($param) ? false : $param[0];
}
2020-09-23 16:28:54 +08:00
}
if (!function_exists('isDomain')) {
2021-04-07 14:15:50 +08:00
/**
* @param $url
* @return false|array
*/
function isDomain($url): array|bool
{
return !isIp($url);
}
2020-09-23 16:28:54 +08:00
}
if (!function_exists('isIp')) {
2021-04-07 14:15:50 +08:00
/**
* @param $url
* @return false|array
*/
function isIp($url): bool|array
{
return preg_match('/(\d{1,3}\.){3}\.\d{1,3}(:\d{1,5})?/', $url);
}
2020-09-23 16:28:54 +08:00
}
2020-09-15 19:26:44 +08:00
if (!function_exists('loadByDir')) {
2021-04-07 14:15:50 +08:00
/**
* @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;
}
2020-09-15 20:11:41 +08:00
2021-04-07 14:15:50 +08:00
$replace = ltrim(str_replace(__DIR__, '', $value), '/');
$replace = str_replace('.php', '', $replace);
2020-09-15 20:11:41 +08:00
2021-04-07 14:15:50 +08:00
$first = explode(DIRECTORY_SEPARATOR, $replace);
array_shift($first);
2020-09-15 20:11:41 +08:00
2021-08-11 01:04:57 +08:00
Kiri::setAutoload($namespace . '\\' . implode('\\', $first), $value);
2021-04-07 14:15:50 +08:00
}
}
}
2020-09-15 19:26:44 +08:00
2020-08-31 01:27:08 +08:00
}
2020-09-16 20:23:23 +08:00
if (!function_exists('write')) {
2021-04-07 14:15:50 +08:00
/**
* @param string $messages
* @param string $category
* @throws Exception
*/
2021-07-12 18:15:58 +08:00
function write(string $messages, string $category = 'app')
2021-04-07 14:15:50 +08:00
{
2021-08-11 01:04:57 +08:00
$logger = Kiri::app()->getLogger();
2021-04-07 14:15:50 +08:00
$logger->write($messages, $category);
}
2021-04-03 02:31:10 +08:00
}
if (!function_exists('redis')) {
2021-04-07 14:15:50 +08:00
/**
2021-08-11 01:04:57 +08:00
* @return \Kiri\Cache\Redis|Redis
2021-04-07 14:15:50 +08:00
* @throws Exception
*/
2021-08-11 01:04:57 +08:00
function redis(): \Kiri\Cache\Redis|Redis
2021-04-07 14:15:50 +08:00
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->getRedis();
2021-04-07 14:15:50 +08:00
}
2020-09-16 20:23:23 +08:00
}
2020-10-30 11:04:04 +08:00
if (!function_exists('fire')) {
2021-04-07 14:15:50 +08:00
/**
2021-08-13 14:58:58 +08:00
* @param object $event
* @throws NotFindClassException
* @throws ReflectionException
2021-04-07 14:15:50 +08:00
*/
2021-08-13 14:58:58 +08:00
function fire(object $event)
2021-04-07 14:15:50 +08:00
{
2021-08-13 14:58:58 +08:00
di(EventDispatch::class)->dispatch($event);
2021-04-07 14:15:50 +08:00
}
2020-10-30 11:04:04 +08:00
}
2021-03-29 03:19:22 +08:00
if (!function_exists('aop')) {
2021-04-07 14:15:50 +08:00
/**
* @param mixed $handler
* @param array $params
* @return mixed
* @throws Exception
*/
function aop(mixed $handler, array $params = []): mixed
{
2021-08-11 01:04:57 +08:00
return Kiri::getDi()->get(Aop::class)->dispatch($handler, $params);
2021-04-07 14:15:50 +08:00
}
2021-02-23 17:43:13 +08:00
}
2021-02-23 17:19:46 +08:00
2021-03-30 11:56:29 +08:00
if (!function_exists('app')) {
2021-04-07 14:15:50 +08:00
/**
* @return Application|null
*/
#[Pure] function app(): ?Application
{
2021-08-11 01:04:57 +08:00
return Kiri::app();
2021-04-07 14:15:50 +08:00
}
2021-03-30 11:56:29 +08:00
}
2020-09-15 20:17:14 +08:00
if (!function_exists('instance_load')) {
2021-04-07 14:15:50 +08:00
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);
}
}
}
2020-09-15 20:17:14 +08:00
}
2020-08-31 12:38:32 +08:00
if (!function_exists('exif_imagetype')) {
2021-04-07 14:15:50 +08:00
/**
* @param $name
* @return string
*/
function exif_imagetype($name): string
{
return get_file_extension($name);
}
2020-08-31 12:38:32 +08:00
}
2020-09-11 15:01:22 +08:00
if (!function_exists('logger')) {
2021-04-07 14:15:50 +08:00
/**
* @return Logger
* @throws Exception
*/
function logger(): Logger
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->getLogger();
2021-04-07 14:15:50 +08:00
}
}
2021-06-09 15:37:23 +08:00
2021-06-09 15:41:13 +08:00
if (!function_exists('trim_blank')) {
2021-06-09 15:37:23 +08:00
/**
2021-06-09 15:40:23 +08:00
* 空白字符替换
* @param string $content 内容
* @param int $len 截取长度
* @param string $encode 编码
2021-06-09 15:37:23 +08:00
* @param bool $htmlTags
2021-06-09 15:40:23 +08:00
* @return array|string|null
2021-06-09 15:37:23 +08:00
*/
2021-07-28 14:09:39 +08:00
function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = true): array|string|null
2021-06-09 15:37:23 +08:00
{
$str = trim($content);
if ($htmlTags) {
$str = strip_tags($str);
}
$str = preg_replace('/[\n|\r|\t]+/', '', $str);
$str = preg_replace("/(\s|\&nbsp\;| |\xc2\xa0)/", '', $str);
if ($len > 0) {
return mb_substr($str, 0, $len, $encode);
} else {
return $str;
}
}
}
2020-08-31 12:38:32 +08:00
if (!function_exists('get_file_extension')) {
2021-04-07 14:15:50 +08:00
function get_file_extension($filename)
{
2021-08-11 11:19:32 +08:00
$mime_types = [
2021-04-07 14:15:50 +08:00
'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',
// 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',
// 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',
// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
2021-08-11 11:19:32 +08:00
'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',
];
2021-04-07 14:15:50 +08:00
$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';
}
}
2020-08-31 12:38:32 +08:00
}
if (!function_exists('request')) {
2021-04-07 14:15:50 +08:00
/**
* @return Request
2021-04-08 15:05:44 +08:00
* @throws Exception
2021-04-07 14:15:50 +08:00
*/
function request(): Request
{
2021-08-11 01:04:57 +08:00
return Kiri::getDi()->get(Request::class);
2021-04-07 14:15:50 +08:00
}
2020-08-31 12:38:32 +08:00
}
if (!function_exists('Input')) {
2021-04-07 14:15:50 +08:00
/**
* @return HttpParams
2021-04-08 15:05:44 +08:00
* @throws Exception
2021-04-07 14:15:50 +08:00
*/
function Input(): HttpParams
{
return request()->params;
}
2020-08-31 12:38:32 +08:00
}
2020-08-31 01:27:08 +08:00
if (!function_exists('storage')) {
2021-04-07 14:15:50 +08:00
/**
2021-07-28 14:10:24 +08:00
* @param string|null $fileName
* @param string|null $path
2021-04-07 14:15:50 +08:00
* @return string
* @throws Exception
*/
2021-07-28 14:10:24 +08:00
function storage(?string $fileName = '', ?string $path = ''): string
2021-04-07 14:15:50 +08:00
{
2021-08-11 01:04:57 +08:00
$basePath = rtrim(Kiri::getStoragePath(), '/');
2021-04-07 14:15:50 +08:00
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;
}
2020-08-31 01:27:08 +08:00
}
2021-04-04 17:12:41 +08:00
if (!function_exists('event')) {
2021-04-07 14:15:50 +08:00
/**
* @param $name
* @param $callback
2021-07-21 11:25:31 +08:00
* @param bool $isAppend
2021-04-07 14:15:50 +08:00
* @throws Exception
*/
2021-08-03 18:18:09 +08:00
function event($name, $callback, bool $isAppend = true)
2021-04-07 14:15:50 +08:00
{
2021-08-13 14:58:58 +08:00
$pro = di(EventProvider::class);
$pro->on($name, $callback,0);
2021-04-07 14:15:50 +08:00
}
2021-04-04 17:12:41 +08:00
}
2020-08-31 01:27:08 +08:00
if (!function_exists('alias')) {
2021-04-07 14:15:50 +08:00
/**
* @param $class
* @param $name
*/
function alias($class, $name)
{
2021-08-11 01:04:57 +08:00
Kiri::setAlias($class, $name);
2021-04-07 14:15:50 +08:00
}
2020-08-31 01:27:08 +08:00
}
if (!function_exists('name')) {
2021-04-07 14:15:50 +08:00
/**
* @param int $pid
* @param string|null $prefix
* @throws ConfigException
* @throws Exception
*/
function name(int $pid, string $prefix = null)
{
2021-08-11 01:04:57 +08:00
if (Kiri::getPlatform()->isMac()) {
2021-04-07 14:15:50 +08:00
return;
}
2021-03-09 19:40:10 +08:00
2021-04-07 14:15:50 +08:00
$name = Config::get('id', 'system') . '[' . $pid . ']';
if (!empty($prefix)) {
$name .= '.' . $prefix;
}
swoole_set_process_name($name);
}
2020-08-31 01:27:08 +08:00
}
if (!function_exists('response')) {
2021-04-07 14:15:50 +08:00
/**
2021-08-04 02:19:17 +08:00
* @return Response
2021-04-07 14:15:50 +08:00
* @throws
*/
2021-08-04 02:19:17 +08:00
function response(): Response
2021-04-07 14:15:50 +08:00
{
2021-08-05 15:55:27 +08:00
if (!Context::hasContext(HttpResponse::class)) {
Context::setContext(HttpResponse::class, new HttpResponse());
}
2021-08-04 17:17:22 +08:00
return di(Response::class);
2021-04-07 14:15:50 +08:00
}
2020-09-09 11:44:14 +08:00
}
2021-03-21 02:15:02 +08:00
if (!function_exists('zero_full')) {
2021-04-07 14:15:50 +08:00
function zero_full(int $data = 1, int $length = 10): string
{
return sprintf('%0' . $length . 'd', $data);
}
2021-03-21 02:15:02 +08:00
}
2020-08-31 01:27:08 +08:00
if (!function_exists('redirect')) {
2021-01-15 15:53:10 +08:00
2021-04-07 14:15:50 +08:00
/**
* @param $url
* @return int
*/
function redirect($url): int
{
return response()->redirect($url);
}
2020-08-31 01:27:08 +08:00
}
if (!function_exists('env')) {
2021-04-07 14:15:50 +08:00
/**
* @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;
}
2020-08-31 01:27:08 +08:00
}
2020-08-31 01:59:30 +08:00
2021-07-21 13:49:55 +08:00
if (!function_exists('di')) {
/**
* @param string $className
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
function di(string $className): mixed
{
2021-08-11 01:04:57 +08:00
return Kiri::getDi()->get($className);
2021-07-21 13:49:55 +08:00
}
}
if (!function_exists('duplicate')) {
/**
* @param string $className
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
*/
function duplicate(string $className): mixed
{
$class = di($className);
2021-07-21 14:46:48 +08:00
return clone $class;
2021-07-21 13:49:55 +08:00
}
}
2020-08-31 01:59:30 +08:00
if (!function_exists('sweep')) {
2021-04-07 14:15:50 +08:00
/**
* @param string $configPath
* @return array|false|string|null
*/
2021-07-21 13:49:55 +08:00
function sweep(string $configPath = APP_PATH . 'config'): bool|array|string|null
2021-04-07 14:15:50 +08:00
{
$array = [];
foreach (glob($configPath . '/*') as $config) {
2021-05-17 18:33:40 +08:00
$array = array_merge(require_once "$config", $array);
2021-04-07 14:15:50 +08:00
}
return $array;
}
2021-03-24 17:57:08 +08:00
}
if (!function_exists('swoole_serialize')) {
2021-04-07 14:15:50 +08:00
/**
* @param $data
* @return string
*/
function swoole_serialize($data): string
{
2021-03-26 01:52:36 +08:00
// if (class_exists('swoole_serialize')) {
// return \swoole_serialize::pack($data);
// } else {
2021-04-07 14:15:50 +08:00
return serialize($data);
2021-03-26 01:52:36 +08:00
// }
2021-04-07 14:15:50 +08:00
}
2021-03-24 17:57:08 +08:00
}
if (!function_exists('swoole_unserialize')) {
2021-04-07 14:15:50 +08:00
/**
* @param $data
* @return string
*/
function swoole_unserialize($data): mixed
{
if (empty($data)) {
return null;
}
2021-03-26 01:52:36 +08:00
// if (class_exists('swoole_serialize')) {
// return \swoole_serialize::unpack($data);
// } else {
2021-04-07 14:15:50 +08:00
return unserialize($data);
2021-03-26 01:52:36 +08:00
// }
2021-04-07 14:15:50 +08:00
}
2020-08-31 01:59:30 +08:00
}
2020-09-02 20:07:02 +08:00
if (!function_exists('merge')) {
2021-04-07 14:15:50 +08:00
/**
* @param $param
* @param $param1
* @return array
*/
function merge($param, $param1): array
{
return ArrayAccess::merge($param, $param1);
}
2020-09-02 20:07:02 +08:00
}
2020-09-03 18:51:13 +08:00
2020-10-29 18:17:25 +08:00
if (!function_exists('router')) {
2021-04-07 14:15:50 +08:00
/**
* @return Router
* @throws Exception
*/
function router(): Router
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->getRouter();
2021-04-07 14:15:50 +08:00
}
2020-10-29 18:17:25 +08:00
}
2021-04-25 16:21:18 +08:00
if (!function_exists('isService')) {
/**
* @param string $name
* @return bool
2021-08-05 19:14:08 +08:00
* @throws NotFindClassException
* @throws ReflectionException
2021-04-25 16:21:18 +08:00
*/
2021-08-05 19:14:08 +08:00
function isService(string $name): bool
2021-04-25 16:21:18 +08:00
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->has($name);
2021-04-25 16:21:18 +08:00
}
}
if (!function_exists('getService')) {
/**
* @param string $name
* @return mixed
* @throws Exception
*/
function getService(string $name): mixed
{
2021-08-11 01:04:57 +08:00
return Kiri::app()->get($name);
2021-04-25 16:21:18 +08:00
}
}
2020-09-03 18:51:13 +08:00
if (!function_exists('jTraceEx')) {
2021-04-07 14:15:50 +08:00
/**
* @param $e
* @param null $seen
2021-08-02 11:20:41 +08:00
* @param bool $toHtml
2021-04-07 14:15:50 +08:00
* @return string
*/
2021-08-02 11:20:41 +08:00
function jTraceEx($e, $seen = null, bool $toHtml = false): string
2021-04-07 14:15:50 +08:00
{
$starter = $seen ? 'Caused by: ' : '';
$result = array();
if (!$seen) $seen = array();
$trace = $e->getTrace();
$prev = $e->getPrevious();
2021-04-25 11:22:23 +08:00
$result[] = sprintf('%s%s: %s', $starter, $e::class, $e->getMessage());
2021-04-07 14:15:50 +08:00
$file = $e->getFile();
$line = $e->getLine();
2021-04-28 10:43:16 +08:00
foreach ($trace as $value) {
2021-04-07 14:15:50 +08:00
$result[] = sprintf(' at %s%s%s(%s%s%s)',
2021-04-28 10:43:16 +08:00
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)',
2021-04-07 14:15:50 +08:00
$line === null ? $file : basename($file),
$line === null ? '' : ':',
$line === null ? '' : $line);
2021-04-28 10:43:16 +08:00
$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;
2021-04-07 14:15:50 +08:00
}
2021-07-27 14:11:04 +08:00
$result = join($toHtml ? "<br>" : "\n", $result);
2021-04-28 10:43:16 +08:00
if ($prev) {
2021-07-27 14:11:04 +08:00
$result .= ($toHtml ? "<br>" : "\n") . jTraceEx($prev, $seen, $toHtml);
2021-04-28 10:43:16 +08:00
}
2021-04-07 14:15:50 +08:00
return $result;
}
2020-09-03 18:51:13 +08:00
}
2020-11-04 19:28:40 +08:00
if (!function_exists('swoole_substr_json_decode')) {
2021-04-07 14:15:50 +08:00
/**
* @param $packet
* @param int $length
* @return mixed
*/
2021-07-23 10:48:18 +08:00
function swoole_substr_json_decode($packet, int $length = 0): mixed
2021-04-07 14:15:50 +08:00
{
return json_decode($packet, true);
}
2020-11-04 19:28:40 +08:00
}
if (!function_exists('swoole_substr_unserialize')) {
2021-04-07 14:15:50 +08:00
/**
* @param $packet
* @param int $length
* @return mixed
*/
2021-07-23 10:48:18 +08:00
function swoole_substr_unserialize($packet, int $length = 0): mixed
2021-04-07 14:15:50 +08:00
{
return unserialize($packet);
}
2020-11-04 19:28:40 +08:00
}
2021-05-08 16:28:50 +08:00
if (!function_exists('debug')) {
/**
2021-07-23 10:48:18 +08:00
* @param mixed $message
2021-05-08 16:28:50 +08:00
* @param string $method
* @throws Exception
*/
function debug(mixed $message, string $method = 'app')
{
2021-08-11 01:04:57 +08:00
Kiri::app()->debug($message, $method);
2021-05-08 16:28:50 +08:00
}
}
2021-08-20 10:19:58 +08:00
if (!function_exists('info')) {
/**
* @param mixed $message
* @param string $method
* @throws Exception
*/
function info(mixed $message, string $method = 'app')
{
Kiri::app()->info($message, $method);
}
}
2021-05-08 16:28:50 +08:00
if (!function_exists('error')) {
/**
2021-07-23 10:48:18 +08:00
* @param mixed $message
2021-05-08 16:28:50 +08:00
* @param string $method
* @throws Exception
*/
2021-07-23 10:48:18 +08:00
function error(mixed $message, string $method = 'error')
2021-05-08 16:28:50 +08:00
{
2021-08-11 01:04:57 +08:00
Kiri::app()->error($message, $method);
2021-05-08 16:28:50 +08:00
}
}
if (!function_exists('success')) {
/**
2021-07-23 10:48:18 +08:00
* @param mixed $message
2021-05-08 16:28:50 +08:00
* @param string $method
* @throws Exception
*/
2021-07-23 10:48:18 +08:00
function success(mixed $message, string $method = 'app')
2021-05-08 16:28:50 +08:00
{
2021-08-11 01:04:57 +08:00
Kiri::app()->success($message, $method);
2021-05-08 16:28:50 +08:00
}
}