This commit is contained in:
as2252258@163.com
2021-02-23 02:46:55 +08:00
parent cba743f43e
commit 5b57b8d4b3
2 changed files with 325 additions and 323 deletions
+1 -3
View File
@@ -92,9 +92,7 @@ class Service extends Component
} else { } else {
$this->_definition[$id] = $definition; $this->_definition[$id] = $definition;
} }
$this->_components[$id] = Snowflake::createObject($definition); return $this->get($id);
var_dump($this->_components[$id]);
return $this->_components[$id];
} }
/** /**
+324 -320
View File
@@ -34,388 +34,392 @@ defined('SOCKET_PATH') or define('SOCKET_PATH', APP_PATH . 'app/Websocket/');
class Snowflake class Snowflake
{ {
/** @var Container */ /** @var Container */
public static Container $container; public static Container $container;
/** @var Application */ /** @var Application */
private static Application $service; private static Application $service;
/** /**
* @param $service * @param $service
* *
* 初始化服务 * 初始化服务
*/ */
public static function init($service) public static function init($service)
{ {
static::$service = $service; static::$service = $service;
} }
/** /**
* @return mixed * @return mixed
*/ */
public static function app(): Application public static function app(): Application
{ {
return static::$service; return static::$service;
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
*/ */
public static function has($name): bool public static function has($name): bool
{ {
return static::$service->has($name); return static::$service->has($name);
} }
/** /**
* @param $className * @param $className
* @param $id * @param $id
*/ */
public static function setAlias($className, $id) public static function setAlias($className, $id)
{ {
static::$service->setAlias($className, $id); static::$service->setAlias($className, $id);
} }
/** /**
* @param $className * @param $className
* @param array $construct * @param array $construct
* @return mixed * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function createObject($className, $construct = []): mixed public static function createObject($className, $construct = []): mixed
{ {
if (is_object($className)) { if (is_object($className)) {
return $className; return $className;
} }
if (is_string($className)) { if (is_string($className)) {
return static::$container->get($className, $construct); return static::$container->get($className, $construct);
} else if (is_array($className)) { } else if (is_array($className)) {
if (!isset($className['class']) || empty($className['class'])) { if (!isset($className['class']) || empty($className['class'])) {
throw new Exception('Object configuration must be an array containing a "class" element.'); throw new Exception('Object configuration must be an array containing a "class" element.');
} }
return static::$container->get($className['class'], $construct, $className);
} else if (is_callable($className, TRUE)) {
return call_user_func($className, $construct);
} else {
throw new Exception('Unsupported configuration type: ' . gettype($className));
}
}
/** $class = $className['class'];
* @return string unset($className['class']);
* @throws Exception
*/ return static::$container->get($class, $construct, $className);
public static function getStoragePath(): string } else if (is_callable($className, TRUE)) {
{ return call_user_func($className, $construct);
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR; } else {
$path = Config::get('storage', false, $default); throw new Exception('Unsupported configuration type: ' . gettype($className));
if (!is_dir($path)) { }
mkdir($path); }
}
return $path; /**
} * @return string
* @throws Exception
*/
public static function getStoragePath(): string
{
$default = APP_PATH . 'storage' . DIRECTORY_SEPARATOR;
$path = Config::get('storage', false, $default);
if (!is_dir($path)) {
mkdir($path);
}
return $path;
}
/** /**
* @return bool * @return bool
*/ */
public static function inCoroutine(): bool public static function inCoroutine(): bool
{ {
return Coroutine::getCid() > 0; return Coroutine::getCid() > 0;
} }
/** /**
* @return Container * @return Container
*/ */
public static function getDi(): Container public static function getDi(): Container
{ {
return static::$container; return static::$container;
} }
/** /**
* @param $workerId * @param $workerId
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public static function setProcessId($workerId): mixed public static function setProcessId($workerId): mixed
{ {
return self::writeFile(storage('socket.sock'), $workerId); return self::writeFile(storage('socket.sock'), $workerId);
} }
/** /**
* @param $workerId * @param $workerId
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public static function setWorkerId($workerId): mixed public static function setWorkerId($workerId): mixed
{ {
if (empty($workerId)) { if (empty($workerId)) {
return $workerId; return $workerId;
} }
return self::writeFile(storage($workerId . '.sock', 'worker'), $workerId); return self::writeFile(storage($workerId . '.sock', 'worker'), $workerId);
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public static function clearWorkerId() public static function clearWorkerId()
{ {
$dir = storage(null, 'worker'); $dir = storage(null, 'worker');
foreach (glob($dir . '/*') as $file) { foreach (glob($dir . '/*') as $file) {
@unlink($file); @unlink($file);
} }
} }
/** /**
* @param $fileName * @param $fileName
* @param $content * @param $content
* @param null $is_append * @param null $is_append
* @return mixed * @return mixed
*/ */
public static function writeFile($fileName, $content, $is_append = null): mixed public static function writeFile($fileName, $content, $is_append = null): mixed
{ {
$params = [$fileName, (string)$content]; $params = [$fileName, (string)$content];
if ($is_append !== null) { if ($is_append !== null) {
$params[] = $is_append; $params[] = $is_append;
} }
return !self::inCoroutine() ? file_put_contents(...$params) : Coroutine::writeFile(...$params); return !self::inCoroutine() ? file_put_contents(...$params) : Coroutine::writeFile(...$params);
} }
/** /**
* @param $object * @param $object
* @param $config * @param $config
* @return mixed * @return mixed
*/ */
public static function configure($object, $config): mixed public static function configure($object, $config): mixed
{ {
foreach ($config as $key => $value) { foreach ($config as $key => $value) {
if (!property_exists($object, $key)) { if (!property_exists($object, $key)) {
continue; continue;
} }
$object->$key = $value; $object->$key = $value;
} }
return $object; return $object;
} }
/** /**
* @param $workerId * @param $workerId
* @throws Exception * @throws Exception
*/ */
public static function clearProcessId($workerId) public static function clearProcessId($workerId)
{ {
@unlink(storage($workerId . '.sock', 'worker')); @unlink(storage($workerId . '.sock', 'worker'));
} }
/** /**
* @return Server|null * @return Server|null
* @throws * @throws
*/ */
public static function getWebSocket(): ?Server public static function getWebSocket(): ?Server
{ {
$server = static::app()->getSwoole(); $server = static::app()->getSwoole();
if (!($server instanceof Server)) { if (!($server instanceof Server)) {
return null; return null;
} }
return $server; return $server;
} }
/** /**
* @return false|string * @return false|string
* @throws Exception * @throws Exception
*/ */
public static function getMasterPid(): bool|string public static function getMasterPid(): bool|string
{ {
$default = APP_PATH . 'storage/server.pid'; $default = APP_PATH . 'storage/server.pid';
$server = Config::get('settings.pid_file', false, $default); $server = Config::get('settings.pid_file', false, $default);
return file_get_contents($server); return file_get_contents($server);
} }
/** /**
* @param int $fd * @param int $fd
* @param $data * @param $data
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public static function push(int $fd, $data): mixed public static function push(int $fd, $data): mixed
{ {
$server = static::getWebSocket(); $server = static::getWebSocket();
if (empty($server)) { if (empty($server)) {
return false; return false;
} }
if (!is_string($data)) { if (!is_string($data)) {
$data = Json::encode($data); $data = Json::encode($data);
} }
return $server->push($fd, $data); return $server->push($fd, $data);
} }
/** /**
* @return mixed * @return mixed
*/ */
public static function localhost(): mixed public static function localhost(): mixed
{ {
return Snowflake::app()->getFirstLocal(); return Snowflake::app()->getFirstLocal();
} }
/** /**
* @param string $class * @param string $class
* @param array $params * @param array $params
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public static function async(string $class, array $params = []) public static function async(string $class, array $params = [])
{ {
$server = static::app()->getSwoole(); $server = static::app()->getSwoole();
if (!isset($server->setting['task_worker_num']) || !class_exists($class)) { if (!isset($server->setting['task_worker_num']) || !class_exists($class)) {
return; return;
} }
$randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1); $randWorkerId = random_int(0, $server->setting['task_worker_num'] - 1);
/** @var Task $class */ /** @var Task $class */
$class = static::createObject($class); $class = static::createObject($class);
$class->setParams($params); $class->setParams($params);
$server->task(serialize($class), $randWorkerId); $server->task(serialize($class), $randWorkerId);
} }
/** /**
* @param $v1 * @param $v1
* @param $v2 * @param $v2
* @return float * @return float
*/ */
public static function distance(array $v1, array $v2): float public static function distance(array $v1, array $v2): float
{ {
$maxX = max($v1['x'], $v2['x']); $maxX = max($v1['x'], $v2['x']);
$minX = min($v1['x'], $v2['x']); $minX = min($v1['x'], $v2['x']);
$maxZ = max($v1['z'], $v2['z']); $maxZ = max($v1['z'], $v2['z']);
$minZ = min($v1['z'], $v2['z']); $minZ = min($v1['z'], $v2['z']);
$dx = abs($maxX - $minX); $dx = abs($maxX - $minX);
$dy = abs($maxZ - $minZ); $dy = abs($maxZ - $minZ);
$sqrt = sqrt($dx * $dx + $dy * $dy); $sqrt = sqrt($dx * $dx + $dy * $dy);
if ($sqrt < 0) { if ($sqrt < 0) {
$sqrt = abs($sqrt); $sqrt = abs($sqrt);
} }
return (float)$sqrt; return (float)$sqrt;
} }
/** /**
* @param $process * @param $process
*/ */
public static function shutdown($process): void public static function shutdown($process): void
{ {
static::app()->getSwoole()->shutdown(); static::app()->getSwoole()->shutdown();
if ($process instanceof Process) { if ($process instanceof Process) {
$process->exit(0); $process->exit(0);
} }
} }
/** /**
* @param $tmp * @param $tmp
* @return string * @return string
*/ */
public static function rename($tmp): string public static function rename($tmp): string
{ {
$hash = md5_file($tmp['tmp_name']); $hash = md5_file($tmp['tmp_name']);
$later = '.' . exif_imagetype($tmp['tmp_name']); $later = '.' . exif_imagetype($tmp['tmp_name']);
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/'; $match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash); $tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
return strtoupper($tmp) . $later; return strtoupper($tmp) . $later;
} }
/** /**
* @return bool * @return bool
*/ */
public static function isMac(): bool public static function isMac(): bool
{ {
$output = strtolower(PHP_OS | PHP_OS_FAMILY); $output = strtolower(PHP_OS | PHP_OS_FAMILY);
if (str_contains('mac', $output)) { if (str_contains('mac', $output)) {
return true; return true;
} else if (str_contains('darwin', $output)) { } else if (str_contains('darwin', $output)) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
/** /**
* @return bool * @return bool
*/ */
public static function isLinux(): bool public static function isLinux(): bool
{ {
if (!static::isMac()) { if (!static::isMac()) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public static function reload(): mixed public static function reload(): mixed
{ {
return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1); return Process::kill((int)Snowflake::getMasterPid(), SIGUSR1);
} }
private static array $_autoload = []; private static array $_autoload = [];
/** /**
* @param $class * @param $class
* @param $file * @param $file
*/ */
public static function setAutoload($class, $file) public static function setAutoload($class, $file)
{ {
if (isset(static::$_autoload[$class])) { if (isset(static::$_autoload[$class])) {
return; return;
} }
static::$_autoload[$class] = $file; static::$_autoload[$class] = $file;
include_once "$file"; include_once "$file";
} }
/** /**
* @param $className * @param $className
*/ */
public static function autoload($className) public static function autoload($className)
{ {
if (!isset(static::$_autoload[$className])) { if (!isset(static::$_autoload[$className])) {
return; return;
} }
$file = static::$_autoload[$className]; $file = static::$_autoload[$className];
require_once "$file"; require_once "$file";
} }
} }