mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-17 08:32:04 +03:00
46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Shlinkio\Shlink\Rest;
|
|
|
|
use Closure;
|
|
|
|
use function Shlinkio\Shlink\Common\loadConfigFromGlob;
|
|
use function sprintf;
|
|
|
|
class ConfigProvider
|
|
{
|
|
private const ROUTES_PREFIX = '/rest/v{version:1|2}';
|
|
private const UNVERSIONED_ROUTES_PREFIX = '/rest';
|
|
|
|
private Closure $loadConfig;
|
|
|
|
public function __construct(?callable $loadConfig = null)
|
|
{
|
|
$this->loadConfig = Closure::fromCallable($loadConfig ?? fn (string $glob) => loadConfigFromGlob($glob));
|
|
}
|
|
|
|
public function __invoke()
|
|
{
|
|
$config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php');
|
|
return $this->applyRoutesPrefix($config);
|
|
}
|
|
|
|
private function applyRoutesPrefix(array $config): array
|
|
{
|
|
$routes =& $config['routes'] ?? [];
|
|
|
|
// Prepend the routes prefix to every path
|
|
foreach ($routes as $key => $route) {
|
|
['path' => $path] = $route;
|
|
$routes[$key]['path'] = sprintf(
|
|
'%s%s',
|
|
$path === '/health' ? self::UNVERSIONED_ROUTES_PREFIX : self::ROUTES_PREFIX,
|
|
$path,
|
|
);
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
}
|