Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复使用 Route 注解时无法自定义响应包体的 Bug #305

Merged
merged 1 commit into from Feb 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ZM/Event/Listener/HttpEventListener.php
Expand Up @@ -8,6 +8,7 @@
use Choir\Http\Stream;
use OneBot\Driver\Event\Http\HttpRequestEvent;
use OneBot\Util\Singleton;
use Psr\Http\Message\ResponseInterface;
use ZM\Annotation\AnnotationHandler;
use ZM\Annotation\Framework\BindEvent;
use ZM\Annotation\Http\Route;
Expand Down Expand Up @@ -51,7 +52,10 @@ public function onRequest999(HttpRequestEvent $event)
// $div->request_method = $node['request_method'];
$div->class = $node['class'];
$route_handler->handle($div, null, $params, $event->getRequest(), $event);
if (is_string($val = $route_handler->getReturnVal()) || ($val instanceof \Stringable)) {
$val = $route_handler->getReturnVal();
if ($val instanceof ResponseInterface) {
$event->withResponse($val);
} elseif (is_string($val)) {
// 返回的内容是可以被字符串化的,就当作 Body 来返回,状态码 200
$event->withResponse(HttpFactory::createResponse(200, null, [], Stream::create($val)));
} elseif ($event->getResponse() === null) {
Expand Down