1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Core\Config; |
6
|
|
|
|
7
|
|
|
use Shlinkio\Shlink\Installer\Util\PathCollection; |
8
|
|
|
use Zend\Stdlib\ArrayUtils; |
9
|
|
|
|
10
|
|
|
use function array_flip; |
11
|
|
|
use function array_intersect_key; |
12
|
|
|
use function array_key_exists; |
13
|
|
|
use function array_keys; |
14
|
|
|
use function Functional\contains; |
15
|
|
|
use function Functional\reduce_left; |
16
|
|
|
use function uksort; |
17
|
|
|
|
18
|
|
|
class SimplifiedConfigParser |
19
|
|
|
{ |
20
|
|
|
private const SIMPLIFIED_CONFIG_MAPPING = [ |
21
|
|
|
'disable_track_param' => ['app_options', 'disable_track_param'], |
22
|
|
|
'short_domain_schema' => ['url_shortener', 'domain', 'schema'], |
23
|
|
|
'short_domain_host' => ['url_shortener', 'domain', 'hostname'], |
24
|
|
|
'validate_url' => ['url_shortener', 'validate_url'], |
25
|
|
|
'not_found_redirect_to' => ['not_found_redirects', 'invalid_short_url'], // Deprecated |
26
|
|
|
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'], |
27
|
|
|
'regular_404_redirect_to' => ['not_found_redirects', 'regular_404'], |
28
|
|
|
'base_url_redirect_to' => ['not_found_redirects', 'base_path'], |
29
|
|
|
'db_config' => ['entity_manager', 'connection'], |
30
|
|
|
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'], |
31
|
|
|
'redis_servers' => ['redis', 'servers'], |
32
|
|
|
'base_path' => ['router', 'base_path'], |
33
|
|
|
]; |
34
|
|
|
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [ |
35
|
|
|
'delete_short_url_threshold' => [ |
36
|
|
|
'path' => ['delete_short_urls', 'check_visits_threshold'], |
37
|
|
|
'value' => true, |
38
|
|
|
], |
39
|
|
|
'redis_servers' => [ |
40
|
|
|
'path' => ['dependencies', 'aliases', 'lock_store'], |
41
|
|
|
'value' => 'redis_lock_store', |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config']; |
45
|
|
|
|
46
|
5 |
|
public function __invoke(array $config): array |
47
|
|
|
{ |
48
|
5 |
|
$configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config); |
49
|
|
|
|
50
|
|
|
return reduce_left($configForExistingKeys, function ($value, string $key, $c, PathCollection $collection) { |
51
|
5 |
|
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key]; |
52
|
5 |
|
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) { |
53
|
1 |
|
$value = ArrayUtils::merge($collection->getValueInPath($path), $value); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
5 |
|
$collection->setValueInPath($value, $path); |
57
|
5 |
|
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) { |
58
|
1 |
|
['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key]; |
59
|
1 |
|
$collection->setValueInPath($sideEffectValue, $sideEffectPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
return $collection; |
63
|
5 |
|
}, new PathCollection($config))->toArray(); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
private function getConfigForKeysInMappingOrderedByMapping(array $config): array |
67
|
|
|
{ |
68
|
|
|
// Ignore any config which is not defined in the mapping |
69
|
5 |
|
$configForExistingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING); |
70
|
|
|
|
71
|
|
|
// Order the config by their key, based on the order it was defined in the mapping. |
72
|
|
|
// This mainly allows deprecating keys and defining new ones that will replace the older and always take |
73
|
|
|
// preference, while the old one keeps working for backwards compatibility if the new one is not provided. |
74
|
5 |
|
$simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING)); |
75
|
|
|
uksort($configForExistingKeys, function (string $a, string $b) use ($simplifiedConfigOrder): int { |
76
|
3 |
|
return $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b]; |
77
|
5 |
|
}); |
78
|
|
|
|
79
|
5 |
|
return $configForExistingKeys; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|