|
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
|
|
|
'web_worker_num' => ['zend-expressive-swoole', 'swoole-http-server', 'options', 'worker_num'], |
|
34
|
|
|
'task_worker_num' => ['zend-expressive-swoole', 'swoole-http-server', 'options', 'task_worker_num'], |
|
35
|
|
|
'visits_webhooks' => ['url_shortener', 'visits_webhooks'], |
|
36
|
|
|
]; |
|
37
|
|
|
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [ |
|
38
|
|
|
'delete_short_url_threshold' => [ |
|
39
|
|
|
'path' => ['delete_short_urls', 'check_visits_threshold'], |
|
40
|
|
|
'value' => true, |
|
41
|
|
|
], |
|
42
|
|
|
'redis_servers' => [ |
|
43
|
|
|
'path' => ['dependencies', 'aliases', 'lock_store'], |
|
44
|
|
|
'value' => 'redis_lock_store', |
|
45
|
|
|
], |
|
46
|
|
|
]; |
|
47
|
|
|
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config']; |
|
48
|
|
|
|
|
49
|
5 |
|
public function __invoke(array $config): array |
|
50
|
|
|
{ |
|
51
|
5 |
|
$configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config); |
|
52
|
|
|
|
|
53
|
|
|
return reduce_left($configForExistingKeys, function ($value, string $key, $c, PathCollection $collection) { |
|
54
|
5 |
|
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key]; |
|
55
|
5 |
|
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) { |
|
56
|
1 |
|
$value = ArrayUtils::merge($collection->getValueInPath($path), $value); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
5 |
|
$collection->setValueInPath($value, $path); |
|
60
|
5 |
|
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) { |
|
61
|
1 |
|
['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key]; |
|
62
|
1 |
|
$collection->setValueInPath($sideEffectValue, $sideEffectPath); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
return $collection; |
|
66
|
5 |
|
}, new PathCollection($config))->toArray(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
private function getConfigForKeysInMappingOrderedByMapping(array $config): array |
|
70
|
|
|
{ |
|
71
|
|
|
// Ignore any config which is not defined in the mapping |
|
72
|
5 |
|
$configForExistingKeys = array_intersect_key($config, self::SIMPLIFIED_CONFIG_MAPPING); |
|
73
|
|
|
|
|
74
|
|
|
// Order the config by their key, based on the order it was defined in the mapping. |
|
75
|
|
|
// This mainly allows deprecating keys and defining new ones that will replace the older and always take |
|
76
|
|
|
// preference, while the old one keeps working for backwards compatibility if the new one is not provided. |
|
77
|
5 |
|
$simplifiedConfigOrder = array_flip(array_keys(self::SIMPLIFIED_CONFIG_MAPPING)); |
|
78
|
5 |
|
uksort( |
|
79
|
5 |
|
$configForExistingKeys, |
|
80
|
5 |
|
fn (string $a, string $b): int => $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b] |
|
|
|
|
|
|
81
|
5 |
|
); |
|
82
|
|
|
|
|
83
|
5 |
|
return $configForExistingKeys; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|