Added feature flag to enable/disable multi-segment support

This commit is contained in:
Alejandro Celaya
2022-08-04 11:49:33 +02:00
parent 7acf27dd38
commit 619999d4f8
9 changed files with 38 additions and 14 deletions
@@ -14,7 +14,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class DeleteShortUrlAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE];
public function __construct(private DeleteShortUrlServiceInterface $deleteShortUrlService)
@@ -16,7 +16,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class EditShortUrlAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH];
public function __construct(
@@ -15,7 +15,7 @@ use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
class ResolveShortUrlAction extends AbstractRestAction
{
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}';
protected const ROUTE_PATH = '/short-urls/{shortCode}';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
public function __construct(
@@ -18,7 +18,7 @@ class ShortUrlVisitsAction extends AbstractRestAction
{
use PagerfantaUtilsTrait;
protected const ROUTE_PATH = '/short-urls/{shortCode:.+}/visits';
protected const ROUTE_PATH = '/short-urls/{shortCode}/visits';
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
public function __construct(private VisitsStatsHelperInterface $visitsHelper)
+7 -3
View File
@@ -8,6 +8,7 @@ use function Functional\first;
use function Functional\map;
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
use function sprintf;
use function str_replace;
class ConfigProvider
{
@@ -20,11 +21,14 @@ class ConfigProvider
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
}
public static function applyRoutesPrefix(array $routes): array
public static function applyRoutesPrefix(array $routes, bool $multiSegmentEnabled): array
{
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
$prefixedRoutes = map($routes, static function (array $route) {
$prefixedRoutes = map($routes, static function (array $route) use ($multiSegmentEnabled) {
['path' => $path] = $route;
if ($multiSegmentEnabled) {
$path = str_replace('{shortCode}', '{shortCode:.+}', $path);
}
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
return $route;
@@ -40,7 +44,7 @@ class ConfigProvider
return null;
}
$path = $healthRoute['path'];
['path' => $path] = $healthRoute;
$healthRoute['path'] = sprintf('%s%s', self::UNVERSIONED_ROUTES_PREFIX, $path);
$healthRoute['name'] = self::UNVERSIONED_HEALTH_ENDPOINT_NAME;
+1 -1
View File
@@ -71,7 +71,7 @@ class CorsTest extends ApiTestCase
public function providePreflightEndpoints(): iterable
{
// yield 'invalid route' => ['/foo/bar', 'GET,POST,PUT,PATCH,DELETE']; // TODO This won't work with multi-segment
yield 'invalid route' => ['/foo/bar', 'GET,POST,PUT,PATCH,DELETE']; // TODO This won't work with multi-segment
yield 'short URLs route' => ['/short-urls', 'GET,POST'];
yield 'tags route' => ['/tags', 'GET,DELETE,PUT'];
yield 'health route' => ['/health', 'GET'];
+17 -2
View File
@@ -33,9 +33,9 @@ class ConfigProviderTest extends TestCase
* @test
* @dataProvider provideRoutesConfig
*/
public function routesAreProperlyPrefixed(array $routes, array $expected): void
public function routesAreProperlyPrefixed(array $routes, bool $multiSegmentEnabled, array $expected): void
{
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes));
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes, $multiSegmentEnabled));
}
public function provideRoutesConfig(): iterable
@@ -47,6 +47,7 @@ class ConfigProviderTest extends TestCase
['path' => '/baz/foo'],
['path' => '/health'],
],
false,
[
['path' => '/rest/v{version:1|2}/foo'],
['path' => '/rest/v{version:1|2}/bar'],
@@ -61,11 +62,25 @@ class ConfigProviderTest extends TestCase
['path' => '/bar'],
['path' => '/baz/foo'],
],
false,
[
['path' => '/rest/v{version:1|2}/foo'],
['path' => '/rest/v{version:1|2}/bar'],
['path' => '/rest/v{version:1|2}/baz/foo'],
],
];
yield 'multi-segment enabled' => [
[
['path' => '/foo'],
['path' => '/bar/{shortCode}'],
['path' => '/baz/{shortCode}/foo'],
],
true,
[
['path' => '/rest/v{version:1|2}/foo'],
['path' => '/rest/v{version:1|2}/bar/{shortCode:.+}'],
['path' => '/rest/v{version:1|2}/baz/{shortCode:.+}/foo'],
],
];
}
}