Files
kiri-core/System/Snowflake.php
T

385 lines
7.4 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake;
use Exception;
2020-09-08 15:30:21 +08:00
use HttpServer\IInterface\Task;
2020-12-15 14:04:02 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 01:27:08 +08:00
use ReflectionException;
2020-09-04 10:44:25 +08:00
use Snowflake\Abstracts\Config;
2020-09-14 02:54:30 +08:00
use Snowflake\Core\JSON;
2020-08-31 01:27:08 +08:00
use Snowflake\Di\Container;
2020-09-14 10:31:10 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 01:27:08 +08:00
use Snowflake\Exception\NotFindClassException;
2020-09-10 19:26:42 +08:00
use Snowflake\Process\Process;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
2020-09-14 02:45:00 +08:00
use Swoole\WebSocket\Server;
2020-08-31 01:27:08 +08:00
2020-09-25 15:37:33 +08:00
defined('DB_ERROR_BUSY') or define('DB_ERROR', 'The database is busy. Please try again later.');
defined('SELECT_IS_NULL') or define('SELECT_IS_NULL', 'Query data does not exist, please check the relevant conditions.');
defined('PARAMS_IS_NULL') or define('PARAMS_IS_NULL', 'Required items cannot be empty, please add.');
2020-12-15 14:04:02 +08:00
defined('CONTROLLER_PATH') or define('CONTROLLER_PATH', APP_PATH . 'app/Http/Controllers/');
defined('SOCKET_PATH') or define('SOCKET_PATH', APP_PATH . 'app/Websocket/');
2020-09-25 15:37:33 +08:00
2020-08-31 01:27:08 +08:00
class Snowflake
{
/** @var Container */
2020-10-29 18:17:25 +08:00
public static Container $container;
2020-08-31 01:27:08 +08:00
/** @var Application */
2020-10-29 18:17:25 +08:00
private static Application $service;
2020-08-31 01:27:08 +08:00
/**
* @param $service
*
* 初始化服务
*/
public static function init($service)
{
static::$service = $service;
}
/**
* @return mixed
*/
2020-12-15 14:04:02 +08:00
public static function app(): Application
2020-08-31 01:27:08 +08:00
{
return static::$service;
}
/**
* @param $name
2020-12-15 14:04:02 +08:00
* @return bool
2020-08-31 01:27:08 +08:00
*/
2020-12-15 14:04:02 +08:00
public static function has($name): bool
2020-08-31 01:27:08 +08:00
{
return static::$service->has($name);
}
/**
* @param $className
* @param $id
*/
public static function setAlias($className, $id)
{
2020-12-15 14:04:02 +08:00
static::$service->setAlias($className, $id);
2020-08-31 01:27:08 +08:00
}
/**
* @param $className
* @param array $construct
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function createObject($className, $construct = []): mixed
2020-08-31 01:27:08 +08:00
{
if (is_string($className)) {
return static::$container->get($className, $construct);
} else if (is_array($className)) {
if (!isset($className['class']) || empty($className['class'])) {
throw new Exception('Object configuration must be an array containing a "class" element.');
}
$class = $className['class'];
unset($className['class']);
return static::$container->get($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 string
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function getStoragePath(): string
2020-08-31 01:27:08 +08:00
{
2020-09-04 10:44:25 +08:00
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
$path = Config::get('storage', false, $default);
2020-08-31 01:27:08 +08:00
if (!is_dir($path)) {
2020-08-31 01:54:20 +08:00
mkdir($path);
2020-08-31 01:27:08 +08:00
}
return $path;
}
/**
* @return bool
*/
2020-12-15 14:04:02 +08:00
public static function inCoroutine(): bool
2020-08-31 01:27:08 +08:00
{
return Coroutine::getCid() > 0;
}
2020-08-31 21:34:04 +08:00
/**
* @return Container
*/
2020-12-15 14:04:02 +08:00
public static function getDi(): Container
2020-08-31 21:34:04 +08:00
{
return static::$container;
}
2020-08-31 01:27:08 +08:00
/**
* @param $workerId
* @return false|int|mixed
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function setProcessId($workerId): mixed
2020-08-31 01:27:08 +08:00
{
return self::writeFile(storage('socket.sock'), $workerId);
}
2020-09-07 16:08:17 +08:00
/**
* @param $workerId
* @return false|int|mixed
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function setWorkerId($workerId): mixed
2020-09-07 16:08:17 +08:00
{
2020-09-11 18:20:08 +08:00
if (empty($workerId)) {
return $workerId;
}
2020-09-07 16:08:17 +08:00
return self::writeFile(storage($workerId . '.sock', 'worker'), $workerId);
}
2020-09-07 16:28:31 +08:00
/**
* @throws Exception
*/
public static function clearWorkerId()
{
$dir = storage(null, 'worker');
foreach (glob($dir . '/*') as $file) {
@unlink($file);
}
}
2020-08-31 01:27:08 +08:00
/**
* @param $fileName
* @param $content
2020-09-03 18:37:13 +08:00
* @param null $is_append
2020-12-15 14:04:02 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
*/
2020-12-15 14:04:02 +08:00
public static function writeFile($fileName, $content, $is_append = null): mixed
2020-08-31 01:27:08 +08:00
{
2020-10-30 01:14:05 +08:00
$params = [$fileName, (string)$content];
2020-09-03 18:37:13 +08:00
if ($is_append !== null) {
$params[] = $is_append;
2020-08-31 01:54:20 +08:00
}
2020-09-03 18:37:13 +08:00
return !self::inCoroutine() ? file_put_contents(...$params) : Coroutine::writeFile(...$params);
2020-08-31 01:27:08 +08:00
}
/**
* @param $object
* @param $config
* @return mixed
*/
2020-12-15 14:04:02 +08:00
public static function configure($object, $config): mixed
2020-08-31 01:27:08 +08:00
{
foreach ($config as $key => $value) {
2020-09-02 15:38:04 +08:00
if (!property_exists($object, $key)) {
continue;
}
2020-08-31 01:27:08 +08:00
$object->$key = $value;
}
return $object;
}
2020-09-14 02:54:30 +08:00
/**
2020-09-27 15:54:15 +08:00
* @param $workerId
2020-09-14 02:54:30 +08:00
* @throws Exception
*/
2020-09-27 15:54:15 +08:00
public static function clearProcessId($workerId)
2020-08-31 01:27:08 +08:00
{
2020-09-27 15:54:15 +08:00
@unlink(storage($workerId . '.sock', 'worker'));
2020-09-08 15:30:21 +08:00
}
2020-09-14 02:45:00 +08:00
/**
2020-09-14 02:49:07 +08:00
* @return Server|null
2020-09-14 02:45:00 +08:00
* @throws
*/
2020-12-15 14:04:02 +08:00
#[Pure] public static function getWebSocket(): ?Server
2020-09-14 02:45:00 +08:00
{
2020-09-14 02:54:30 +08:00
$server = static::app()->server->getServer();
2020-09-14 02:49:07 +08:00
if (!($server instanceof Server)) {
return null;
}
return $server;
2020-09-14 02:45:00 +08:00
}
2020-10-14 11:21:56 +08:00
/**
* @return false|string
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function getMasterPid(): bool|string
2020-10-14 11:21:56 +08:00
{
2020-11-11 10:59:12 +08:00
$default = APP_PATH . 'storage/server.pid';
2020-11-11 15:02:23 +08:00
$server = Config::get('settings.pid_file', false, $default);
2020-11-11 10:59:12 +08:00
return file_get_contents($server);
2020-10-14 11:21:56 +08:00
}
2020-09-14 02:54:30 +08:00
/**
* @param int $fd
* @param $data
2020-12-15 14:04:02 +08:00
* @return mixed
2020-09-14 02:54:30 +08:00
* @throws Exception
*/
2020-12-15 14:04:02 +08:00
public static function push(int $fd, $data): mixed
2020-09-14 02:54:30 +08:00
{
$server = static::getWebSocket();
if (empty($server)) {
return false;
}
if (!is_string($data)) {
$data = JSON::encode($data);
}
return $server->push($fd, $data);
}
2020-09-08 15:30:21 +08:00
/**
* @param string $class
* @param array $params
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function async(string $class, array $params = [])
{
$server = static::app()->server->getServer();
2020-09-08 15:40:31 +08:00
if (!isset($server->setting['task_worker_num']) || !class_exists($class)) {
2020-09-08 15:30:21 +08:00
return;
}
2020-11-27 14:35:15 +08:00
2020-11-27 14:38:17 +08:00
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
2020-09-08 15:30:21 +08:00
/** @var Task $class */
$class = static::createObject($class);
$class->setParams($params);
2020-08-31 01:27:08 +08:00
2020-09-08 15:30:21 +08:00
$server->task(serialize($class), $randWorkerId);
2020-08-31 01:27:08 +08:00
}
2020-09-10 19:26:42 +08:00
/**
* @param $process
*/
2020-12-15 14:04:02 +08:00
public static function shutdown($process): void
2020-09-10 19:26:42 +08:00
{
static::app()->server->getServer()->shutdown();
if ($process instanceof Process) {
$process->exit(0);
}
}
/**
* @param $tmp
* @return string
*/
2020-12-15 14:04:02 +08:00
public static function rename($tmp): string
2020-08-31 12:38:32 +08:00
{
$hash = md5_file($tmp['tmp_name']);
$later = '.' . exif_imagetype($tmp['tmp_name']);
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
return strtoupper($tmp) . $later;
}
2020-08-31 01:27:08 +08:00
/**
* @return bool
*/
public static function isMac()
{
$output = strtolower(PHP_OS | PHP_OS_FAMILY);
2020-12-15 14:04:02 +08:00
if (str_contains('mac', $output)) {
2020-08-31 01:27:08 +08:00
return true;
2020-12-15 14:04:02 +08:00
} else if (str_contains('darwin', $output)) {
2020-08-31 01:27:08 +08:00
return true;
} else {
return false;
}
}
/**
* @return bool
*/
2020-12-15 14:04:02 +08:00
public static function isLinux(): bool
2020-08-31 01:27:08 +08:00
{
if (!static::isMac()) {
return true;
} else {
return false;
}
}
2020-09-14 10:31:10 +08:00
/**
* @return mixed
2020-10-14 11:21:56 +08:00
* @throws Exception
2020-09-14 10:31:10 +08:00
*/
2020-12-15 14:04:02 +08:00
public static function reload(): mixed
2020-08-31 01:27:08 +08:00
{
2020-10-30 01:23:33 +08:00
return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1);
2020-08-31 01:27:08 +08:00
}
2020-09-15 19:41:42 +08:00
2020-10-30 01:23:33 +08:00
private static array $_autoload = [];
2020-09-16 11:03:35 +08:00
/**
* @param $class
* @param $file
*/
public static function setAutoload($class, $file)
{
if (isset(static::$_autoload[$class])) {
return;
}
static::$_autoload[$class] = $file;
include_once "$file";
}
/**
* @param $className
*/
public static function autoload($className)
{
if (!isset(static::$_autoload[$className])) {
return;
}
$file = static::$_autoload[$className];
2020-09-16 11:11:24 +08:00
require_once "$file";
2020-09-16 11:03:35 +08:00
}
2020-09-15 20:11:41 +08:00
}
2020-09-16 11:03:35 +08:00
2020-09-16 11:12:41 +08:00
//spl_autoload_register([Snowflake::class, 'autoload'], true, true);
2020-08-31 01:27:08 +08:00
Snowflake::$container = new Container();