Skip to content

Commit adb0e73

Browse files
authored
feat: Add Htmlable contract interface for HTTP responses (#6794)
1 parent 1b9b660 commit adb0e73

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/CoreMiddleware.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Hyperf\Context\RequestContext;
2020
use Hyperf\Context\ResponseContext;
2121
use Hyperf\Contract\Arrayable;
22+
use Hyperf\Contract\Htmlable;
2223
use Hyperf\Contract\Jsonable;
2324
use Hyperf\Contract\NormalizerInterface;
2425
use Hyperf\Di\ClosureDefinitionCollectorInterface;
@@ -186,7 +187,7 @@ protected function prepareHandler(array|string $handler): array
186187
/**
187188
* Transfer the non-standard response content to a standard response object.
188189
*
189-
* @param null|array|Arrayable|Jsonable|ResponseInterface|string $response
190+
* @param null|array|Arrayable|Htmlable|Jsonable|ResponseInterface|string $response
190191
*/
191192
protected function transferToResponse($response, ServerRequestInterface $request): ResponsePlusInterface
192193
{
@@ -210,6 +211,12 @@ protected function transferToResponse($response, ServerRequestInterface $request
210211
->setBody(new SwooleStream((string) $response));
211212
}
212213

214+
if ($response instanceof Htmlable) {
215+
return $this->response()
216+
->addHeader('content-type', 'text/html')
217+
->setBody(new SwooleStream((string) $response));
218+
}
219+
213220
if ($this->response()->hasHeader('content-type')) {
214221
return $this->response()->setBody(new SwooleStream((string) $response));
215222
}

0 commit comments

Comments
 (0)