eee
This commit is contained in:
@@ -29,260 +29,261 @@ class Kiri
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Container
|
* @return Container
|
||||||
*/
|
|
||||||
public static function getContainer(): Container
|
|
||||||
{
|
|
||||||
return Container::instance();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Container|null
|
|
||||||
*/
|
|
||||||
public static function getContainerContext(): ?Container
|
|
||||||
{
|
|
||||||
return static::getContainer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $className
|
|
||||||
* @param array $construct
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function createObject($className, array $construct = []): mixed
|
|
||||||
{
|
|
||||||
if (is_string($className) && class_exists($className)) {
|
|
||||||
return static::getContainer()->get($className);
|
|
||||||
} else if (is_array($className) && isset($className['class'])) {
|
|
||||||
$class = $className['class'];
|
|
||||||
unset($className['class']);
|
|
||||||
return static::getContainer()->make($class, $construct, $className);
|
|
||||||
} else if (is_callable($className, TRUE)) {
|
|
||||||
return call_user_func($className, $construct);
|
|
||||||
} else {
|
|
||||||
throw new Exception('Unsupported configuration type: ' . gettype($className));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \Kiri\Pool\Pool
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public static function getPool(): \Kiri\Pool\Pool
|
|
||||||
{
|
|
||||||
return static::getDi()->get(\Kiri\Pool\Pool::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $prefix
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function setProcessName($prefix): void
|
|
||||||
{
|
|
||||||
if (Kiri::getPlatform()->isMac()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$name = '[' . \config('id', 'system-service') . ']';
|
|
||||||
if (!empty($prefix)) {
|
|
||||||
$name .= '.' . $prefix;
|
|
||||||
}
|
|
||||||
swoole_set_process_name($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public static function getStoragePath(): string
|
|
||||||
{
|
|
||||||
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
|
|
||||||
$path = \config('storage', $default);
|
|
||||||
if (!is_dir($path)) {
|
|
||||||
mkdir($path, 0777, true);
|
|
||||||
}
|
|
||||||
return $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Container
|
|
||||||
*/
|
|
||||||
public static function getDi(): Container
|
|
||||||
{
|
|
||||||
return static::getContainer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return LocalService
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public static function service(): LocalService
|
|
||||||
{
|
|
||||||
return static::getContainer()->get(LocalService::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return StdoutLogger
|
|
||||||
* @throws ReflectionException
|
|
||||||
*/
|
|
||||||
public static function getLogger(): StdoutLogger
|
|
||||||
{
|
|
||||||
return static::getContainer()->get(StdoutLogger::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function isDocker(): bool
|
|
||||||
{
|
|
||||||
$output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no');
|
|
||||||
if (trim($output) === 'yes') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $fileName
|
|
||||||
* @param $content
|
|
||||||
* @param null $is_append
|
|
||||||
* @return int|bool
|
|
||||||
*/
|
*/
|
||||||
public static function writeFile($fileName, $content, $is_append = null): int|bool
|
public static function getContainer(): Container
|
||||||
{
|
{
|
||||||
$params = [$fileName, (string)$content];
|
return Container::instance();
|
||||||
if ($is_append !== null) {
|
}
|
||||||
$params[] = $is_append;
|
|
||||||
}
|
|
||||||
return !(Coroutine::getCid() > 0) ? file_put_contents(...$params) : Coroutine::writeFile(...$params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $object
|
* @return Container|null
|
||||||
* @param $config
|
*/
|
||||||
* @return mixed
|
public static function getContainerContext(): ?Container
|
||||||
*/
|
{
|
||||||
public static function configure($object, $config): mixed
|
return static::getContainer();
|
||||||
{
|
}
|
||||||
foreach ($config as $key => $value) {
|
|
||||||
if (!property_exists($object, $key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$object->$key = $value;
|
|
||||||
}
|
|
||||||
return $object;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @param $className
|
||||||
*/
|
* @param array $construct
|
||||||
public static function localhost(): mixed
|
* @return mixed
|
||||||
{
|
* @throws Exception
|
||||||
return current(swoole_get_local_ip());
|
*/
|
||||||
}
|
public static function createObject($className, array $construct = []): mixed
|
||||||
|
{
|
||||||
|
if (is_string($className) && class_exists($className)) {
|
||||||
|
$object = static::getContainer()->get($className);
|
||||||
|
return self::configure($object, $construct);
|
||||||
|
} else if (is_array($className) && isset($className['class'])) {
|
||||||
|
$class = $className['class'];
|
||||||
|
unset($className['class']);
|
||||||
|
return static::getContainer()->make($class, $construct, $className);
|
||||||
|
} else if (is_callable($className, TRUE)) {
|
||||||
|
return call_user_func($className, $construct);
|
||||||
|
} else {
|
||||||
|
throw new Exception('Unsupported configuration type: ' . gettype($className));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $v1
|
* @return \Kiri\Pool\Pool
|
||||||
* @param array $v2
|
* @throws ReflectionException
|
||||||
* @return float
|
*/
|
||||||
*/
|
public static function getPool(): \Kiri\Pool\Pool
|
||||||
#[Pure] public static function distance(array $v1, array $v2): float
|
{
|
||||||
{
|
return static::getDi()->get(\Kiri\Pool\Pool::class);
|
||||||
$maxX = max($v1['x'], $v2['x']);
|
}
|
||||||
$minX = min($v1['x'], $v2['x']);
|
|
||||||
|
|
||||||
$maxZ = max($v1['z'], $v2['z']);
|
|
||||||
$minZ = min($v1['z'], $v2['z']);
|
|
||||||
|
|
||||||
$dx = abs($maxX - $minX);
|
|
||||||
$dy = abs($maxZ - $minZ);
|
|
||||||
|
|
||||||
$sqrt = sqrt($dx * $dx + $dy * $dy);
|
|
||||||
if ($sqrt < 0) {
|
|
||||||
$sqrt = abs($sqrt);
|
|
||||||
}
|
|
||||||
return (float)$sqrt;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $tmp_name
|
* @param $prefix
|
||||||
* @return string
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function rename($tmp_name): string
|
public static function setProcessName($prefix): void
|
||||||
{
|
{
|
||||||
$hash = md5_file($tmp_name);
|
if (Kiri::getPlatform()->isMac()) {
|
||||||
|
return;
|
||||||
$later = '.' . exif_imagetype($tmp_name);
|
}
|
||||||
|
$name = '[' . \config('id', 'system-service') . ']';
|
||||||
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
|
if (!empty($prefix)) {
|
||||||
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
|
$name .= '.' . $prefix;
|
||||||
|
}
|
||||||
return strtoupper($tmp) . $later;
|
swoole_set_process_name($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Environmental
|
* @return string
|
||||||
* @throws
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function getPlatform(): Environmental
|
public static function getStoragePath(): string
|
||||||
{
|
{
|
||||||
return Kiri::createObject(Environmental::class);
|
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
|
||||||
}
|
$path = \config('storage', $default);
|
||||||
|
if (!is_dir($path)) {
|
||||||
|
mkdir($path, 0777, true);
|
||||||
|
}
|
||||||
|
return $path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const PROCESS = 'process';
|
/**
|
||||||
const TASK = 'task';
|
* @return Container
|
||||||
const WORKER = 'worker';
|
*/
|
||||||
|
public static function getDi(): Container
|
||||||
/**
|
{
|
||||||
* @return string|null
|
return static::getContainer();
|
||||||
*/
|
}
|
||||||
#[Pure] public static function getEnvironmental(): ?string
|
|
||||||
{
|
|
||||||
return env('environmental');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return LocalService
|
||||||
*/
|
* @throws ReflectionException
|
||||||
#[Pure] public static function isTask(): bool
|
*/
|
||||||
{
|
public static function service(): LocalService
|
||||||
return static::getEnvironmental() == static::TASK;
|
{
|
||||||
}
|
return static::getContainer()->get(LocalService::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return StdoutLogger
|
||||||
*/
|
* @throws ReflectionException
|
||||||
#[Pure] public static function isWorker(): bool
|
*/
|
||||||
{
|
public static function getLogger(): StdoutLogger
|
||||||
return static::getEnvironmental() == static::WORKER;
|
{
|
||||||
}
|
return static::getContainer()->get(StdoutLogger::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public static function isProcess(): bool
|
public static function isDocker(): bool
|
||||||
{
|
{
|
||||||
return static::getEnvironmental() == static::PROCESS;
|
$output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no');
|
||||||
}
|
if (trim($output) === 'yes') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $fileName
|
||||||
|
* @param $content
|
||||||
|
* @param null $is_append
|
||||||
|
* @return int|bool
|
||||||
|
*/
|
||||||
|
public static function writeFile($fileName, $content, $is_append = null): int|bool
|
||||||
|
{
|
||||||
|
$params = [$fileName, (string)$content];
|
||||||
|
if ($is_append !== null) {
|
||||||
|
$params[] = $is_append;
|
||||||
|
}
|
||||||
|
return !(Coroutine::getCid() > 0) ? file_put_contents(...$params) : Coroutine::writeFile(...$params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $object
|
||||||
|
* @param $config
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function configure($object, $config): mixed
|
||||||
|
{
|
||||||
|
foreach ($config as $key => $value) {
|
||||||
|
if (!property_exists($object, $key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$object->$key = $value;
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function localhost(): mixed
|
||||||
|
{
|
||||||
|
return current(swoole_get_local_ip());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $v1
|
||||||
|
* @param array $v2
|
||||||
|
* @return float
|
||||||
|
*/
|
||||||
|
#[Pure] public static function distance(array $v1, array $v2): float
|
||||||
|
{
|
||||||
|
$maxX = max($v1['x'], $v2['x']);
|
||||||
|
$minX = min($v1['x'], $v2['x']);
|
||||||
|
|
||||||
|
$maxZ = max($v1['z'], $v2['z']);
|
||||||
|
$minZ = min($v1['z'], $v2['z']);
|
||||||
|
|
||||||
|
$dx = abs($maxX - $minX);
|
||||||
|
$dy = abs($maxZ - $minZ);
|
||||||
|
|
||||||
|
$sqrt = sqrt($dx * $dx + $dy * $dy);
|
||||||
|
if ($sqrt < 0) {
|
||||||
|
$sqrt = abs($sqrt);
|
||||||
|
}
|
||||||
|
return (float)$sqrt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $tmp_name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function rename($tmp_name): string
|
||||||
|
{
|
||||||
|
$hash = md5_file($tmp_name);
|
||||||
|
|
||||||
|
$later = '.' . exif_imagetype($tmp_name);
|
||||||
|
|
||||||
|
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
|
||||||
|
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
|
||||||
|
|
||||||
|
return strtoupper($tmp) . $later;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Environmental
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public static function getPlatform(): Environmental
|
||||||
|
{
|
||||||
|
return Kiri::createObject(Environmental::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const PROCESS = 'process';
|
||||||
|
const TASK = 'task';
|
||||||
|
const WORKER = 'worker';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
#[Pure] public static function getEnvironmental(): ?string
|
||||||
|
{
|
||||||
|
return env('environmental');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public static function isTask(): bool
|
||||||
|
{
|
||||||
|
return static::getEnvironmental() == static::TASK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public static function isWorker(): bool
|
||||||
|
{
|
||||||
|
return static::getEnvironmental() == static::WORKER;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public static function isProcess(): bool
|
||||||
|
{
|
||||||
|
return static::getEnvironmental() == static::PROCESS;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user