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;
|
|
|
|
|
2019-12-01 12:04:31 +01:00
|
|
|
use function Shlinkio\Shlink\Common\loadConfigFromGlob;
|
2018-12-23 10:18:38 +01:00
|
|
|
use function sprintf;
|
2016-07-19 17:07:59 +02:00
|
|
|
|
|
|
|
class ConfigProvider
|
|
|
|
{
|
2019-11-24 12:41:12 +01:00
|
|
|
private const ROUTES_PREFIX = '/rest/v{version:1|2}';
|
2018-01-07 20:45:05 +01:00
|
|
|
|
2019-12-01 12:04:31 +01:00
|
|
|
/** @var callable */
|
|
|
|
private $loadConfig;
|
|
|
|
|
|
|
|
public function __construct(?callable $loadConfig = null)
|
|
|
|
{
|
|
|
|
$this->loadConfig = $loadConfig ?? function (string $glob) {
|
|
|
|
return loadConfigFromGlob($glob);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:07:59 +02:00
|
|
|
public function __invoke()
|
|
|
|
{
|
2019-12-01 12:04:31 +01:00
|
|
|
$config = ($this->loadConfig)(__DIR__ . '/../config/{,*.}config.php');
|
2018-01-07 20:46:28 +01:00
|
|
|
return $this->applyRoutesPrefix($config);
|
2018-01-07 20:45:05 +01: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 11:24:55 +01:00
|
|
|
['path' => $path] = $route;
|
|
|
|
$routes[$key]['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path);
|
2018-01-07 20:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
2016-07-19 17:07:59 +02:00
|
|
|
}
|
|
|
|
}
|