mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 12:13:13 +08:00
Centralized how routes are configured to support multi-segment slugs
This commit is contained in:
@@ -8,7 +8,6 @@ use function Functional\first;
|
||||
use function Functional\map;
|
||||
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
@@ -21,16 +20,12 @@ class ConfigProvider
|
||||
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
|
||||
}
|
||||
|
||||
public static function applyRoutesPrefix(array $routes, bool $multiSegmentEnabled): array
|
||||
public static function applyRoutesPrefix(array $routes): array
|
||||
{
|
||||
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
|
||||
$prefixedRoutes = map($routes, static function (array $route) use ($multiSegmentEnabled) {
|
||||
$prefixedRoutes = map($routes, static function (array $route) {
|
||||
['path' => $path] = $route;
|
||||
if ($multiSegmentEnabled) {
|
||||
$path = str_replace('{shortCode}', '{shortCode:.+}', $path);
|
||||
}
|
||||
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
||||
|
||||
return $route;
|
||||
});
|
||||
|
||||
|
||||
@@ -33,9 +33,9 @@ class ConfigProviderTest extends TestCase
|
||||
* @test
|
||||
* @dataProvider provideRoutesConfig
|
||||
*/
|
||||
public function routesAreProperlyPrefixed(array $routes, bool $multiSegmentEnabled, array $expected): void
|
||||
public function routesAreProperlyPrefixed(array $routes, array $expected): void
|
||||
{
|
||||
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes, $multiSegmentEnabled));
|
||||
self::assertEquals($expected, ConfigProvider::applyRoutesPrefix($routes));
|
||||
}
|
||||
|
||||
public function provideRoutesConfig(): iterable
|
||||
@@ -47,7 +47,6 @@ class ConfigProviderTest extends TestCase
|
||||
['path' => '/baz/foo'],
|
||||
['path' => '/health'],
|
||||
],
|
||||
false,
|
||||
[
|
||||
['path' => '/rest/v{version:1|2}/foo'],
|
||||
['path' => '/rest/v{version:1|2}/bar'],
|
||||
@@ -62,25 +61,11 @@ 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'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user