shlink/module/Rest/src/ConfigProvider.php

42 lines
1,010 B
PHP
Raw Normal View History

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;
use Closure;
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
{
private const ROUTES_PREFIX = '/rest/v{version:1|2}';
2018-01-07 20:45:05 +01:00
private Closure $loadConfig;
public function __construct(?callable $loadConfig = null)
{
$this->loadConfig = Closure::fromCallable($loadConfig ?? fn (string $glob) => loadConfigFromGlob($glob));
}
2016-07-19 17:07:59 +02:00
public function __invoke()
{
$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
}
}