From 8f43012a5c57790b4830ffd0ff36252609ff09f5 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Fri, 24 Feb 2023 23:09:37 +0800 Subject: [PATCH] fix properties not initialized --- config/container.php | 4 ++-- src/Module/Example/Hello123.php | 39 ++++++--------------------------- src/ZM/Kernel.php | 6 ++--- 3 files changed, 12 insertions(+), 37 deletions(-) diff --git a/config/container.php b/config/container.php index 9e7cc42c..099d2337 100644 --- a/config/container.php +++ b/config/container.php @@ -7,8 +7,8 @@ use OneBot\Driver\Process\ProcessManager; use Psr\Log\LoggerInterface; use ZM\Config\Environment; use ZM\Config\EnvironmentInterface; -use ZM\Config\ZMConfig; use ZM\Framework; +use ZM\Kernel; /* * 这里是容器的配置文件,你可以在这里配置容器的绑定和其他一些参数。 @@ -37,7 +37,7 @@ return [ // 详细介绍请参阅:https://php-di.org/doc/performances.html#caching 'cache' => [ // 是否启用缓存,支持 bool、callable - 'enable' => fn () => ZMConfig::getInstance()->getEnvironment() === 'production', + 'enable' => fn () => Kernel::getInstance()->environment('production'), 'namespace' => 'zm', ], ]; diff --git a/src/Module/Example/Hello123.php b/src/Module/Example/Hello123.php index 135d0b9e..94fd6381 100644 --- a/src/Module/Example/Hello123.php +++ b/src/Module/Example/Hello123.php @@ -5,15 +5,11 @@ declare(strict_types=1); namespace Module\Example; use Choir\Http\ServerRequest; -use Choir\WebSocket\FrameInterface; -use OneBot\Driver\Coroutine\Adaptive; use OneBot\Driver\Event\WebSocket\WebSocketMessageEvent; use ZM\Annotation\Framework\BindEvent; -use ZM\Annotation\Framework\Cron; use ZM\Annotation\Http\Route; use ZM\Annotation\Middleware\Middleware; use ZM\Annotation\OneBot\BotCommand; -use ZM\Annotation\OneBot\BotEvent; use ZM\Annotation\OneBot\CommandArgument; use ZM\Annotation\OneBot\CommandHelp; use ZM\Context\BotContext; @@ -21,22 +17,22 @@ use ZM\Middleware\TimerMiddleware; class Hello123 { + #[BindEvent(WebSocketMessageEvent::class, level: 5000)] + public function onMessage(WebSocketMessageEvent $event) + { + $Data = json_decode($event->getFrame()->getData(), true); + } + #[Route('/route', request_method: ['GET'])] #[Route('/route/{id}', request_method: ['GET'])] #[Middleware(TimerMiddleware::class)] - public function route(array $params, ServerRequest $request, \HttpRequestEvent $event) + public function route(array $params, ServerRequest $request, \HttpRequestEvent $event, BotContext $ctx) { // 目前因内部实现限制,路由方法的参数必须按照这个顺序定义,可以省略,但是不能乱序 // 如果希望获取其他依赖,可以在现有参数后面继续添加 return 'Hello Zhamao!This is the first 3.0 page!' . ($params['id'] ?? ''); } - #[BotEvent()] - public function onOBEvent(WebSocketMessageEvent $messageEvent, \OneBotEvent $event): void - { - logger()->info("收到了 {$event->getType()}.{$event->getDetailType()} 事件"); - } - #[BotCommand('echo', 'echo')] #[CommandArgument('text', '要回复的内容', required: true)] #[CommandHelp('复读机', '只需要发送 echo+内容 即可自动复读', 'echo 你好 会回复 你好')] @@ -44,25 +40,4 @@ class Hello123 { $context->reply($event->getMessage()); } - - #[BindEvent(WebSocketMessageEvent::class)] - public function onWSMessage(FrameInterface $frame, WebSocketMessageEvent $event): void - { - logger()->info('收到了 WebSocket 消息'); - } - - #[Cron('* * * * *', no_overlap: true)] - public function logTime(): void - { - $time = date('Y-m-d H:i:s'); - logger()->info('我看到时间了,让我写下来'); - Adaptive::sleep(5); - logger()->info('写好啦,时间是' . $time); - } - - #[Cron('* * * * *')] - public function logTime2(): void - { - logger()->info('我不需要等,但也不给你看时间'); - } } diff --git a/src/ZM/Kernel.php b/src/ZM/Kernel.php index 3a6445cd..352ee43b 100644 --- a/src/ZM/Kernel.php +++ b/src/ZM/Kernel.php @@ -13,13 +13,13 @@ class Kernel private array $bootstrappers = []; - private string $config_dir; + private string $config_dir = SOURCE_ROOT_DIR . '/config'; - private string $environment; + private string $environment = 'development'; private bool $debug_mode = false; - private string $log_level; + private string $log_level = 'info'; private bool $test_mode = false;