This commit is contained in:
as2252258@163.com
2021-08-04 02:43:28 +08:00
parent 4f19878dd6
commit ae5fd2b587
4 changed files with 59 additions and 26 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ class Controller
*
* @var Response|null
*/
#[Inject('response')]
#[Inject(\Server\Response::class)]
public ?Response $response = null;
+16 -4
View File
@@ -159,9 +159,19 @@ class Response extends HttpService
*/
public function getBuilder(mixed $data, SResponse $response = null): static
{
if ($response === null) {
return $this->setContent($data);
if ($response != null) {
$this->configure($response);
}
return $this->setContent($data);
}
/**
* @param \Swoole\Http\Response|null $response
* @throws \Exception
*/
public function configure(SResponse $response = null): static
{
$response->setStatusCode($this->statusCode);
if (!isset($response->header['Content-Type'])) {
$response->header('Content-Type', $this->getResponseFormat());
@@ -177,7 +187,7 @@ class Response extends HttpService
$response->setCookie(...$header);
}
}
return $this->setContent($data);
return $this;
}
@@ -202,9 +212,11 @@ class Response extends HttpService
/**
* @param mixed $content
*/
public function setContent(mixed $content): static
public function setContent(mixed $content, $statusCode = 200, $format = self::JSON): static
{
$this->endData = $content;
$this->setStatusCode($statusCode);
$this->setFormat($format);
return $this;
}
+10 -21
View File
@@ -36,9 +36,10 @@ class HTTPServerListener extends Abstracts\Server
#[Inject('router')]
public Router $router;
/** @var \HttpServer\Http\Response|mixed */
#[Inject(\HttpServer\Http\Response::class)]
public \HttpServer\Http\Response $response;
/** @var \Server\Response|mixed */
#[Inject(\Server\Response::class)]
public \Server\Response $response;
/** @var EventDispatch */
@@ -96,32 +97,20 @@ class HTTPServerListener extends Abstracts\Server
if (!($node instanceof Node)) {
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
}
$data = $node->dispatch();
$responseData = $this->response->setContent($node->dispatch(),200);
} catch (Error | Throwable $exception) {
$response->header('Content-Type','text/html;charset=utf-8');
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
$data = $code ? $exception->getMessage() : jTraceEx($exception);
$responseData = $this->response->setContent($data, $code, \Server\Response::HTML);
} finally {
$this->requestEnd($data, $response, $code ?? 200);
$response->end($responseData->configure($response)->getContent());
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}
/**
* @param mixed $data
* @param \Swoole\Http\Response $response
*/
protected function requestEnd(mixed $data, Response $response, $code)
{
$sResponse = $this->response->getBuilder($data, $response);
$response->setStatusCode($code);
$response->end($sResponse->getContent());
$this->eventDispatch->dispatch(new OnAfterRequest());
}
/**
* @param Server $server
* @param int $fd
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace Server;
use HttpServer\Http\Context;
/**
* Class Response
* @package Server
* @mixin \HttpServer\Http\Response
*/
class Response
{
const JSON = 'json';
const XML = 'xml';
const HTML = 'html';
/**
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
return Context::getContext(\HttpServer\Http\Response::class)->{$name}(...$args);
}
}