add basic container usage example

This commit is contained in:
sunxyw 2022-04-05 01:07:51 +08:00 committed by sunxyw
parent 90290bee94
commit cad5636f92
No known key found for this signature in database
GPG Key ID: CEA01A083E98C578

View File

@ -12,12 +12,15 @@ use ZM\Annotation\Http\RequestMapping;
use ZM\Annotation\Swoole\OnCloseEvent;
use ZM\Annotation\Swoole\OnOpenEvent;
use ZM\Annotation\Swoole\OnRequestEvent;
use ZM\Annotation\Swoole\OnStart;
use ZM\API\CQ;
use ZM\API\OneBotV11;
use ZM\API\TuringAPI;
use ZM\Config\ZMConfig;
use ZM\ConnectionManager\ConnectionObject;
use ZM\Console\Console;
use ZM\Container\WorkerContainer;
use ZM\Context\ContextInterface;
use ZM\Event\EventDispatcher;
use ZM\Exception\InterruptException;
use ZM\Module\QQBot;
@ -244,4 +247,31 @@ class Hello
{
bot()->all()->allGroups()->sendGroupMsg(0, ctx()->getMessage());
}
/*
* @OnStart(-1)
*/
#[OnStart(-1)]
public function initContainer()
{
$worker_container = new WorkerContainer();
$worker_container->bindIf('echo', function ($container, $parameters) {
return 'Hello, ' . $parameters[0];
});
container()->bind(ContextInterface::class, function () {
return ctx();
});
}
/**
* 容器呐
*
* @CQCommand("container")
*/
#[CQCommand('container')]
public function container(): string
{
resolve(ContextInterface::class)->reply('Good'); // ctx()->reply('good')
return resolve('echo', ['hello']); // Hello, hello
}
}