mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Defined new configs for not found redirects
This commit is contained in:
29
module/Core/src/Config/DeprecatedConfigParser.php
Normal file
29
module/Core/src/Config/DeprecatedConfigParser.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use function Functional\compose;
|
||||
|
||||
class DeprecatedConfigParser
|
||||
{
|
||||
public function __invoke(array $config): array
|
||||
{
|
||||
return compose([$this, 'parseNotFoundRedirect'])($config);
|
||||
}
|
||||
|
||||
public function parseNotFoundRedirect(array $config): array
|
||||
{
|
||||
// If the new config value is already set, keep it
|
||||
if (isset($config['not_found_redirects']['invalid_short_url'])) {
|
||||
return $config;
|
||||
}
|
||||
|
||||
$oldRedirectEnabled = $config['url_shortener']['not_found_short_url']['enable_redirection'] ?? false;
|
||||
$oldRedirectValue = $config['url_shortener']['not_found_short_url']['redirect_to'] ?? null;
|
||||
$config['not_found_redirects']['invalid_short_url'] = $oldRedirectEnabled ? $oldRedirectValue : null;
|
||||
|
||||
return $config;
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,13 @@ namespace Shlinkio\Shlink\Core\Config;
|
||||
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
||||
use Zend\Stdlib\ArrayUtils;
|
||||
|
||||
use function array_flip;
|
||||
use function array_intersect_key;
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function Functional\contains;
|
||||
use function Functional\reduce_left;
|
||||
use function uksort;
|
||||
|
||||
class SimplifiedConfigParser
|
||||
{
|
||||
@@ -19,17 +22,16 @@ class SimplifiedConfigParser
|
||||
'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'],
|
||||
'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated
|
||||
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
|
||||
'404_redirect_to' => ['not_found_redirects', '404'],
|
||||
'base_url_redirect_to' => ['not_found_redirects', 'base_path'],
|
||||
'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,
|
||||
@@ -43,9 +45,9 @@ class SimplifiedConfigParser
|
||||
|
||||
public function __invoke(array $config): array
|
||||
{
|
||||
$existingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
|
||||
$configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config);
|
||||
|
||||
return reduce_left($existingKeys, function ($value, string $key, $c, PathCollection $collection) {
|
||||
return reduce_left($configForExistingKeys, 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);
|
||||
@@ -60,4 +62,20 @@ class SimplifiedConfigParser
|
||||
return $collection;
|
||||
}, new PathCollection($config))->toArray();
|
||||
}
|
||||
|
||||
private function getConfigForKeysInMappingOrderedByMapping(array $config): array
|
||||
{
|
||||
// Ignore any config which is not defined in the mapping
|
||||
$configForExistingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING);
|
||||
|
||||
// Order the config by their key, based on the order it was defined in the mapping.
|
||||
// This mainly allows deprecating keys and defining new ones that will replace the older and always take
|
||||
// preference, while the old one keeps working for backwards compatibility if the new one is not provided.
|
||||
$simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING));
|
||||
uksort($configForExistingKeys, function (string $a, string $b) use ($simplifiedConfigOrder): int {
|
||||
return $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b];
|
||||
});
|
||||
|
||||
return $configForExistingKeys;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,10 +75,6 @@ class SimplifiedConfigParserTest extends TestCase
|
||||
'hostname' => 'doma.in',
|
||||
],
|
||||
'validate_url' => false,
|
||||
'not_found_short_url' => [
|
||||
'redirect_to' => 'foobar.com',
|
||||
'enable_redirection' => true,
|
||||
],
|
||||
],
|
||||
|
||||
'delete_short_urls' => [
|
||||
@@ -102,10 +98,38 @@ class SimplifiedConfigParserTest extends TestCase
|
||||
'router' => [
|
||||
'base_path' => '/foo/bar',
|
||||
],
|
||||
|
||||
'not_found_redirects' => [
|
||||
'invalid_short_url' => 'foobar.com',
|
||||
],
|
||||
];
|
||||
|
||||
$result = ($this->postProcessor)(array_merge($config, $simplified));
|
||||
|
||||
$this->assertEquals(array_merge($expected, $simplified), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideConfigWithDeprecates
|
||||
*/
|
||||
public function properlyMapsDeprecatedConfigs(array $config, string $expected): void
|
||||
{
|
||||
$result = ($this->postProcessor)($config);
|
||||
$this->assertEquals($expected, $result['not_found_redirects']['invalid_short_url']);
|
||||
}
|
||||
|
||||
public function provideConfigWithDeprecates(): iterable
|
||||
{
|
||||
yield 'only deprecated config' => [['not_found_redirect_to' => 'old_value'], 'old_value'];
|
||||
yield 'only new config' => [['invalid_short_url_redirect_to' => 'new_value'], 'new_value'];
|
||||
yield 'both configs, new first' => [
|
||||
['invalid_short_url_redirect_to' => 'new_value', 'not_found_redirect_to' => 'old_value'],
|
||||
'new_value',
|
||||
];
|
||||
yield 'both configs, deprecated first' => [
|
||||
['not_found_redirect_to' => 'old_value', 'invalid_short_url_redirect_to' => 'new_value'],
|
||||
'new_value',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user