2016-07-19 17:07:59 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 17:07:59 +02:00
|
|
|
namespace Shlinkio\Shlink\Rest;
|
|
|
|
|
2020-01-07 18:07:51 +01:00
|
|
|
use function Functional\first;
|
|
|
|
use function Functional\map;
|
2020-03-14 19:24:21 +01:00
|
|
|
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
|
2018-12-23 10:18:38 +01:00
|
|
|
use function sprintf;
|
2016-07-19 17:07:59 +02:00
|
|
|
|
|
|
|
class ConfigProvider
|
|
|
|
{
|
2022-08-13 17:15:04 +02:00
|
|
|
private const ROUTES_PREFIX = '/rest/v{version:1|2|3}';
|
2019-12-31 16:26:00 +01:00
|
|
|
private const UNVERSIONED_ROUTES_PREFIX = '/rest';
|
2020-01-06 23:32:43 +01:00
|
|
|
public const UNVERSIONED_HEALTH_ENDPOINT_NAME = 'unversioned_health';
|
2018-01-07 20:45:05 +01:00
|
|
|
|
2020-01-01 20:48:31 +01:00
|
|
|
public function __invoke(): array
|
2016-07-19 17:07:59 +02:00
|
|
|
{
|
2022-08-04 11:14:26 +02:00
|
|
|
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
|
2018-01-07 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
2022-08-06 09:30:13 +02:00
|
|
|
public static function applyRoutesPrefix(array $routes): array
|
2018-01-07 20:45:05 +01:00
|
|
|
{
|
2022-08-04 11:14:26 +02:00
|
|
|
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
|
2022-08-06 09:30:13 +02:00
|
|
|
$prefixedRoutes = map($routes, static function (array $route) {
|
2018-12-29 11:24:55 +01:00
|
|
|
['path' => $path] = $route;
|
2020-01-07 18:07:51 +01:00
|
|
|
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
|
|
|
return $route;
|
2022-08-04 11:14:26 +02:00
|
|
|
});
|
2018-01-07 20:45:05 +01:00
|
|
|
|
2022-08-04 11:14:26 +02:00
|
|
|
return $healthRoute !== null ? [...$prefixedRoutes, $healthRoute] : $prefixedRoutes;
|
2016-07-19 17:07:59 +02:00
|
|
|
}
|
2020-01-07 18:07:51 +01:00
|
|
|
|
2022-08-04 11:14:26 +02:00
|
|
|
private static function buildUnversionedHealthRouteFromExistingRoutes(array $routes): ?array
|
2020-01-07 18:07:51 +01:00
|
|
|
{
|
|
|
|
$healthRoute = first($routes, fn (array $route) => $route['path'] === '/health');
|
|
|
|
if ($healthRoute === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-04 11:49:33 +02:00
|
|
|
['path' => $path] = $healthRoute;
|
2020-01-07 18:07:51 +01:00
|
|
|
$healthRoute['path'] = sprintf('%s%s', self::UNVERSIONED_ROUTES_PREFIX, $path);
|
|
|
|
$healthRoute['name'] = self::UNVERSIONED_HEALTH_ENDPOINT_NAME;
|
|
|
|
|
|
|
|
return $healthRoute;
|
|
|
|
}
|
2016-07-19 17:07:59 +02:00
|
|
|
}
|