2019-09-13 21:03:53 +03:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core\Config;
|
|
|
|
|
|
|
|
use function Functional\map;
|
|
|
|
|
|
|
|
class BasePathPrefixer
|
|
|
|
{
|
2019-09-13 21:22:41 +03:00
|
|
|
private const ELEMENTS_WITH_PATH = ['routes', 'middleware_pipeline'];
|
|
|
|
|
2019-09-13 21:03:53 +03:00
|
|
|
public function __invoke(array $config): array
|
|
|
|
{
|
|
|
|
$basePath = $config['router']['base_path'] ?? '';
|
|
|
|
$config['url_shortener']['domain']['hostname'] .= $basePath;
|
|
|
|
|
2019-09-13 21:22:41 +03:00
|
|
|
foreach (self::ELEMENTS_WITH_PATH as $configKey) {
|
|
|
|
$config[$configKey] = $this->prefixPathsWithBasePath($configKey, $config, $basePath);
|
|
|
|
}
|
2019-09-13 21:03:53 +03:00
|
|
|
|
2019-09-13 21:22:41 +03:00
|
|
|
return $config;
|
2019-09-13 21:03:53 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:22:41 +03:00
|
|
|
private function prefixPathsWithBasePath(string $configKey, array $config, string $basePath): array
|
2019-09-13 21:03:53 +03:00
|
|
|
{
|
2019-09-13 21:22:41 +03:00
|
|
|
return map($config[$configKey] ?? [], function (array $element) use ($basePath) {
|
|
|
|
if (! isset($element['path'])) {
|
|
|
|
return $element;
|
2019-09-13 21:03:53 +03:00
|
|
|
}
|
|
|
|
|
2019-09-13 21:22:41 +03:00
|
|
|
$element['path'] = $basePath . $element['path'];
|
|
|
|
return $element;
|
2019-09-13 21:03:53 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|