From 566940349f8f35f0a44c8052a356dc058f1dcf52 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 13 Oct 2017 11:55:14 +0200 Subject: [PATCH] Created default delegate that returns a JSON response when accepted type is json --- module/Core/config/dependencies.config.php | 9 ++++ module/Core/src/Response/NotFoundDelegate.php | 54 +++++++++++++++++++ .../JsonErrorResponseGenerator.php | 7 +-- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 module/Core/src/Response/NotFoundDelegate.php diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index a21a92b8..d8009880 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -6,8 +6,10 @@ use Shlinkio\Shlink\Common\Service\PreviewGenerator; use Shlinkio\Shlink\Core\Action; use Shlinkio\Shlink\Core\Middleware; use Shlinkio\Shlink\Core\Options; +use Shlinkio\Shlink\Core\Response\NotFoundDelegate; use Shlinkio\Shlink\Core\Service; use Zend\Expressive\Router\RouterInterface; +use Zend\Expressive\Template\TemplateRendererInterface; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; return [ @@ -15,6 +17,7 @@ return [ 'dependencies' => [ 'factories' => [ Options\AppOptions::class => Options\AppOptionsFactory::class, + NotFoundDelegate::class => ConfigAbstractFactory::class, // Services Service\UrlShortener::class => ConfigAbstractFactory::class, @@ -29,9 +32,15 @@ return [ Action\PreviewAction::class => ConfigAbstractFactory::class, Middleware\QrCodeCacheMiddleware::class => ConfigAbstractFactory::class, ], + + 'aliases' => [ + 'Zend\Expressive\Delegate\DefaultDelegate' => NotFoundDelegate::class, + ], ], ConfigAbstractFactory::class => [ + NotFoundDelegate::class => [TemplateRendererInterface::class], + // Services Service\UrlShortener::class => ['httpClient', 'em', Cache::class, 'config.url_shortener.shortcode_chars'], Service\VisitsTracker::class => ['em'], diff --git a/module/Core/src/Response/NotFoundDelegate.php b/module/Core/src/Response/NotFoundDelegate.php new file mode 100644 index 00000000..da0710bb --- /dev/null +++ b/module/Core/src/Response/NotFoundDelegate.php @@ -0,0 +1,54 @@ +renderer = $renderer; + $this->template = $template; + } + + /** + * Dispatch the next available middleware and return the response. + * + * @param ServerRequestInterface $request + * + * @return ResponseInterface + * @throws \InvalidArgumentException + */ + public function process(ServerRequestInterface $request): ResponseInterface + { + $accepts = explode(',', $request->getHeaderLine('Accept')); + $accept = array_shift($accepts); + $status = StatusCodeInterface::STATUS_NOT_FOUND; + + // If the first accepted type is json, return a json response + if (in_array($accept, ['application/json', 'text/json', 'application/x-json'], true)) { + return new Response\JsonResponse([ + 'error' => 'NOT_FOUND', + 'message' => 'Not found', + ], $status); + } + + return new Response\HtmlResponse($this->renderer->render($this->template, ['request' => $request]), $status); + } +} diff --git a/module/Rest/src/ErrorHandler/JsonErrorResponseGenerator.php b/module/Rest/src/ErrorHandler/JsonErrorResponseGenerator.php index 664e693a..7d0b6013 100644 --- a/module/Rest/src/ErrorHandler/JsonErrorResponseGenerator.php +++ b/module/Rest/src/ErrorHandler/JsonErrorResponseGenerator.php @@ -18,6 +18,7 @@ class JsonErrorResponseGenerator implements ErrorResponseGeneratorInterface, Sta * @param Request $request * @param Response $response * @return Response + * @throws \InvalidArgumentException */ public function __invoke($e, Request $request, Response $response) { @@ -31,11 +32,7 @@ class JsonErrorResponseGenerator implements ErrorResponseGeneratorInterface, Sta ], $status); } - /** - * @param string $responsePhrase - * @return string - */ - protected function responsePhraseToCode($responsePhrase): string + protected function responsePhraseToCode(string $responsePhrase): string { return strtoupper(str_replace(' ', '_', $responsePhrase)); }