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;
|
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
use Closure;
|
|
|
|
|
2019-12-01 14:04:31 +03:00
|
|
|
use function Shlinkio\Shlink\Common\loadConfigFromGlob;
|
2018-12-23 12:18:38 +03:00
|
|
|
use function sprintf;
|
2016-07-19 18:07:59 +03:00
|
|
|
|
|
|
|
class ConfigProvider
|
|
|
|
{
|
2019-11-24 14:41:12 +03:00
|
|
|
private const ROUTES_PREFIX = '/rest/v{version:1|2}';
|
2019-12-31 18:26:00 +03:00
|
|
|
private const UNVERSIONED_ROUTES_PREFIX = '/rest';
|
2018-01-07 22:45:05 +03:00
|
|
|
|
2019-12-30 00:48:40 +03:00
|
|
|
private Closure $loadConfig;
|
2019-12-01 14:04:31 +03:00
|
|
|
|
|
|
|
public function __construct(?callable $loadConfig = null)
|
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
$this->loadConfig = Closure::fromCallable($loadConfig ?? fn (string $glob) => loadConfigFromGlob($glob));
|
2019-12-01 14:04:31 +03:00
|
|
|
}
|
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
public function __invoke(): array
|
2016-07-19 18:07:59 +03:00
|
|
|
{
|
2019-12-01 14:04:31 +03:00
|
|
|
$config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php');
|
2018-01-07 22:46:28 +03:00
|
|
|
return $this->applyRoutesPrefix($config);
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function applyRoutesPrefix(array $config): array
|
|
|
|
{
|
|
|
|
$routes =& $config['routes'] ?? [];
|
|
|
|
|
|
|
|
// Prepend the routes prefix to every path
|
|
|
|
foreach ($routes as $key => $route) {
|
2018-12-29 13:24:55 +03:00
|
|
|
['path' => $path] = $route;
|
2019-12-31 18:26:00 +03:00
|
|
|
$routes[$key]['path'] = sprintf(
|
|
|
|
'%s%s',
|
|
|
|
$path === '/health' ? self::UNVERSIONED_ROUTES_PREFIX : self::ROUTES_PREFIX,
|
|
|
|
$path,
|
|
|
|
);
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
2016-07-19 18:07:59 +03:00
|
|
|
}
|
|
|
|
}
|