mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
refactor container injection
This commit is contained in:
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace ZM\Event\SwooleEvent;
|
||||
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Http\Server;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Throwable;
|
||||
use ZM\Annotation\Swoole\OnMessageEvent;
|
||||
@@ -15,6 +14,7 @@ use ZM\ConnectionManager\ConnectionObject;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Console\TermColor;
|
||||
use ZM\Container\Container;
|
||||
use ZM\Context\Context;
|
||||
use ZM\Context\ContextInterface;
|
||||
use ZM\Event\EventDispatcher;
|
||||
@@ -33,13 +33,7 @@ class OnMessage implements SwooleEvent
|
||||
$conn = ManagerGM::get($frame->fd);
|
||||
set_coroutine_params(['server' => $server, 'frame' => $frame, 'connection' => $conn]);
|
||||
|
||||
container()->instance(Server::class, $server);
|
||||
container()->instance(Frame::class, $frame);
|
||||
container()->instance(ConnectionObject::class, $conn);
|
||||
container()->bind(ContextInterface::class, function () {
|
||||
return ctx();
|
||||
});
|
||||
container()->alias(ContextInterface::class, Context::class);
|
||||
$this->registerRequestContainerBindings($frame, $conn);
|
||||
|
||||
$dispatcher1 = new EventDispatcher(OnMessageEvent::class);
|
||||
$dispatcher1->setRuleFunction(function ($v) use ($conn) {
|
||||
@@ -70,9 +64,21 @@ class OnMessage implements SwooleEvent
|
||||
Console::trace();
|
||||
} finally {
|
||||
container()->flush();
|
||||
if (Console::getLevel() >= 4) {
|
||||
Console::debug(sprintf('Request container [fd=%d, cid=%d] flushed.', $frame->fd, Coroutine::getCid()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册请求容器绑定
|
||||
*/
|
||||
private function registerRequestContainerBindings(Frame $frame, ?ConnectionObject $conn): void
|
||||
{
|
||||
$container = Container::getInstance();
|
||||
$container->setLogPrefix("[Container#{$frame->fd}]");
|
||||
$container->instance(Frame::class, $frame);
|
||||
$container->instance(ConnectionObject::class, $conn);
|
||||
$container->bind(ContextInterface::class, function () {
|
||||
return ctx();
|
||||
});
|
||||
$container->alias(ContextInterface::class, Context::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,11 +54,7 @@ class OnWorkerStart implements SwooleEvent
|
||||
}
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
|
||||
/* @noinspection PhpExpressionResultUnusedInspection */
|
||||
new WorkerContainer();
|
||||
if (Console::getLevel() >= 4) {
|
||||
Console::debug(sprintf('Worker container [id=%d,cid=%d] booted', $worker_id, Coroutine::getCid()));
|
||||
}
|
||||
$this->registerWorkerContainerBindings($server);
|
||||
|
||||
if ($server->taskworker === false) {
|
||||
Framework::saveProcessState(ZM_PROCESS_WORKER, $server->worker_pid, ['worker_id' => $worker_id]);
|
||||
@@ -292,4 +288,24 @@ class OnWorkerStart implements SwooleEvent
|
||||
DB::initTableList($real_conf['dbname']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册进程容器绑定
|
||||
*/
|
||||
private function registerWorkerContainerBindings(Server $server): void
|
||||
{
|
||||
$container = WorkerContainer::getInstance();
|
||||
$container->setLogPrefix("[WorkerContainer#{$server->worker_id}]");
|
||||
// 路径
|
||||
$container->instance('path.working', DataProvider::getWorkingDir());
|
||||
$container->instance('path.source', DataProvider::getSourceRootDir());
|
||||
$container->alias('path.source', 'path.base');
|
||||
$container->instance('path.config', DataProvider::getSourceRootDir() . '/config');
|
||||
$container->instance('path.module_config', ZMConfig::get('global', 'config_dir'));
|
||||
$container->instance('path.data', DataProvider::getDataFolder());
|
||||
$container->instance('path.framework', DataProvider::getFrameworkRootDir());
|
||||
// 基础
|
||||
$container->instance('server', $server);
|
||||
$container->instance('worker_id', $server->worker_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,6 @@ class OnWorkerStop implements SwooleEvent
|
||||
public function onCall(Server $server, $worker_id)
|
||||
{
|
||||
WorkerContainer::getInstance()->flush();
|
||||
if (Console::getLevel() >= 4) {
|
||||
Console::debug(sprintf('Worker container [id=%d] flushed', $worker_id));
|
||||
}
|
||||
|
||||
if ($worker_id == (ZMConfig::get('worker_cache')['worker'] ?? 0)) {
|
||||
LightCache::savePersistence();
|
||||
|
||||
Reference in New Issue
Block a user