mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 15:23:12 +08:00
Moved routes config together, and ensure they are loaded last
This commit is contained in:
@@ -4,8 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest;
|
||||
|
||||
use Closure;
|
||||
|
||||
use function Functional\first;
|
||||
use function Functional\map;
|
||||
use function Shlinkio\Shlink\Config\loadConfigFromGlob;
|
||||
@@ -17,38 +15,25 @@ class ConfigProvider
|
||||
private const UNVERSIONED_ROUTES_PREFIX = '/rest';
|
||||
public const UNVERSIONED_HEALTH_ENDPOINT_NAME = 'unversioned_health';
|
||||
|
||||
private Closure $loadConfig;
|
||||
|
||||
public function __construct(?callable $loadConfig = null)
|
||||
{
|
||||
$this->loadConfig = Closure::fromCallable($loadConfig ?? fn (string $glob) => loadConfigFromGlob($glob));
|
||||
}
|
||||
|
||||
public function __invoke(): array
|
||||
{
|
||||
$config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php');
|
||||
return $this->applyRoutesPrefix($config);
|
||||
return loadConfigFromGlob(__DIR__ . '/../config/{,*.}config.php');
|
||||
}
|
||||
|
||||
private function applyRoutesPrefix(array $config): array
|
||||
public static function applyRoutesPrefix(array $routes): array
|
||||
{
|
||||
$routes = $config['routes'] ?? [];
|
||||
$healthRoute = $this->buildUnversionedHealthRouteFromExistingRoutes($routes);
|
||||
|
||||
$prefixRoute = static function (array $route) {
|
||||
$healthRoute = self::buildUnversionedHealthRouteFromExistingRoutes($routes);
|
||||
$prefixedRoutes = map($routes, static function (array $route) {
|
||||
['path' => $path] = $route;
|
||||
$route['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
||||
|
||||
return $route;
|
||||
};
|
||||
$prefixedRoutes = map($routes, $prefixRoute);
|
||||
});
|
||||
|
||||
$config['routes'] = $healthRoute !== null ? [...$prefixedRoutes, $healthRoute] : $prefixedRoutes;
|
||||
|
||||
return $config;
|
||||
return $healthRoute !== null ? [...$prefixedRoutes, $healthRoute] : $prefixedRoutes;
|
||||
}
|
||||
|
||||
private function buildUnversionedHealthRouteFromExistingRoutes(array $routes): ?array
|
||||
private static function buildUnversionedHealthRouteFromExistingRoutes(array $routes): ?array
|
||||
{
|
||||
$healthRoute = first($routes, fn (array $route) => $route['path'] === '/health');
|
||||
if ($healthRoute === null) {
|
||||
|
||||
Reference in New Issue
Block a user