mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 20:11:34 +03:00
Created middleware used with short codes creation actions to handle content negotiation
This commit is contained in:
parent
334710e92c
commit
59f10619ba
4 changed files with 56 additions and 3 deletions
|
@ -36,6 +36,7 @@ return [
|
||||||
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
||||||
Middleware\PathVersionMiddleware::class => InvokableFactory::class,
|
Middleware\PathVersionMiddleware::class => InvokableFactory::class,
|
||||||
Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class,
|
Middleware\CheckAuthenticationMiddleware::class => ConfigAbstractFactory::class,
|
||||||
|
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class => InvokableFactory::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Rest\Action;
|
use Shlinkio\Shlink\Rest\Action;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
@ -9,8 +11,12 @@ return [
|
||||||
Action\AuthenticateAction::getRouteDef(),
|
Action\AuthenticateAction::getRouteDef(),
|
||||||
|
|
||||||
// Short codes
|
// Short codes
|
||||||
Action\ShortCode\CreateShortCodeAction::getRouteDef(),
|
Action\ShortCode\CreateShortCodeAction::getRouteDef([
|
||||||
Action\ShortCode\SingleStepCreateShortCodeAction::getRouteDef(),
|
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class,
|
||||||
|
]),
|
||||||
|
Action\ShortCode\SingleStepCreateShortCodeAction::getRouteDef([
|
||||||
|
Middleware\ShortCode\CreateShortCodeContentNegotiationMiddleware::class,
|
||||||
|
]),
|
||||||
Action\ShortCode\EditShortCodeAction::getRouteDef(),
|
Action\ShortCode\EditShortCodeAction::getRouteDef(),
|
||||||
Action\ShortCode\ResolveUrlAction::getRouteDef(),
|
Action\ShortCode\ResolveUrlAction::getRouteDef(),
|
||||||
Action\ShortCode\ListShortCodesAction::getRouteDef(),
|
Action\ShortCode\ListShortCodesAction::getRouteDef(),
|
||||||
|
|
|
@ -77,7 +77,6 @@ abstract class AbstractCreateShortCodeAction extends AbstractRestAction
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
->withHost($this->domainConfig['hostname']);
|
->withHost($this->domainConfig['hostname']);
|
||||||
|
|
||||||
// TODO Make response to be generated based on Accept header
|
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'longUrl' => (string) $longUrl,
|
'longUrl' => (string) $longUrl,
|
||||||
'shortUrl' => (string) $shortUrl,
|
'shortUrl' => (string) $shortUrl,
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest\Middleware\ShortCode;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
use Zend\Diactoros\Response;
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
|
class CreateShortCodeContentNegotiationMiddleware implements MiddlewareInterface
|
||||||
|
{
|
||||||
|
private const PLAIN_TEXT = 'text';
|
||||||
|
private const JSON = 'json';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process an incoming server request and return a response, optionally delegating
|
||||||
|
* response creation to a handler.
|
||||||
|
* @throws \RuntimeException
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
|
{
|
||||||
|
/** @var JsonResponse $response */
|
||||||
|
$response = $handler->handle($request);
|
||||||
|
$acceptedType = $this->determineAcceptedType($request);
|
||||||
|
if ($acceptedType === self::JSON) {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If requested, return a plain text response containing the short URL only
|
||||||
|
$resp = (new Response())->withHeader('Content-Type', 'text/plain');
|
||||||
|
$body = $resp->getBody();
|
||||||
|
$body->write($response->getPayload()['shortUrl'] ?? '');
|
||||||
|
$body->rewind();
|
||||||
|
return $resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function determineAcceptedType(ServerRequestInterface $request): string
|
||||||
|
{
|
||||||
|
$accepts = \explode(',', $request->getHeaderLine('Accept'));
|
||||||
|
$accept = \strtolower(\array_shift($accepts));
|
||||||
|
return \strpos($accept, 'text/plain') !== false ? self::PLAIN_TEXT : self::JSON;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue