| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace App\Middlewares;
- use App\Constants\CommonConstant;
- use Hyperf\HttpMessage\Stream\SwooleStream;
- use Hyperf\HttpServer\CoreMiddleware;
- use Hyperf\Utils\Codec\Json;
- use Hyperf\Utils\Contracts\Arrayable;
- use Hyperf\Utils\Contracts\Jsonable;
- use Hyperf\Utils\Str;
- use Psr\Http\Message\ResponseInterface;
- use Psr\Http\Message\ServerRequestInterface;
- use Psr\Http\Server\RequestHandlerInterface;
- use App\Contracts\Responsable;
- /**
- * Core middleware of Hyperf, main responsibility is use to handle route info
- * and then delegate to the specified handler (which is Controller) to handle the request,
- * generate a response object and delegate to next middleware (Because this middleware is the
- * core middleware, then the next middleware also means it's the previous middlewares object) .
- */
- class AppCoreMiddleware extends CoreMiddleware
- {
- /**
- * Process an incoming server request and return a response, optionally delegating
- * response creation to a handler.
- */
- public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
- {
- // 覆盖Hyperf服务名字标识.
- $res = parent::process($request, $handler);
- return $res->withHeader('Server', env('RESPONSE_SERVER_NAME', 'Hyperf'));
- }
- /**
- * Transfer the non-standard response content to a standard response object.
- *
- * @param null|array|Arrayable|Jsonable|string $response
- */
- protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
- {
- // 加入Responsable钩子.
- if ($response instanceof Responsable) {
- $response = $response->toResponse($request);
- }
- if (is_string($response)) {
- return $this->response()->withAddedHeader('content-type', 'text/plain')->withBody(new SwooleStream($response));
- }
- if (is_array($response) || $response instanceof Arrayable) {
- return $this->response()
- ->withAddedHeader('content-type', 'application/json')
- ->withBody(new SwooleStream(Json::encode($response)));
- }
- if ($response instanceof Jsonable) {
- return $this->response()
- ->withAddedHeader('content-type', 'application/json')
- ->withBody(new SwooleStream((string) $response));
- }
- return $response;
- // return $this->response()->withAddedHeader('content-type', 'text/plain')->withBody(new SwooleStream((string) $response));
- }
- /**
- * Handle the response when cannot found any routes.
- *
- * @param ServerRequestInterface $request
- * @return array|Arrayable|mixed|ResponseInterface|string
- */
- protected function handleNotFound(ServerRequestInterface $request)
- {
- // 重写路由找不到的处理逻辑.
- $statusCode = 404;
- $clientRequest = request();
- $headerContentType = $clientRequest->header('content-type');
- if (!empty($headerContentType) && Str::contains($headerContentType, ['/json', '+json'])) {
- $res = [
- 'code' => $statusCode,
- 'message' => '404 not found',
- 'data' => [],
- 'handshake' => CommonConstant::HANDSHAKE,
- ];
- return $this->response()->withStatus($statusCode)->withAddedHeader('content-type', 'application/json')
- ->withBody(new SwooleStream(Json::encode($res)));
- } else {
- return $this->response()->withStatus($statusCode)->withBody(new SwooleStream((string)$statusCode));
- }
- }
- /**
- * Handle the response when the routes found but doesn't match any available methods.
- *
- * @return array|Arrayable|mixed|ResponseInterface|string
- */
- protected function handleMethodNotAllowed(array $methods, ServerRequestInterface $request)
- {
- // 重写 HTTP 方法不允许的处理逻辑.
- $statusCode = 405;
- $clientRequest = request();
- $headerContentType = $clientRequest->header('content-type');
- if (!empty($headerContentType) && Str::contains($headerContentType, ['/json', '+json'])) {
- // 重写路由找不到的处理逻辑.
- $res = [
- 'code' => $statusCode,
- 'message' => '405 Method Not Allowed',
- 'data' => [],
- 'handshake' => CommonConstant::HANDSHAKE,
- ];
- return $this->response()->withStatus($statusCode)->withAddedHeader('content-type', 'application/json')
- ->withBody(new SwooleStream(Json::encode($res)));
- } else {
- return $this->response()->withStatus($statusCode)->withAddedHeader('Allow', implode(', ', $methods))->withBody(new SwooleStream((string)$statusCode));
- }
- }
- }
|