Files
kiri-core/System/Snowflake.php
T

604 lines
11 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;
2021-03-29 17:29:39 +08:00
use Annotation\Annotation;
2021-04-13 19:19:35 +08:00
use Database\ActiveRecord;
use Database\Collection;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-08 15:30:21 +08:00
use HttpServer\IInterface\Task;
2021-03-04 14:40:33 +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-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2020-08-31 01:27:08 +08:00
use Snowflake\Di\Container;
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
2021-04-06 18:21:49 +08:00
defined('DB_ERROR_BUSY') or define('DB_ERROR_BUSY', 'The database is busy. Please try again later.');
2020-09-25 15:37:33 +08:00
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/');
2021-04-11 19:15:24 +08:00
defined('KAFKA_PATH') or define('KAFKA_PATH', APP_PATH . 'app/Kafka/');
2021-03-23 16:14:05 +08:00
defined('RPC_SERVICE_PATH') or define('RPC_SERVICE_PATH', APP_PATH . 'app/Http/Rpc/');
defined('RPC_CLIENT_PATH') or define('RPC_CLIENT_PATH', APP_PATH . 'app/Client/Rpc/');
2021-01-19 17:59:31 +08:00
defined('MODEL_PATH') or define('MODEL_PATH', APP_PATH . 'app/Models/');
2020-12-15 14:04:02 +08:00
defined('SOCKET_PATH') or define('SOCKET_PATH', APP_PATH . 'app/Websocket/');
2020-09-25 15:37:33 +08:00
2020-12-17 14:09:14 +08:00
/**
* Class Snowflake
* @package Snowflake
*/
2020-08-31 01:27:08 +08:00
class Snowflake
{
2021-03-29 17:29:39 +08:00
/** @var Container */
public static Container $container;
/** @var ?Application */
private static ?Application $service = null;
/**
* @param $service
*
* 初始化服务
*/
public static function init($service)
{
static::$service = $service;
}
2021-03-31 00:58:18 +08:00
/**
* @param $alias
* @param array $array
* @return mixed
2021-03-31 18:37:53 +08:00
* @throws Exception
2021-03-31 00:58:18 +08:00
*/
public static function set($alias, $array = []): mixed
{
return static::app()->set($alias, $array);
}
2021-03-29 17:29:39 +08:00
/**
* @return Application|null
*/
public static function app(): ?Application
{
return static::$service;
}
/**
* @param $name
* @return bool
*/
public static function has($name): bool
{
return static::$service->has($name);
}
/**
* @param $className
* @param $id
*/
public static function setAlias($className, $id)
{
static::$service->setAlias($className, $id);
}
/**
* @param $port
* @return bool
* @throws Exception
*/
public static function port_already($port): bool
{
if (empty($port)) {
return false;
}
if (Snowflake::getPlatform()->isLinux()) {
exec('netstat -tunlp | grep ' . $port, $output);
} else {
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
}
return !empty($output);
}
/**
* @return Annotation
* @throws Exception
*/
public static function getAnnotation(): Annotation
{
2021-04-02 18:40:50 +08:00
return static::app()->getAnnotation();
2021-03-29 17:29:39 +08:00
}
/**
* @param $service
* @return string
*/
#[Pure] public static function listen($service): string
{
return sprintf('Check listen %s::%d -> ok', $service['host'], $service['port']);
}
/**
* @param $className
* @param array $construct
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function createObject($className, $construct = []): mixed
{
if (is_object($className)) {
return $className;
}
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
*/
public static function getStoragePath(): string
{
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
2021-04-04 03:01:12 +08:00
$path = Config::get('storage', $default);
2021-03-29 17:29:39 +08:00
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
return $path;
}
/**
* @return bool
*/
public static function inCoroutine(): bool
{
return Coroutine::getCid() > 0;
}
/**
* @return Container
*/
public static function getDi(): Container
{
return static::$container;
}
/**
* @param $workerId
* @return mixed
* @throws Exception
*/
public static function setManagerId($workerId): mixed
{
2021-04-16 11:39:29 +08:00
if (empty($workerId) || static::isDcoker()) {
return $workerId;
}
$tmpFile = storage($workerId . '.sock', 'pid/manager');
return self::writeFile($tmpFile, $workerId);
2021-03-29 17:29:39 +08:00
}
/**
* @param $workerId
* @return mixed
* @throws Exception
*/
public static function setProcessId($workerId): mixed
{
2021-04-16 11:39:29 +08:00
if (empty($workerId) || static::isDcoker()) {
return $workerId;
}
$tmpFile = storage($workerId . '.sock', 'pid/process');
return self::writeFile($tmpFile, $workerId);
2021-03-29 17:29:39 +08:00
}
2021-04-16 11:36:27 +08:00
/**
* @return bool
*/
public static function isDcoker(): bool
{
exec('ls -alh /.dockerenv', $output, $cod);
if ($cod === 0 && !empty($output)) {
return true;
}
return false;
}
2021-03-29 17:29:39 +08:00
/**
* @param $workerId
* @return mixed
* @throws Exception
*/
public static function setWorkerId($workerId): mixed
{
2021-04-16 11:36:27 +08:00
if (empty($workerId) || static::isDcoker()) {
2021-03-29 17:29:39 +08:00
return $workerId;
}
2021-04-16 11:36:27 +08:00
$tmpFile = storage($workerId . '.sock', 'pid/worker');
return self::writeFile($tmpFile, $workerId);
2021-03-29 17:29:39 +08:00
}
/**
* @param $workerId
* @return mixed
* @throws Exception
*/
public static function setTaskId($workerId): mixed
{
2021-04-16 11:39:29 +08:00
if (empty($workerId) || static::isDcoker()) {
2021-03-29 17:29:39 +08:00
return $workerId;
}
2021-04-16 11:39:29 +08:00
$tmpFile = storage($workerId . '.sock', 'pid/task');
return self::writeFile($tmpFile, $workerId);
2021-03-29 17:29:39 +08:00
}
/**
* @param $fileName
* @param $content
* @param null $is_append
* @return mixed
*/
public static function writeFile($fileName, $content, $is_append = null): mixed
{
$params = [$fileName, (string)$content];
if ($is_append !== null) {
$params[] = $is_append;
}
return !self::inCoroutine() ? 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;
}
/**
* @param $workerId
* @param bool $isWorker
* @throws Exception
*/
public static function clearProcessId($workerId, $isWorker = false)
{
clearstatcache();
$directory = $isWorker === true ? 'pid/worker' : 'pid/task';
if (!file_exists($file = storage($workerId, $directory))) {
return;
}
shell_exec('rm -rf ' . $file);
}
/**
* @param string|null $taskPid
* @throws Exception
*/
public static function clearTaskPid(string $taskPid = null)
{
if (empty($taskPid)) {
exec('rm -rf ' . storage(null, 'pid/task'));
} else {
static::clearProcessId($taskPid);
}
}
/**
* @param $taskPid
* @throws Exception
*/
public static function clearWorkerPid($taskPid = null)
{
if (empty($taskPid)) {
exec('rm -rf ' . storage(null, 'pid/worker'));
} else {
static::clearProcessId($taskPid, true);
}
}
/**
* @return Server|null
* @throws
*/
public static function getWebSocket(): ?Server
{
$server = static::app()->getSwoole();
if (!($server instanceof Server)) {
return null;
}
return $server;
}
/**
* @return false|string
* @throws Exception
*/
public static function getMasterPid(): bool|string
{
$pid = Snowflake::app()->getSwoole()->setting['pid_file'];
return file_get_contents($pid);
}
/**
* @param int $fd
* @param $data
* @return mixed
* @throws Exception
*/
public static function push(int $fd, $data): mixed
{
$server = static::getWebSocket();
2021-04-12 15:37:13 +08:00
if (empty($server) || !$server->isEstablished($fd)) {
2021-03-29 17:29:39 +08:00
return false;
}
if (!is_string($data)) {
$data = Json::encode($data);
}
return $server->push($fd, $data);
}
/**
* @return mixed
*/
public static function localhost(): mixed
{
return current(swoole_get_local_ip());
}
/**
* @param string $class
* @param array $params
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function async(string $class, array $params = [])
{
$server = static::app()->getSwoole();
if (!isset($server->setting['task_worker_num']) || !class_exists($class)) {
return;
}
/** @var Task $class */
$class = static::createObject($class);
$class->setParams($params);
$server->task(swoole_serialize($class));
}
/**
* @param $v1
* @param $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 $process
* @throws Exception
*/
public static function shutdown($process): void
{
static::app()->getSwoole()->shutdown();
if ($process instanceof Process) {
$process->exit(0);
}
}
/**
* @param $tmp
* @return string
*/
public static function rename($tmp): string
{
$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;
}
/**
* @return Environmental
2021-04-13 17:45:00 +08:00
* @throws
2021-03-29 17:29:39 +08:00
*/
public static function getPlatform(): Environmental
{
return Snowflake::createObject(Environmental::class);
}
/**
* @return mixed
* @throws Exception
*/
public static function reload(): mixed
{
return Snowflake::app()->getSwoole()->reload();
}
private static array $_autoload = [];
const PROCESS = 'process';
const TASK = 'task';
const WORKER = 'worker';
2021-04-13 19:19:35 +08:00
/**
* @param string $event
* @param null $data
* @return false|string
*/
public static function param(string $event, $data = NULL): bool|string
{
if (is_object($data)) {
if ($data instanceof ActiveRecord || $data instanceof Collection) {
$data = $data->getAttributes();
} else {
$data = get_object_vars($data);
}
}
if (!is_array($data)) $data = ['data' => $data];
return json_encode(array_merge(['callback' => $event], $data));
}
2021-03-29 17:29:39 +08:00
/**
* @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;
}
/**
* @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];
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();