mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-05 06:43:12 +08:00
40 lines
1.3 KiB
PHP
40 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\Rest\Middleware;
|
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
use function strpos;
|
|
|
|
class PathVersionMiddleware implements MiddlewareInterface
|
|
{
|
|
// TODO The /health endpoint needs this middleware in order to work without the version.
|
|
// Take it into account if this middleware is ever removed.
|
|
|
|
/**
|
|
* Process an incoming server request and return a response, optionally delegating
|
|
* to the next middleware component to create the response.
|
|
*
|
|
* @param Request $request
|
|
* @param RequestHandlerInterface $handler
|
|
*
|
|
* @return Response
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
public function process(Request $request, RequestHandlerInterface $handler): Response
|
|
{
|
|
$uri = $request->getUri();
|
|
$path = $uri->getPath();
|
|
|
|
// If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
|
|
if (strpos($path, '/v') !== 0) {
|
|
$request = $request->withUri($uri->withPath('/v1' . $uri->getPath()));
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|