* @Link https://gitee.com/xmo/MineAdmin */ declare(strict_types=1); namespace Builder\Middlewares; use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\Utils\Codec\Json; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Builder\Helper\BaseCode; class HttpCoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware { /** * Handle the response when cannot found any routes. * @param ServerRequestInterface $request * @return ResponseInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function handleNotFound(ServerRequestInterface $request): ResponseInterface { $format = [ 'success' => false, 'code' => BaseCode::NOT_FOUND, 'message' => t('uiview.not_found') ]; return $this->response()->withHeader('Server', 'MineAdmin') ->withAddedHeader('content-type', 'application/json; charset=utf-8') ->withStatus(404) ->withBody(new SwooleStream(Json::encode($format))); } /** * Handle the response when the routes found but doesn't match any available methods. * @param array $methods * @param ServerRequestInterface $request * @return ResponseInterface * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ protected function handleMethodNotAllowed( array $methods, ServerRequestInterface $request): ResponseInterface { $format = [ 'success' => false, 'code' => BaseCode::METHOD_NOT_ALLOW, 'message' => t('uiview.allow_method', ['method' => implode(',', $methods)]) ]; return $this->response()->withHeader('Server', 'MineAdmin') ->withAddedHeader('content-type', 'application/json; charset=utf-8') ->withStatus(405) ->withBody(new SwooleStream(Json::encode($format))); } }