mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Created a config prost-processor which adds the base path on every applicable configuration
This commit is contained in:
39
module/Core/src/Config/BasePathPrefixer.php
Normal file
39
module/Core/src/Config/BasePathPrefixer.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use function Functional\map;
|
||||
|
||||
class BasePathPrefixer
|
||||
{
|
||||
public function __invoke(array $config): array
|
||||
{
|
||||
$basePath = $config['router']['base_path'] ?? '';
|
||||
$config['routes'] = $this->prefixRoutesWithBasePath($config, $basePath);
|
||||
$config['middleware_pipeline'] = $this->prefixMiddlewarePathsWithBasePath($config, $basePath);
|
||||
$config['url_shortener']['domain']['hostname'] .= $basePath;
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
private function prefixRoutesWithBasePath(array $config, string $basePath): array
|
||||
{
|
||||
return map($config['routes'] ?? [], function (array $route) use ($basePath) {
|
||||
$route['path'] = $basePath . $route['path'];
|
||||
return $route;
|
||||
});
|
||||
}
|
||||
|
||||
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'];
|
||||
return $middleware;
|
||||
});
|
||||
}
|
||||
}
|
||||
62
module/Core/src/Config/SimplifiedConfigParser.php
Normal file
62
module/Core/src/Config/SimplifiedConfigParser.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
||||
use Zend\Stdlib\ArrayUtils;
|
||||
|
||||
use function array_intersect_key;
|
||||
use function array_key_exists;
|
||||
use function Functional\contains;
|
||||
use function Functional\reduce_left;
|
||||
|
||||
class SimplifiedConfigParser
|
||||
{
|
||||
private const SIMPLIFIED_CONFIG_MAPPING = [
|
||||
'disable_track_param' => ['app_options', 'disable_track_param'],
|
||||
'short_domain_schema' => ['url_shortener', 'domain', 'schema'],
|
||||
'short_domain_host' => ['url_shortener', 'domain', 'hostname'],
|
||||
'validate_url' => ['url_shortener', 'validate_url'],
|
||||
'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'redirect_to'],
|
||||
'db_config' => ['entity_manager', 'connection'],
|
||||
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
||||
'redis_servers' => ['redis', 'servers'],
|
||||
'base_path' => ['router', 'base_path'],
|
||||
];
|
||||
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
|
||||
'not_found_redirect_to' => [
|
||||
'path' => ['url_shortener', 'not_found_short_url', 'enable_redirection'],
|
||||
'value' => true,
|
||||
],
|
||||
'delete_short_url_threshold' => [
|
||||
'path' => ['delete_short_urls', 'check_visits_threshold'],
|
||||
'value' => true,
|
||||
],
|
||||
'redis_servers' => [
|
||||
'path' => ['dependencies', 'aliases', 'lock_store'],
|
||||
'value' => 'redis_lock_store',
|
||||
],
|
||||
];
|
||||
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
|
||||
|
||||
public function __invoke(array $config): array
|
||||
{
|
||||
$existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
|
||||
|
||||
return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
|
||||
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
|
||||
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
|
||||
$value = ArrayUtils::merge($collection->getValueInPath($path), $value);
|
||||
}
|
||||
|
||||
$collection->setValueInPath($value, $path);
|
||||
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) {
|
||||
['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key];
|
||||
$collection->setValueInPath($sideEffectValue, $sideEffectPath);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}, new PathCollection($config))->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user