Removed duplicated code from BasePathPrefixer

This commit is contained in:
Alejandro Celaya 2019-09-13 20:22:41 +02:00
parent 6e38457655
commit bc07d77d06

View File

@ -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
{
return map($config['middleware_pipeline'] ?? [], function (array $middleware) use ($basePath) {
if (! isset($middleware['path'])) {
return $middleware;
} }
$middleware['path'] = $basePath . $middleware['path']; $element['path'] = $basePath . $element['path'];
return $middleware; return $element;
}); });
} }
} }