mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-27 17:31:20 -06:00
Removed duplicated code from BasePathPrefixer
This commit is contained in:
parent
6e38457655
commit
bc07d77d06
@ -7,33 +7,29 @@ use function Functional\map;
|
|||||||
|
|
||||||
class BasePathPrefixer
|
class BasePathPrefixer
|
||||||
{
|
{
|
||||||
|
private const ELEMENTS_WITH_PATH = ['routes', 'middleware_pipeline'];
|
||||||
|
|
||||||
public function __invoke(array $config): array
|
public function __invoke(array $config): array
|
||||||
{
|
{
|
||||||
$basePath = $config['router']['base_path'] ?? '';
|
$basePath = $config['router']['base_path'] ?? '';
|
||||||
$config['routes'] = $this->prefixRoutesWithBasePath($config, $basePath);
|
|
||||||
$config['middleware_pipeline'] = $this->prefixMiddlewarePathsWithBasePath($config, $basePath);
|
|
||||||
$config['url_shortener']['domain']['hostname'] .= $basePath;
|
$config['url_shortener']['domain']['hostname'] .= $basePath;
|
||||||
|
|
||||||
|
foreach (self::ELEMENTS_WITH_PATH as $configKey) {
|
||||||
|
$config[$configKey] = $this->prefixPathsWithBasePath($configKey, $config, $basePath);
|
||||||
|
}
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prefixRoutesWithBasePath(array $config, string $basePath): array
|
private function prefixPathsWithBasePath(string $configKey, array $config, string $basePath): array
|
||||||
{
|
{
|
||||||
return map($config['routes'] ?? [], function (array $route) use ($basePath) {
|
return map($config[$configKey] ?? [], function (array $element) use ($basePath) {
|
||||||
$route['path'] = $basePath . $route['path'];
|
if (! isset($element['path'])) {
|
||||||
return $route;
|
return $element;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function prefixMiddlewarePathsWithBasePath(array $config, string $basePath): array
|
$element['path'] = $basePath . $element['path'];
|
||||||
{
|
return $element;
|
||||||
return map($config['middleware_pipeline'] ?? [], function (array $middleware) use ($basePath) {
|
|
||||||
if (! isset($middleware['path'])) {
|
|
||||||
return $middleware;
|
|
||||||
}
|
|
||||||
|
|
||||||
$middleware['path'] = $basePath . $middleware['path'];
|
|
||||||
return $middleware;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user