modify
This commit is contained in:
@@ -59,7 +59,7 @@ class Controller
|
|||||||
*
|
*
|
||||||
* @var Response|null
|
* @var Response|null
|
||||||
*/
|
*/
|
||||||
#[Inject('response')]
|
#[Inject(\Server\Response::class)]
|
||||||
public ?Response $response = null;
|
public ?Response $response = null;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -159,9 +159,19 @@ class Response extends HttpService
|
|||||||
*/
|
*/
|
||||||
public function getBuilder(mixed $data, SResponse $response = null): static
|
public function getBuilder(mixed $data, SResponse $response = null): static
|
||||||
{
|
{
|
||||||
if ($response === null) {
|
if ($response != null) {
|
||||||
return $this->setContent($data);
|
$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);
|
$response->setStatusCode($this->statusCode);
|
||||||
if (!isset($response->header['Content-Type'])) {
|
if (!isset($response->header['Content-Type'])) {
|
||||||
$response->header('Content-Type', $this->getResponseFormat());
|
$response->header('Content-Type', $this->getResponseFormat());
|
||||||
@@ -177,7 +187,7 @@ class Response extends HttpService
|
|||||||
$response->setCookie(...$header);
|
$response->setCookie(...$header);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this->setContent($data);
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -202,9 +212,11 @@ class Response extends HttpService
|
|||||||
/**
|
/**
|
||||||
* @param mixed $content
|
* @param mixed $content
|
||||||
*/
|
*/
|
||||||
public function setContent(mixed $content): static
|
public function setContent(mixed $content, $statusCode = 200, $format = self::JSON): static
|
||||||
{
|
{
|
||||||
$this->endData = $content;
|
$this->endData = $content;
|
||||||
|
$this->setStatusCode($statusCode);
|
||||||
|
$this->setFormat($format);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,9 +36,10 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
#[Inject('router')]
|
#[Inject('router')]
|
||||||
public Router $router;
|
public Router $router;
|
||||||
|
|
||||||
/** @var \HttpServer\Http\Response|mixed */
|
|
||||||
#[Inject(\HttpServer\Http\Response::class)]
|
/** @var \Server\Response|mixed */
|
||||||
public \HttpServer\Http\Response $response;
|
#[Inject(\Server\Response::class)]
|
||||||
|
public \Server\Response $response;
|
||||||
|
|
||||||
|
|
||||||
/** @var EventDispatch */
|
/** @var EventDispatch */
|
||||||
@@ -96,32 +97,20 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
if (!($node instanceof Node)) {
|
if (!($node instanceof Node)) {
|
||||||
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
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) {
|
} catch (Error | Throwable $exception) {
|
||||||
$response->header('Content-Type','text/html;charset=utf-8');
|
|
||||||
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
$code = $exception->getCode() == 0 ? 500 : $exception->getCode();
|
||||||
$data = $code ? $exception->getMessage() : jTraceEx($exception);
|
$data = $code ? $exception->getMessage() : jTraceEx($exception);
|
||||||
|
|
||||||
|
$responseData = $this->response->setContent($data, $code, \Server\Response::HTML);
|
||||||
} finally {
|
} 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 Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user