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