Compare commits

...

2 Commits

Author SHA1 Message Date
as2252258 76351fbe66 eee 2026-06-24 20:11:12 +08:00
as2252258 8479106b9f eee 2026-06-12 23:57:25 +08:00
16 changed files with 193 additions and 293 deletions
+1
View File
@@ -34,3 +34,4 @@ runtime/
oot oot
d d
composer.lock composer.lock
.gstack/
-66
View File
@@ -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;
}
/** /**
+1 -1
View File
@@ -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": "*",
+11 -3
View File
@@ -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
+65 -129
View File
@@ -1,129 +1,65 @@
<?php <?php
/** declare(strict_types=1);
* Created by PhpStorm.
* User: whwyy namespace Kiri\Abstracts;
* Date: 2018/3/30 0030
* Time: 14:28 use Exception;
*/ use JetBrains\PhpStorm\Pure;
declare(strict_types=1); use Kiri;
use Kiri\Error\StdoutLogger;
namespace Kiri\Abstracts; use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider;
use Psr\Container\ContainerInterface;
use Exception;
use JetBrains\PhpStorm\Pure; class Component
use Kiri; {
use Kiri\Error\StdoutLogger;
use Kiri\Events\EventDispatch; public function __construct()
use Kiri\Events\EventProvider; {
use Psr\Container\ContainerInterface; }
/** public function init(): void
* Class Component {
* @package Kiri\Base }
* @property ContainerInterface $container
* @property EventDispatch $dispatch #[Pure] public static function className(): string
* @property EventProvider $provider {
*/ return static::class;
class Component implements Configure }
{
/**
* @throws
/** */
* BaseAbstract constructor. public function getLogger(): StdoutLogger
*/ {
public function __construct() return Kiri::getLogger();
{ }
}
public function getDispatch(): EventDispatch
{
/** return Kiri::getDi()->get(EventDispatch::class);
* @return void }
*/
public function init(): void public function getProvider(): EventProvider
{ {
} return Kiri::getDi()->get(EventProvider::class);
}
/** public function getContainer(): ContainerInterface
* @return string {
*/ return Kiri::getDi();
#[Pure] public static function className(): string }
{
return static::class; /**
} * @throws
*/
public function __get(string $name)
/** {
* @return StdoutLogger $method = 'get' . ucfirst($name);
* @throws if (method_exists($this, $method)) {
*/ return $this->{$method}();
public function getLogger(): StdoutLogger }
{ throw new Exception('Unable getting property ' . get_called_class() . '::' . $name);
return Kiri::getLogger(); }
}
}
/**
* @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();
}
}
-18
View File
@@ -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
{
}
+4 -6
View File
@@ -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];
} }
} }
+15 -15
View File
@@ -1,15 +1,15 @@
<?php <?php
namespace Kiri\Abstracts; namespace Kiri\Abstracts;
interface Kernel interface Kernel
{ {
/** /**
* @return array * @return array
*/ */
public function getCommands(): array; public function getCommands(): array;
} }
+12 -12
View File
@@ -1,12 +1,12 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Kiri\Abstracts; namespace Kiri\Abstracts;
interface Provider interface Provider
{ {
public function onImport(); public function onImport();
} }
+14 -14
View File
@@ -1,14 +1,14 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Kiri\Abstracts; namespace Kiri\Abstracts;
/** /**
* Class Providers * Class Providers
* @package Kiri\Abstracts * @package Kiri\Abstracts
*/ */
abstract class Providers extends Component implements Provider abstract class Providers extends Component implements Provider
{ {
} }
+3
View File
@@ -57,9 +57,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();
} }
+19 -9
View File
@@ -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 = true;
private ?Channel $channel = null;
/** /**
@@ -17,18 +21,24 @@ class Coordinator
*/ */
public function yield(): void public function yield(): void
{ {
while ($this->waite) { if (Coroutine::getCid() > 0) {
usleep(1000); $this->channel = new Channel(1);
} $this->channel->pop();
} else {
while ($this->wait) {
usleep(1000);
}
}
} }
/** /**
* @return void * @return void
*/ */
public function waite(): void public function wait(): void
{ {
$this->waite = true; $this->wait = true;
$this->channel = null;
} }
@@ -37,8 +47,8 @@ class Coordinator
*/ */
public function done(): void public function done(): void
{ {
$this->waite = false; $this->wait = false;
$this->channel?->push(true);
} }
} }
+4 -13
View File
@@ -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;
}
} }
} }
+38 -6
View File
@@ -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);
} }
} }
+5
View File
@@ -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
View File
@@ -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) {