modify
This commit is contained in:
@@ -59,7 +59,7 @@ class Controller
|
||||
*
|
||||
* @var Response|null
|
||||
*/
|
||||
#[Inject('response')]
|
||||
#[Inject(\Server\Response::class)]
|
||||
public ?Response $response = null;
|
||||
|
||||
|
||||
|
||||
@@ -159,9 +159,19 @@ class Response extends HttpService
|
||||
*/
|
||||
public function getBuilder(mixed $data, SResponse $response = null): static
|
||||
{
|
||||
if ($response === null) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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,30 +97,18 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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());
|
||||
$response->end($responseData->configure($response)->getContent());
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -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