2016-07-19 18:07:59 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 18:07:59 +03:00
|
|
|
namespace Shlinkio\Shlink\Rest;
|
|
|
|
|
|
|
|
use Zend\Config\Factory;
|
|
|
|
use Zend\Stdlib\Glob;
|
2018-12-23 12:18:38 +03:00
|
|
|
use function sprintf;
|
2016-07-19 18:07:59 +03:00
|
|
|
|
|
|
|
class ConfigProvider
|
|
|
|
{
|
2018-12-23 12:18:38 +03:00
|
|
|
private const ROUTES_PREFIX = '/rest';
|
|
|
|
private const ROUTES_VERSION_PARAM = '/v{version:1}';
|
2018-01-07 22:45:05 +03:00
|
|
|
|
2016-07-19 18:07:59 +03:00
|
|
|
public function __invoke()
|
|
|
|
{
|
2018-01-07 22:46:28 +03:00
|
|
|
/** @var array $config */
|
|
|
|
$config = Factory::fromFiles(Glob::glob(__DIR__ . '/../config/{,*.}config.php', Glob::GLOB_BRACE));
|
|
|
|
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-23 12:18:38 +03:00
|
|
|
['can_be_versioned' => $routeCanBeVersioned, 'path' => $path] = $route;
|
|
|
|
$routes[$key]['path'] = sprintf(
|
|
|
|
'%s%s%s',
|
|
|
|
self::ROUTES_PREFIX,
|
|
|
|
$routeCanBeVersioned ? self::ROUTES_VERSION_PARAM : '',
|
|
|
|
$path
|
|
|
|
);
|
|
|
|
unset($routes[$key]['can_be_versioned']);
|
2018-01-07 22:45:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
2016-07-19 18:07:59 +03:00
|
|
|
}
|
|
|
|
}
|