Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ebcb7237ef | |||
| 5c1282959c | |||
| 115273e5a4 | |||
| 709f2e273e | |||
| 536e4c9bc5 | |||
| 91be2eba20 | |||
| d7f5f62c1c | |||
| 8b8bfa2a60 | |||
| 1ecf32bfa6 | |||
| 8c0c5b56c8 | |||
| 76351fbe66 | |||
| 8479106b9f | |||
| ca8cc081bc | |||
| a31c24ddf3 |
@@ -34,3 +34,4 @@ runtime/
|
|||||||
oot
|
oot
|
||||||
d
|
d
|
||||||
composer.lock
|
composer.lock
|
||||||
|
.gstack/
|
||||||
|
|||||||
@@ -37,13 +37,6 @@ class Kiri
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Container|null
|
|
||||||
*/
|
|
||||||
public static function getContainerContext(): ?Container
|
|
||||||
{
|
|
||||||
return static::getContainer();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,17 +124,6 @@ class Kiri
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function isDocker(): bool
|
|
||||||
{
|
|
||||||
$output = shell_exec('[ -f /.dockerenv ] && echo yes || echo no');
|
|
||||||
if (trim($output) === 'yes') {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -177,54 +159,6 @@ class Kiri
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+4
-9
@@ -9,7 +9,7 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.4",
|
"php": ">=8.5",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-fileinfo": "*",
|
"ext-fileinfo": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
"ext-openssl": "*",
|
"ext-openssl": "*",
|
||||||
"ext-swoole": "*",
|
"ext-swoole": "*",
|
||||||
"ext-msgpack": "*",
|
"ext-msgpack": "*",
|
||||||
"symfony/console": "^v8.0",
|
"symfony/console": "^v8.1.1",
|
||||||
"psr/log": "1.*",
|
"psr/log": "1.*",
|
||||||
"composer-runtime-api": "^2.0",
|
"composer-runtime-api": "^2.0",
|
||||||
"psr/http-server-middleware": "^1.0.2",
|
"psr/http-server-middleware": "^1.0.2",
|
||||||
@@ -32,17 +32,12 @@
|
|||||||
"nikic/php-parser": "^v5.5.0",
|
"nikic/php-parser": "^v5.5.0",
|
||||||
"ext-inotify": "*",
|
"ext-inotify": "*",
|
||||||
"game-worker/kiri-pool": "^v1.0",
|
"game-worker/kiri-pool": "^v1.0",
|
||||||
"psr/container": "^2.0",
|
"psr/container": "^2.0"
|
||||||
"swiftmailer/swiftmailer": "^v6.3.0"
|
|
||||||
},
|
},
|
||||||
"replace": {
|
"replace": {
|
||||||
"symfony/polyfill-apcu": "*",
|
"symfony/polyfill-apcu": "*",
|
||||||
"symfony/polyfill-php80": "*",
|
|
||||||
"symfony/polyfill-mbstring": "*",
|
"symfony/polyfill-mbstring": "*",
|
||||||
"symfony/polyfill-ctype": "*",
|
"symfony/polyfill-ctype": "*"
|
||||||
"symfony/polyfill-php73": "*",
|
|
||||||
"symfony/polyfill-php72": "*",
|
|
||||||
"symfony/polyfill-php81": "*"
|
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|||||||
+37
-32
@@ -80,9 +80,9 @@ if (!function_exists('application')) {
|
|||||||
|
|
||||||
if (!function_exists('task')) {
|
if (!function_exists('task')) {
|
||||||
/**
|
/**
|
||||||
* @param string $handler
|
* @param string $handler
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
* @param int $dstWorkerId
|
* @param int $dstWorkerId
|
||||||
* @param callable|null $finishFinishCallback
|
* @param callable|null $finishFinishCallback
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -98,9 +98,9 @@ if (!function_exists('task')) {
|
|||||||
if (!function_exists('taskWait')) {
|
if (!function_exists('taskWait')) {
|
||||||
/**
|
/**
|
||||||
* @param string $handler
|
* @param string $handler
|
||||||
* @param mixed $data
|
* @param mixed $data
|
||||||
* @param float $timeout
|
* @param float $timeout
|
||||||
* @param int $dstWorkerId
|
* @param int $dstWorkerId
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -166,8 +166,7 @@ if (!function_exists('isJson')) {
|
|||||||
{
|
{
|
||||||
if (is_null($string))
|
if (is_null($string))
|
||||||
return false;
|
return false;
|
||||||
return (str_starts_with($string, '{') && str_ends_with($string, '}'))
|
return (str_starts_with($string, '{') && str_ends_with($string, '}')) || (str_ends_with($string, '[') && str_starts_with($string, ']'));
|
||||||
|| (str_ends_with($string, '[') && str_starts_with($string, ']'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -176,7 +175,7 @@ if (!function_exists('instance')) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param array $constrict
|
* @param array $constrict
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return null|object
|
* @return null|object
|
||||||
@@ -195,7 +194,7 @@ if (!function_exists('call')) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $handler
|
* @param $handler
|
||||||
* @param mixed ...$params
|
* @param mixed ...$params
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
@@ -219,33 +218,41 @@ if (!function_exists('checkPortIsAlready')) {
|
|||||||
/**
|
/**
|
||||||
* @param $port
|
* @param $port
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
* @throws
|
|
||||||
*/
|
*/
|
||||||
function checkPortIsAlready($port): bool|string
|
function checkPortIsAlready($port): bool|string
|
||||||
{
|
{
|
||||||
|
$port = (int)$port;
|
||||||
|
if ($port <= 0) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!Kiri::getPlatform()->isLinux()) {
|
if (!Kiri::getPlatform()->isLinux()) {
|
||||||
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
exec("lsof -i :" . $port . " | grep -i 'LISTEN' | awk '{print $2}'", $output);
|
||||||
if (empty($output))
|
if (empty($output)) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
}
|
||||||
$output = explode(PHP_EOL, $output[0]);
|
$output = explode(PHP_EOL, $output[0]);
|
||||||
return $output[0];
|
return $output[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
$serverPid = file_get_contents(storage('.swoole.pid'));
|
exec('netstat -lnp 2>/dev/null | grep :' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
||||||
if (!empty($serverPid) && shell_exec('ps -ef | grep ' . $serverPid . ' | grep -v grep')) {
|
if (empty($output)) {
|
||||||
Process::kill($serverPid, 0) && Process::kill($serverPid, SIGTERM);
|
exec('ss -ltnp 2>/dev/null | grep :' . $port . ' | sed -n \"s/.*pid=\\([0-9]*\\).*/\\1/p\"', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
|
||||||
if (empty($output)) {
|
if (empty($output)) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
return explode('/', $output[0])[0];
|
|
||||||
|
$pid = trim((string)$output[0]);
|
||||||
|
if (str_contains($pid, '/')) {
|
||||||
|
$pid = explode('/', $pid)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $pid !== '' ? $pid : FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!function_exists('set_env')) {
|
if (!function_exists('set_env')) {
|
||||||
|
|
||||||
|
|
||||||
@@ -449,9 +456,9 @@ if (!function_exists('trim_blank')) {
|
|||||||
/**
|
/**
|
||||||
* 空白字符替换
|
* 空白字符替换
|
||||||
* @param string $content 内容
|
* @param string $content 内容
|
||||||
* @param int $len 截取长度
|
* @param int $len 截取长度
|
||||||
* @param string $encode 编码
|
* @param string $encode 编码
|
||||||
* @param bool $htmlTags
|
* @param bool $htmlTags
|
||||||
* @return array|string|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = TRUE): array|string|null
|
function trim_blank(string $content, int $len = 0, string $encode = 'utf-8', bool $htmlTags = TRUE): array|string|null
|
||||||
@@ -745,8 +752,8 @@ if (!function_exists('on')) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $callback
|
* @param $callback
|
||||||
* @param int $index
|
* @param int $index
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
@@ -779,7 +786,7 @@ if (!function_exists('off')) {
|
|||||||
if (!function_exists('process_name_set')) {
|
if (!function_exists('process_name_set')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $pid
|
* @param int $pid
|
||||||
* @param string|null $prefix
|
* @param string|null $prefix
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
@@ -816,7 +823,7 @@ if (!function_exists('zero_full')) {
|
|||||||
if (!function_exists('env')) {
|
if (!function_exists('env')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param null $default
|
* @param null $default
|
||||||
* @return array|string|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
@@ -835,7 +842,7 @@ if (!function_exists('env')) {
|
|||||||
if (!function_exists('config')) {
|
if (!function_exists('config')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param null $default
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
@@ -850,7 +857,7 @@ if (!function_exists('config')) {
|
|||||||
if (!function_exists('created')) {
|
if (!function_exists('created')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param array $construct
|
* @param array $construct
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @return null|object
|
* @return null|object
|
||||||
@@ -1029,9 +1036,7 @@ if (!function_exists('json_log')) {
|
|||||||
];
|
];
|
||||||
$param['data'] = $data;
|
$param['data'] = $data;
|
||||||
|
|
||||||
file_put_contents(storage('exception-' . date('Y-m-d') . '.log','exception'),
|
file_put_contents(storage('exception-' . date('Y-m-d') . '.log', 'exception'), json_encode($param, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
|
||||||
json_encode($param, JSON_UNESCAPED_UNICODE),
|
|
||||||
FILE_APPEND);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1039,7 +1044,7 @@ if (!function_exists('json_log')) {
|
|||||||
if (!function_exists('map')) {
|
if (!function_exists('map')) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $map
|
* @param array $map
|
||||||
* @param Closure $closure
|
* @param Closure $closure
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -46,13 +46,21 @@ abstract class BaseApplication extends LocalService
|
|||||||
public function __construct(public EventProvider $provider, public ConfigProvider $config, public ContainerInterface $container)
|
public function __construct(public EventProvider $provider, public ConfigProvider $config, public ContainerInterface $container)
|
||||||
{
|
{
|
||||||
$this->mapping($config);
|
$this->mapping($config);
|
||||||
$this->parseStorage($config);
|
|
||||||
$this->parseEvents($config);
|
|
||||||
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function init(): void
|
||||||
|
{
|
||||||
|
$this->parseStorage($this->config);
|
||||||
|
$this->parseEvents($this->config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ConfigProvider $config
|
* @param ConfigProvider $config
|
||||||
* @return void
|
* @return void
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/3/30 0030
|
|
||||||
* Time: 14:28
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Kiri\Abstracts;
|
namespace Kiri\Abstracts;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
@@ -18,112 +11,55 @@ use Kiri\Events\EventDispatch;
|
|||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
/**
|
class Component
|
||||||
* Class Component
|
|
||||||
* @package Kiri\Base
|
|
||||||
* @property ContainerInterface $container
|
|
||||||
* @property EventDispatch $dispatch
|
|
||||||
* @property EventProvider $provider
|
|
||||||
*/
|
|
||||||
class Component implements Configure
|
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public function init(): void
|
||||||
* BaseAbstract constructor.
|
{
|
||||||
*/
|
}
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#[Pure] public static function className(): string
|
||||||
|
{
|
||||||
|
return static::class;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function init(): void
|
public function getLogger(): StdoutLogger
|
||||||
{
|
{
|
||||||
}
|
return Kiri::getLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDispatch(): EventDispatch
|
||||||
|
{
|
||||||
|
return Kiri::getDi()->get(EventDispatch::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public function getProvider(): EventProvider
|
||||||
* @return string
|
{
|
||||||
*/
|
return Kiri::getDi()->get(EventProvider::class);
|
||||||
#[Pure] public static function className(): string
|
}
|
||||||
{
|
|
||||||
return static::class;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public function getContainer(): ContainerInterface
|
||||||
|
{
|
||||||
|
return Kiri::getDi();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return StdoutLogger
|
* @throws
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function __get(string $name)
|
||||||
public function getLogger(): StdoutLogger
|
{
|
||||||
{
|
$method = 'get' . ucfirst($name);
|
||||||
return Kiri::getLogger();
|
if (method_exists($this, $method)) {
|
||||||
}
|
return $this->{$method}();
|
||||||
|
}
|
||||||
|
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return mixed
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function __get(string $name)
|
|
||||||
{
|
|
||||||
$method = 'get' . ucfirst($name);
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
return $this->{$method}();
|
|
||||||
} else if (method_exists($this, $name)) {
|
|
||||||
return $this->{$name};
|
|
||||||
} else {
|
|
||||||
throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $value
|
|
||||||
* @return void
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function __set(string $name, $value): void
|
|
||||||
{
|
|
||||||
$method = 'set' . ucfirst($name);
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
$this->{$method}($value);
|
|
||||||
} else if (method_exists($this, $name)) {
|
|
||||||
$this->{$name} = $value;
|
|
||||||
} else {
|
|
||||||
throw new Exception('Unable setting property ' . get_called_class() . '::' . $name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return EventDispatch
|
|
||||||
*/
|
|
||||||
public function getDispatch(): EventDispatch
|
|
||||||
{
|
|
||||||
return Kiri::getDi()->get(EventDispatch::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return EventProvider
|
|
||||||
*/
|
|
||||||
public function getProvider(): EventProvider
|
|
||||||
{
|
|
||||||
return Kiri::getDi()->get(EventProvider::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ContainerInterface
|
|
||||||
*/
|
|
||||||
public function getContainer(): ContainerInterface
|
|
||||||
{
|
|
||||||
return Kiri::getDi();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/3/30 0030
|
|
||||||
* Time: 14:11
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
namespace Kiri\Abstracts;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface Configure
|
|
||||||
* @package Kiri\Base
|
|
||||||
*/
|
|
||||||
interface Configure
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -7,9 +7,7 @@ use Kiri\Coordinator;
|
|||||||
class CoordinatorManager
|
class CoordinatorManager
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private static array $_items = [];
|
||||||
|
|
||||||
private static array $_waite = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,10 +16,10 @@ class CoordinatorManager
|
|||||||
*/
|
*/
|
||||||
public static function utility(string $category): Coordinator
|
public static function utility(string $category): Coordinator
|
||||||
{
|
{
|
||||||
if (!((static::$_waite[$category] ?? null) instanceof Coordinator)) {
|
if (!((static::$_items[$category] ?? null) instanceof Coordinator)) {
|
||||||
static::$_waite[$category] = new Coordinator();
|
static::$_items[$category] = new Coordinator();
|
||||||
}
|
}
|
||||||
return static::$_waite[$category];
|
return static::$_items[$category];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ use Symfony\Component\Console\{Application as ConsoleApplication,
|
|||||||
Output\OutputInterface
|
Output\OutputInterface
|
||||||
};
|
};
|
||||||
use Kiri\Server\ServerCommand;
|
use Kiri\Server\ServerCommand;
|
||||||
|
use Kiri\Server\FileBuildCommand;
|
||||||
use Kiri\Di\Inject\Container;
|
use Kiri\Di\Inject\Container;
|
||||||
use function config;
|
use function config;
|
||||||
|
|
||||||
@@ -57,9 +58,12 @@ class Application extends BaseApplication
|
|||||||
$this->errorHandler->registerShutdownHandler(config('site.error.shutdown', []));
|
$this->errorHandler->registerShutdownHandler(config('site.error.shutdown', []));
|
||||||
$this->errorHandler->registerExceptionHandler(config('site.error.exception', []));
|
$this->errorHandler->registerExceptionHandler(config('site.error.exception', []));
|
||||||
$this->errorHandler->registerErrorHandler(config('site.error.error', []));
|
$this->errorHandler->registerErrorHandler(config('site.error.error', []));
|
||||||
|
|
||||||
$this->id = config('site.id', uniqid('id.'));
|
$this->id = config('site.id', uniqid('id.'));
|
||||||
|
|
||||||
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
|
$this->provider->on(OnBeforeCommandExecute::class, [$this, 'beforeCommandExecute']);
|
||||||
|
|
||||||
|
parent::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,12 +74,14 @@ class Application extends BaseApplication
|
|||||||
*/
|
*/
|
||||||
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
|
public function beforeCommandExecute(OnBeforeCommandExecute $beforeCommandExecute): void
|
||||||
{
|
{
|
||||||
if ($beforeCommandExecute->command instanceof ServerCommand) {
|
if ($beforeCommandExecute->command instanceof ServerCommand || $beforeCommandExecute->command instanceof FileBuildCommand) {
|
||||||
if (config('servers.reload.hot', false) === false) {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$scanner = $this->container->get(Scanner::class);
|
$scanner = $this->container->get(Scanner::class);
|
||||||
|
$scanner->setConfig(array_merge(
|
||||||
|
config('servers.reload.scan', []),
|
||||||
|
config('site.scanner', [])
|
||||||
|
));
|
||||||
$scanner->scan(APP_PATH . 'app/');
|
$scanner->scan(APP_PATH . 'app/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
namespace Kiri;
|
namespace Kiri;
|
||||||
|
|
||||||
|
use Swoole\Coroutine;
|
||||||
|
use Swoole\Coroutine\Channel;
|
||||||
|
|
||||||
class Coordinator
|
class Coordinator
|
||||||
{
|
{
|
||||||
|
|
||||||
const string WORKER_START = 'worker:start';
|
const string WORKER_START = 'worker:start';
|
||||||
|
|
||||||
private bool $waite = true;
|
private bool $wait = false;
|
||||||
|
|
||||||
|
private ?Channel $channel = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,18 +21,28 @@ class Coordinator
|
|||||||
*/
|
*/
|
||||||
public function yield(): void
|
public function yield(): void
|
||||||
{
|
{
|
||||||
while ($this->waite) {
|
if (Coroutine::getCid() > 0) {
|
||||||
usleep(1000);
|
if ($this->channel instanceof Channel) {
|
||||||
}
|
$this->channel->pop();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->wait === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while ($this->wait === true) {
|
||||||
|
usleep(1000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function waite(): void
|
public function wait(): void
|
||||||
{
|
{
|
||||||
$this->waite = true;
|
Coroutine::getCid() > 0 ? $this->channel = new Channel(1) : $this->wait = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -37,8 +51,13 @@ class Coordinator
|
|||||||
*/
|
*/
|
||||||
public function done(): void
|
public function done(): void
|
||||||
{
|
{
|
||||||
$this->waite = false;
|
if (Coroutine::getCid() > 0) {
|
||||||
|
$this->channel?->push(true);
|
||||||
|
$this->channel->close();
|
||||||
|
$this->channel = null;
|
||||||
|
} else {
|
||||||
|
$this->wait = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,14 +22,9 @@ class Environmental
|
|||||||
*/
|
*/
|
||||||
public function isMac(): bool
|
public function isMac(): bool
|
||||||
{
|
{
|
||||||
$output = strtolower(PHP_OS | PHP_OS_FAMILY);
|
$os = strtolower(PHP_OS);
|
||||||
if (str_contains('mac', $output)) {
|
|
||||||
return true;
|
return str_contains($os, 'mac') || str_contains($os, 'darwin');
|
||||||
} else if (str_contains('darwin', $output)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,11 +33,7 @@ class Environmental
|
|||||||
*/
|
*/
|
||||||
#[Pure] public function isLinux(): bool
|
#[Pure] public function isLinux(): bool
|
||||||
{
|
{
|
||||||
if (!static::isMac()) {
|
return PHP_OS_FAMILY === 'Linux' || strtolower(PHP_OS) === 'linux';
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Kiri\Error;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use ErrorException;
|
use ErrorException;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
use Kiri\Di\Scanner;
|
||||||
use Kiri\Events\OnSystemError;
|
use Kiri\Events\OnSystemError;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -85,16 +86,23 @@ class ErrorHandler extends Component implements ErrorInterface
|
|||||||
public function shutdown(): void
|
public function shutdown(): void
|
||||||
{
|
{
|
||||||
$lastError = error_get_last();
|
$lastError = error_get_last();
|
||||||
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
|
$fatalTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
|
||||||
|
if (empty($lastError) || !in_array($lastError['type'], $fatalTypes, true)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->getLogger()->println(json_encode($lastError,JSON_UNESCAPED_UNICODE));
|
if (class_exists(Scanner::class, false)) {
|
||||||
|
$scanFile = Scanner::getCurrentFile();
|
||||||
|
if ($scanFile !== null) {
|
||||||
|
$lastError['scanner_file'] = $scanFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->getLogger()->println(json_encode($lastError, JSON_UNESCAPED_UNICODE));
|
||||||
|
|
||||||
event(new OnSystemError());
|
event(new OnSystemError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Throwable $exception
|
* @param Throwable $exception
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -106,7 +106,8 @@ class MongoDB
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代理方法调用到 MongoDB Client
|
* 代理方法调用到 MongoDB Client,内置连接健康检查和回收
|
||||||
|
* 异常时关闭连接并回退计数器,防止断连对象污染连接池
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -124,21 +125,53 @@ class MongoDB
|
|||||||
// 如果方法存在于 Database,通过默认数据库调用
|
// 如果方法存在于 Database,通过默认数据库调用
|
||||||
$database = $this->getDatabase();
|
$database = $this->getDatabase();
|
||||||
if (method_exists($database, $name)) {
|
if (method_exists($database, $name)) {
|
||||||
return $database->{$name}(...$arguments);
|
$result = $database->{$name}(...$arguments);
|
||||||
|
$this->returnClient($client);
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new \BadMethodCallException("Method {$name} does not exist on MongoDB Client or Database.");
|
throw new \BadMethodCallException("Method {$name} does not exist on MongoDB Client or Database.");
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
\Kiri::getLogger()->json_log($throwable);
|
\Kiri::getLogger()->json_log($throwable);
|
||||||
|
|
||||||
|
$this->closeClient($client);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
|
||||||
// MongoDB 连接是持久的,不需要释放
|
|
||||||
$this->pool()->push($this->getName(), $client);
|
|
||||||
}
|
}
|
||||||
|
$this->returnClient($client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 归还连接
|
||||||
|
* @param Client $client
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function returnClient(Client $client): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->pool()->push($this->getName(), $client);
|
||||||
|
} catch (\Throwable) {
|
||||||
|
$this->closeClient($client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭连接并回退计数器
|
||||||
|
* @param Client $client
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function closeClient(Client $client): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$client->close();
|
||||||
|
} catch (\Throwable) {
|
||||||
|
}
|
||||||
|
$this->pool()->abandon($this->getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行 MongoDB 命令
|
* 执行 MongoDB 命令
|
||||||
* @param array|object $command
|
* @param array|object $command
|
||||||
@@ -382,4 +415,3 @@ class MongoDB
|
|||||||
], $this->options);
|
], $this->options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -136,6 +136,8 @@ class Redis
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 代理 Redis 方法调用,内置健康检查和连接回收
|
||||||
|
* 如果连接 ping 失败则关闭连接并移除,不归还池中防止污染
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @return mixed
|
* @return mixed
|
||||||
@@ -151,6 +153,9 @@ class Redis
|
|||||||
} finally {
|
} finally {
|
||||||
if ($client->ping('h') == 'h') {
|
if ($client->ping('h') == 'h') {
|
||||||
$this->pool()->push($this->getName(), $client);
|
$this->pool()->push($this->getName(), $client);
|
||||||
|
} else {
|
||||||
|
$client->close();
|
||||||
|
$this->pool()->abandon($this->getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -67,7 +67,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
sock = new WebSocket('wss://center-wss.stupideyes.com/ws?access_token=a8exu0la-77ecu-ijhk-inkgt6vln-rlwwuk');
|
sock = new WebSocket('ws://121.40.147.153:6611/sockets/?auth=dG9rZW49NjYxNTBhMWQwYS0wMWItMDMxYS0wZTRjNTJiNGEtMjkzMTI5JnRpbWU9MTc4MjEyODcwMCZyZWZyZXNoPTJzMTdyR1BMWDRuWElKWExycTdNU1pQNEsyUXQwQ1pId1RsWUM5dzZJTmpLU1dvOThocW5KR0tQVFNCU2E2QTllNWRMTm9DYkRBaVhVQVd1cUU3Q1B5ejZLMTVzamdaTlpJWHhXNVlKeWQ2ODBaTkhZdktHUnd5RklKT1NZTEVBMURSb3JrbTUwRVUrTmpNMjJJL0lsMmpPT3p2MzFFNFZ4WkZwb1pXSmZVV015Q1VZenFtYTg4MDRHZ0Z3anZobDlYdWFkakR3QmhmZFd4QloxSC9HWndDRHdVQjN3elFrL01wUWFOSTB2YTlqZjZzQTRESFI2VlJtYWpxYWFHdkVNV3BGOXllbGVrTXFhcUhyT0tkdUpZRXVpSzEzZkNoRUljblFqdUVzWjdGUDBYVGlkNTUybUlyQnlYQjc0YktxQVRNbklvZlFQcnl3aEVUNGRSZVhhdz09');
|
||||||
// sock = new WebSocket('wss://meet-bottle.zhuangb123.com/socket/?auth=dG9rZW49M2I2ODJhNzg0NS0xMTktMzBiMS1mMDkxOGRhNjktNTg2ZDEyJnRpbWU9MTc1MzA4MTI5MyZyZWZyZXNoPXBDT0VFYk9KOG8xTEVZQytyUkR4VlZIaXR1TmVWcndCY2crRTBua2U1ZkJuUWNJaHl6NUtTV0x2ZExXa1Y5aXlyK3NmRnRwOVRCVU91MnhPSVRPRjROTjhoT0hlODNNVmZjN1NXb2QyeDY0TXEvZTFEUCtySjNzNjZhVlplcXdYV0QzV2VRd0V6YkowZ29oOFFqRHVvZGcyb281OEZkZVp5TjVIcHFyejRZQ0VMbkxydXlCUmpFdjNTWnRsQ3gxMWthNDNxbEwzM1lJYVlaV2t3dEhOMm9VaXllNFpKOHFnU1FueEZ4N0c4RDhabzBhajFFeEJIZTlJUFQ0VUo3UkR0V0g2Y3A3bkY3bXlkVHB4Wnp5NG1kRlgxa3M5eC9iVlJHaVFDRnU4VEFsUVdDdHEzbmJ1TnNYZVd3Q2dXWEd1OEUzMld3THVFRzRCZFRCanA2MGtYUT09');
|
// sock = new WebSocket('wss://meet-bottle.zhuangb123.com/socket/?auth=dG9rZW49M2I2ODJhNzg0NS0xMTktMzBiMS1mMDkxOGRhNjktNTg2ZDEyJnRpbWU9MTc1MzA4MTI5MyZyZWZyZXNoPXBDT0VFYk9KOG8xTEVZQytyUkR4VlZIaXR1TmVWcndCY2crRTBua2U1ZkJuUWNJaHl6NUtTV0x2ZExXa1Y5aXlyK3NmRnRwOVRCVU91MnhPSVRPRjROTjhoT0hlODNNVmZjN1NXb2QyeDY0TXEvZTFEUCtySjNzNjZhVlplcXdYV0QzV2VRd0V6YkowZ29oOFFqRHVvZGcyb281OEZkZVp5TjVIcHFyejRZQ0VMbkxydXlCUmpFdjNTWnRsQ3gxMWthNDNxbEwzM1lJYVlaV2t3dEhOMm9VaXllNFpKOHFnU1FueEZ4N0c4RDhabzBhajFFeEJIZTlJUFQ0VUo3UkR0V0g2Y3A3bkY3bXlkVHB4Wnp5NG1kRlgxa3M5eC9iVlJHaVFDRnU4VEFsUVdDdHEzbmJ1TnNYZVd3Q2dXWEd1OEUzMld3THVFRzRCZFRCanA2MGtYUT09');
|
||||||
sock.onopen = function (data) {
|
sock.onopen = function (data) {
|
||||||
if (tick) {
|
if (tick) {
|
||||||
|
|||||||
Reference in New Issue
Block a user