2022-01-09 03:50:38 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use JetBrains\PhpStorm\Pure;
|
|
|
|
|
|
use Kiri\Abstracts\Config;
|
|
|
|
|
|
use Kiri\Core\ArrayAccess;
|
2022-02-23 16:32:08 +08:00
|
|
|
|
use Kiri\Error\StdoutLoggerInterface;
|
2022-01-09 03:50:38 +08:00
|
|
|
|
use Kiri\Events\EventDispatch;
|
|
|
|
|
|
use Kiri\Events\EventProvider;
|
|
|
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
|
use Swoole\Process;
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('make')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $name
|
|
|
|
|
|
* @param $default
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws
|
|
|
|
|
|
*/
|
|
|
|
|
|
function make($name, $default = NULL): mixed
|
|
|
|
|
|
{
|
2022-06-22 16:29:42 +08:00
|
|
|
|
if (!class_exists($name) && !interface_exists($name)) {
|
|
|
|
|
|
return Kiri::getDi()->get($default);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2022-06-22 16:29:42 +08:00
|
|
|
|
return Kiri::getDi()->get($name);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('service')) {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param string $name
|
|
|
|
|
|
* @return mixed|null
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function service(string $name): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return Kiri::service()->get($name);
|
|
|
|
|
|
}
|
2022-09-23 18:59:35 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2023-04-07 00:11:29 +08:00
|
|
|
|
if (!function_exists('replica')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-04-07 00:05:01 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $name
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws
|
|
|
|
|
|
*/
|
2023-04-07 00:11:29 +08:00
|
|
|
|
function replica($name): mixed
|
2023-04-07 00:05:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
return Kiri::getDi()->copy($name);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2023-04-07 00:05:01 +08:00
|
|
|
|
}
|
2022-09-23 18:59:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('isJson')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-09-23 18:59:35 +08:00
|
|
|
|
function isJson(?string $string): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if (is_null($string)) return false;
|
2022-09-23 19:00:53 +08:00
|
|
|
|
return (str_starts_with($string, '{') && str_ends_with($string, '}'))
|
|
|
|
|
|
|| (str_ends_with($string, '[') && str_starts_with($string, ']'));
|
2022-09-23 18:59:35 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('instance')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $class
|
|
|
|
|
|
* @param array $constrict
|
|
|
|
|
|
* @param array $config
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
function instance($class, array $constrict = [], array $config = []): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return Kiri::getDi()->create($class, $constrict, $config);
|
2022-06-16 17:38:23 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-06-16 17:38:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('call')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-06-16 17:38:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $handler
|
|
|
|
|
|
* @param mixed ...$params
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function call($handler, ...$params): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
if (is_array($handler) && is_string($handler[0])) {
|
|
|
|
|
|
$handler[0] = di($handler[0]);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
|
if (!is_callable($handler, true)) {
|
|
|
|
|
|
throw new Exception('Call function not exists.');
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2022-06-16 17:38:23 +08:00
|
|
|
|
return call_user_func($handler, ...$params);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('map')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param array $params
|
|
|
|
|
|
* @param Closure $closure
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
function map(array $params, Closure $closure): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return array_map($closure, $params);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('checkPortIsAlready')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $port
|
|
|
|
|
|
* @return bool|string
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function checkPortIsAlready($port): bool|string
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Kiri::getPlatform()->isLinux()) {
|
|
|
|
|
|
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
|
|
|
|
|
if (empty($output)) return FALSE;
|
|
|
|
|
|
$output = explode(PHP_EOL, $output[0]);
|
|
|
|
|
|
return $output[0];
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$serverPid = file_get_contents(storage('.swoole.pid'));
|
|
|
|
|
|
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
|
|
|
|
|
|
Process::kill($serverPid, 0) && Process::kill($serverPid, SIGTERM);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
|
|
|
|
|
if (empty($output)) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
return explode('/', $output[0])[0];
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('done')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
function done()
|
|
|
|
|
|
{
|
|
|
|
|
|
set_env('state', 'exit');
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('set_env')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $key
|
|
|
|
|
|
* @param $value
|
|
|
|
|
|
*/
|
|
|
|
|
|
function set_env($key, $value)
|
|
|
|
|
|
{
|
|
|
|
|
|
putenv(sprintf('%s=%s', $key, $value));
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('enable_file_modification_listening')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
function enable_file_modification_listening(): void
|
|
|
|
|
|
{
|
|
|
|
|
|
putenv('enable_file_modification_listening=on');
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('is_enable_file_modification_listening')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @return bool
|
|
|
|
|
|
*/
|
|
|
|
|
|
#[Pure] function is_enable_file_modification_listening(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return env('enable_file_modification_listening', 'off') == 'on';
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('disable_file_modification_listening')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
function disable_file_modification_listening()
|
|
|
|
|
|
{
|
|
|
|
|
|
putenv('enable_file_modification_listening=off');
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('now')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
function now(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return date('Y-m-d H:i:s') . '.' . str_replace(time() . '.', '', (string)microtime(TRUE));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-16 01:54:43 +08:00
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
//if (!function_exists('scan_directory')) {
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
// /**
|
|
|
|
|
|
// * @param $dir
|
|
|
|
|
|
// * @param $namespace
|
|
|
|
|
|
// * @param array $exclude
|
|
|
|
|
|
// * @throws ReflectionException
|
|
|
|
|
|
// * @throws Exception
|
|
|
|
|
|
// */
|
|
|
|
|
|
// function scan_directory($dir, $namespace, array $exclude = []): void
|
|
|
|
|
|
// {
|
|
|
|
|
|
// $annotation = Kiri::getDi()->get(Annotation::class);
|
|
|
|
|
|
// $annotation->read($dir, $namespace, $exclude);
|
|
|
|
|
|
//
|
|
|
|
|
|
// injectRuntime($dir, $exclude);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
//}
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
//if (!function_exists('injectRuntime')) {
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
// /**
|
|
|
|
|
|
// * @param string $path
|
|
|
|
|
|
// * @param array $exclude
|
|
|
|
|
|
// * @throws ReflectionException
|
|
|
|
|
|
// * @throws Exception
|
|
|
|
|
|
// */
|
|
|
|
|
|
// function injectRuntime(string $path, array $exclude = []): void
|
|
|
|
|
|
// {
|
|
|
|
|
|
// $fileLists = Kiri::getAnnotation()->runtime($path, $exclude);
|
|
|
|
|
|
//
|
|
|
|
|
|
// $router = [];
|
|
|
|
|
|
// foreach ($fileLists as $class) {
|
|
|
|
|
|
// $target = TargetManager::get($class);
|
|
|
|
|
|
// $targetAttributes = $target->getAttributes();
|
|
|
|
|
|
// foreach ($targetAttributes as $value) {
|
|
|
|
|
|
// $value = $value->newInstance();
|
|
|
|
|
|
// if (!method_exists($value, 'execute')) {
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// $value->execute($class);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
//
|
|
|
|
|
|
// $methods = $target->getMethodsAttribute();
|
|
|
|
|
|
// foreach ($methods as $method => $attribute) {
|
|
|
|
|
|
// /** @var ReflectionAttribute $item */
|
|
|
|
|
|
// foreach ($attribute as $item) {
|
|
|
|
|
|
// if (!class_exists($item->getName())) {
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// $item = $item->newInstance();
|
|
|
|
|
|
// if ($item instanceof Route) {
|
|
|
|
|
|
// $router[] = [$item, $class, $method];
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// if (!method_exists($item, 'execute')) {
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// $item->execute($class, $method);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if (!empty($router)) {
|
|
|
|
|
|
// foreach ($router as $class) {
|
|
|
|
|
|
// [$item, $class, $method] = $class;
|
|
|
|
|
|
// if (!method_exists($item, 'execute')) {
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// $item->execute($class, $method);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
//}
|
2022-01-09 03:50:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('directory')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $name
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
#[Pure] function directory($name): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return realpath(APP_PATH . $name);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('isUrl')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $url
|
|
|
|
|
|
* @param bool $get_info
|
|
|
|
|
|
* @return false|array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function isUrl($url, bool $get_info = TRUE): bool|array
|
|
|
|
|
|
{
|
|
|
|
|
|
if (str_starts_with($url, '/')) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
|
|
|
|
|
$queryMatch = '/((http[s]?):\/\/)?(([\w\-\_]+\.)+\w+(:\d+)?)(\/.*)?/';
|
|
|
|
|
|
if (!preg_match($queryMatch, $url, $outPut)) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
[$scheme, $host, $port, $user, $pass, $query, $path, $fragment] = parse_url($url);
|
|
|
|
|
|
if ($scheme == 'https' && empty($port)) {
|
|
|
|
|
|
$port = 443;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
if (!empty($query)) $path .= '?' . $query;
|
|
|
|
|
|
if (!empty($fragment)) $path .= '#' . $fragment;
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
return [$scheme == 'https', $host, $port, $path];
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('split_request_uri')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $url
|
|
|
|
|
|
* @return false|array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function split_request_uri($url): bool|array
|
|
|
|
|
|
{
|
|
|
|
|
|
if (($parse = isUrl($url, NULL)) === FALSE) {
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
[$isHttps, $domain, $port, $path] = $parse;
|
|
|
|
|
|
$uri = $isHttps ? 'https://' . $domain : 'http://' . $domain;
|
|
|
|
|
|
if (!empty($port)) {
|
|
|
|
|
|
$uri .= ':' . $port;
|
|
|
|
|
|
}
|
|
|
|
|
|
return [$uri, $path];
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('hadDomain')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $url
|
|
|
|
|
|
* @return false|array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function hadDomain($url): bool|array
|
|
|
|
|
|
{
|
|
|
|
|
|
$param = split_request_uri($url);
|
|
|
|
|
|
return !is_array($param) ? FALSE : $param[0];
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('isDomain')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $url
|
|
|
|
|
|
* @return false|array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function isDomain($url): array|bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return !isIp($url);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (!function_exists('isIp')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +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);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('loadByDir')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +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;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$replace = ltrim(str_replace(__DIR__, '', $value), '/');
|
|
|
|
|
|
$replace = str_replace('.php', '', $replace);
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$first = explode(DIRECTORY_SEPARATOR, $replace);
|
|
|
|
|
|
array_shift($first);
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
Kiri::setAutoload($namespace . '\\' . implode('\\', $first), $value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('redis')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
2022-02-24 15:24:29 +08:00
|
|
|
|
* @return \Kiri\Redis\Redis|Redis
|
2022-01-09 03:50:38 +08:00
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-02-24 15:24:29 +08:00
|
|
|
|
function redis(): \Kiri\Redis\Redis|Redis
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2022-02-24 15:24:29 +08:00
|
|
|
|
return Kiri::getDi()->get(\Kiri\Redis\Redis::class);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('fire')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param object $event
|
|
|
|
|
|
*/
|
|
|
|
|
|
function fire(object $event)
|
|
|
|
|
|
{
|
|
|
|
|
|
di(EventDispatch::class)->dispatch($event);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
if (!function_exists('instance_load')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
2022-06-22 16:29:42 +08:00
|
|
|
|
* @return void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
*/
|
2022-06-22 16:29:42 +08:00
|
|
|
|
function instance_load(): void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
$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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('exif_imagetype')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $name
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
function exif_imagetype($name): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return get_file_extension($name);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('logger')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-02-23 16:32:08 +08:00
|
|
|
|
function logger(): StdoutLoggerInterface
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2022-02-23 16:32:08 +08:00
|
|
|
|
return Kiri::getDi()->get(StdoutLoggerInterface::class);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('trim_blank')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 空白字符替换
|
|
|
|
|
|
* @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')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
function get_file_extension($filename): bool|int|string
|
|
|
|
|
|
{
|
|
|
|
|
|
$mime_types = [
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'txt' => 'text/plain',
|
|
|
|
|
|
'htm' => 'text/html',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'html' => 'text/html',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'php' => 'text/html',
|
|
|
|
|
|
'css' => 'text/css',
|
|
|
|
|
|
'js' => 'application/javascript',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'json' => 'application/json',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'xml' => 'application/xml',
|
|
|
|
|
|
'swf' => 'application/x-shockwave-flash',
|
|
|
|
|
|
'flv' => 'video/x-flv',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// images
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'png' => 'image/png',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'jpeg' => 'image/jpeg',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'gif' => 'image/gif',
|
|
|
|
|
|
'bmp' => 'image/bmp',
|
|
|
|
|
|
'ico' => 'image/vnd.microsoft.icon',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'tiff' => 'image/tiff',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'svg' => 'image/svg+xml',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// archives
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'zip' => 'application/zip',
|
|
|
|
|
|
'rar' => 'application/x-rar-compressed',
|
|
|
|
|
|
'exe' => 'application/x-msdownload',
|
|
|
|
|
|
'msi' => 'application/x-msdownload',
|
|
|
|
|
|
'cab' => 'application/vnd.ms-cab-compressed',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// audio/video
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'mp3' => 'audio/mpeg',
|
|
|
|
|
|
'qt' => 'video/quicktime',
|
|
|
|
|
|
'mov' => 'video/quicktime',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// adobe
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'pdf' => 'application/pdf',
|
|
|
|
|
|
'psd' => 'image/vnd.adobe.photoshop',
|
|
|
|
|
|
'ai' => 'application/postscript',
|
|
|
|
|
|
'eps' => 'application/postscript',
|
|
|
|
|
|
'ps' => 'application/postscript',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// ms office
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'doc' => 'application/msword',
|
|
|
|
|
|
'rtf' => 'application/rtf',
|
|
|
|
|
|
'xls' => 'application/vnd.ms-excel',
|
|
|
|
|
|
'ppt' => 'application/vnd.ms-powerpoint',
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
// open office
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'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',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'sv4cpio' => 'application/x-sv4cpio',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'sv4crc' => 'application/x-sv4crc',
|
|
|
|
|
|
't' => 'application/x-troff',
|
|
|
|
|
|
'tar' => 'application/x-tar',
|
|
|
|
|
|
'tcl' => 'application/x-tcl',
|
|
|
|
|
|
'tex' => 'application/x-tex',
|
|
|
|
|
|
'texi' => 'application/x-texinfo',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
'texinfo' => 'application/x-texinfo',
|
2023-04-15 23:32:00 +08:00
|
|
|
|
'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',
|
2022-01-09 03:50:38 +08:00
|
|
|
|
];
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$explode = explode('.', $filename);
|
|
|
|
|
|
$ext = strtolower(array_pop($explode));
|
|
|
|
|
|
if (array_key_exists($ext, $mime_types)) {
|
|
|
|
|
|
return $ext;
|
|
|
|
|
|
} else if (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('storage')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param string|null $fileName
|
|
|
|
|
|
* @param string|null $path
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function storage(?string $fileName = '', ?string $path = ''): string
|
|
|
|
|
|
{
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$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')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('name')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param int $pid
|
|
|
|
|
|
* @param string|null $prefix
|
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function name(int $pid, string $prefix = NULL)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Kiri::getPlatform()->isMac()) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$name = Config::get('id', 'system') . '[' . $pid . ']';
|
|
|
|
|
|
if (!empty($prefix)) {
|
|
|
|
|
|
$name .= '.' . $prefix;
|
|
|
|
|
|
}
|
|
|
|
|
|
swoole_set_process_name($name);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('zero_full')) {
|
|
|
|
|
|
function zero_full(int $data = 1, int $length = 10): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return sprintf('%0' . $length . 'd', $data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('env')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +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;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('config')) {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param $key
|
|
|
|
|
|
* @param null $default
|
|
|
|
|
|
* @return array|string|null
|
|
|
|
|
|
*/
|
|
|
|
|
|
#[Pure] function config($key, $default = NULL): null|array|string
|
|
|
|
|
|
{
|
|
|
|
|
|
return Config::get($key, $default);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('di')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
2023-04-16 13:26:19 +08:00
|
|
|
|
* @param string|object $className
|
2022-01-09 03:50:38 +08:00
|
|
|
|
* @return mixed
|
2023-04-15 23:32:00 +08:00
|
|
|
|
* @throws ReflectionException
|
2022-01-09 03:50:38 +08:00
|
|
|
|
*/
|
2023-04-16 13:26:19 +08:00
|
|
|
|
function di(string|object $className): mixed
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2023-04-16 13:26:19 +08:00
|
|
|
|
if (is_object($className)) {
|
|
|
|
|
|
return $className;
|
|
|
|
|
|
}
|
2022-01-09 03:50:38 +08:00
|
|
|
|
return Kiri::getDi()->get($className);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('interval')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param callable $callback
|
|
|
|
|
|
* @param int $interval
|
|
|
|
|
|
* @param bool $is
|
|
|
|
|
|
*/
|
2023-04-15 23:32:00 +08:00
|
|
|
|
function interval(callable $callback, int $interval = 1000, bool $is = FALSE): void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
usleep($interval * 1000);
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
$callback();
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
interval($callback, $interval, $is);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('sweep')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @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) {
|
2022-02-18 17:16:46 +08:00
|
|
|
|
$array = array_merge(require "$config", $array);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
return $array;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('swoole_serialize')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $data
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
function swoole_serialize($data): string
|
|
|
|
|
|
{
|
|
|
|
|
|
// if (class_exists('swoole_serialize')) {
|
|
|
|
|
|
// return \swoole_serialize::pack($data);
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
return serialize($data);
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('swoole_unserialize')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('merge')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $param
|
|
|
|
|
|
* @param $param1
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function merge($param, $param1): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return ArrayAccess::merge($param, $param1);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('router')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @return Router
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function router(): Router
|
|
|
|
|
|
{
|
|
|
|
|
|
return Kiri::getDi()->get(Router::class);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('jTraceEx')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $e
|
|
|
|
|
|
* @param null $seen
|
|
|
|
|
|
* @param bool $toHtml
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
function jTraceEx($e, $seen = NULL, bool $toHtml = FALSE): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$starter = $seen ? 'Caused by: ' : '';
|
|
|
|
|
|
$result = [];
|
|
|
|
|
|
if (!$seen) $seen = [];
|
|
|
|
|
|
$trace = $e->getTrace();
|
|
|
|
|
|
$prev = $e->getPrevious();
|
|
|
|
|
|
$result[] = sprintf('%s%s: %s', $starter, $e::class, $e->getMessage());
|
|
|
|
|
|
$file = $e->getFile();
|
|
|
|
|
|
$line = $e->getLine();
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
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);
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +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;
|
|
|
|
|
|
}
|
|
|
|
|
|
$result = join($toHtml ? "<br>" : "\n", $result);
|
|
|
|
|
|
if ($prev) {
|
|
|
|
|
|
$result .= ($toHtml ? "<br>" : "\n") . jTraceEx($prev, $seen, $toHtml);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('swoole_substr_json_decode')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $packet
|
|
|
|
|
|
* @param int $length
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
function swoole_substr_json_decode($packet, int $length = 0): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return json_decode($packet, TRUE);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('swoole_substr_unserialize')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param $packet
|
|
|
|
|
|
* @param int $length
|
|
|
|
|
|
* @return mixed
|
|
|
|
|
|
*/
|
|
|
|
|
|
function swoole_substr_unserialize($packet, int $length = 0): mixed
|
|
|
|
|
|
{
|
|
|
|
|
|
return unserialize($packet);
|
|
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('debug')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param mixed $message
|
|
|
|
|
|
* @param string $method
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-06-22 16:29:42 +08:00
|
|
|
|
function debug(mixed $message, string $method = 'app'): void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2022-06-22 16:29:42 +08:00
|
|
|
|
Kiri::getLogger()->debug($method, [$message]);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('info')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param mixed $message
|
|
|
|
|
|
* @param string $method
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
2022-06-22 16:29:42 +08:00
|
|
|
|
function info(mixed $message, string $method = 'app'): void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2022-06-22 16:29:42 +08:00
|
|
|
|
Kiri::getLogger()->info($method, [$message]);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('error')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param mixed $message
|
2023-04-15 23:32:00 +08:00
|
|
|
|
* @param array $method
|
|
|
|
|
|
* @return void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
*/
|
2023-04-15 23:32:00 +08:00
|
|
|
|
function error(mixed $message, array $method = []): void
|
2022-01-09 03:50:38 +08:00
|
|
|
|
{
|
2023-04-15 23:32:00 +08:00
|
|
|
|
Kiri::getLogger()->error($message, $method);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!function_exists('success')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-09 03:50:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param mixed $message
|
|
|
|
|
|
* @param string $method
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
function success(mixed $message, string $method = 'app')
|
|
|
|
|
|
{
|
2022-06-22 16:29:42 +08:00
|
|
|
|
Kiri::getLogger()->critical($method, [$message]);
|
2022-01-09 03:50:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-11 16:16:05 +08:00
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
if (!function_exists('throwable')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-01-11 16:16:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param Throwable|Error $throwable
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
2022-06-22 16:29:42 +08:00
|
|
|
|
function throwable(\Throwable|\Error $throwable): string
|
2022-01-11 16:16:05 +08:00
|
|
|
|
{
|
2022-09-25 17:16:40 +08:00
|
|
|
|
$message = $throwable->getMessage() . PHP_EOL . $throwable->getFile() . " at line " . $throwable->getLine() . PHP_EOL;
|
2022-01-11 16:16:05 +08:00
|
|
|
|
foreach ($throwable->getTrace() as $value) {
|
|
|
|
|
|
if (!isset($value['file'])) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2022-09-25 17:15:23 +08:00
|
|
|
|
$message .= $value['file'] . " -> " . (isset($value['class']) ? $value['class'] . '::' : '') . ($value['function'] ?? 'Closure') . "(" . $value['line'] . ")" . PHP_EOL;
|
2022-01-11 16:16:05 +08:00
|
|
|
|
}
|
2022-06-22 16:29:42 +08:00
|
|
|
|
return $message;
|
2022-01-11 16:16:05 +08:00
|
|
|
|
}
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-01-11 16:16:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
|
|
|
|
|
|
if (!function_exists('map')) {
|
2023-04-15 23:32:00 +08:00
|
|
|
|
|
2022-06-22 16:29:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @param array $map
|
|
|
|
|
|
* @param Closure $closure
|
|
|
|
|
|
* @return void
|
|
|
|
|
|
*/
|
|
|
|
|
|
function map(array $map, Closure $closure): void
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach ($map as $key => $value) {
|
|
|
|
|
|
$closure($key, $value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|