2019-04-18 11:37:38 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-04-18 11:37:38 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-09-13 21:03:53 +03:00
|
|
|
namespace Shlinkio\Shlink\Core\Config;
|
2019-04-18 11:37:38 +03:00
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Stdlib\ArrayUtils;
|
2020-03-14 21:24:21 +03:00
|
|
|
use Shlinkio\Shlink\Config\Collection\PathCollection;
|
2019-04-18 11:37:38 +03:00
|
|
|
|
2019-11-02 13:30:09 +03:00
|
|
|
use function array_flip;
|
2019-04-18 11:37:38 +03:00
|
|
|
use function array_intersect_key;
|
|
|
|
use function array_key_exists;
|
2019-11-02 13:30:09 +03:00
|
|
|
use function array_keys;
|
2019-04-18 11:37:38 +03:00
|
|
|
use function Functional\contains;
|
|
|
|
use function Functional\reduce_left;
|
2019-11-02 13:30:09 +03:00
|
|
|
use function uksort;
|
2019-04-18 11:37:38 +03:00
|
|
|
|
2019-08-07 19:45:28 +03:00
|
|
|
class SimplifiedConfigParser
|
2019-04-18 11:37:38 +03:00
|
|
|
{
|
|
|
|
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'],
|
2019-11-02 13:30:09 +03:00
|
|
|
'invalid_short_url_redirect_to' => ['not_found_redirects', 'invalid_short_url'],
|
2019-11-02 19:23:21 +03:00
|
|
|
'regular_404_redirect_to' => ['not_found_redirects', 'regular_404'],
|
2020-02-25 09:53:07 +03:00
|
|
|
'base_url_redirect_to' => ['not_found_redirects', 'base_url'],
|
2019-04-18 11:37:38 +03:00
|
|
|
'db_config' => ['entity_manager', 'connection'],
|
|
|
|
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
2020-01-05 18:45:14 +03:00
|
|
|
'redis_servers' => ['cache', 'redis', 'servers'],
|
2019-09-13 21:03:53 +03:00
|
|
|
'base_path' => ['router', 'base_path'],
|
2020-01-01 23:11:53 +03:00
|
|
|
'web_worker_num' => ['mezzio-swoole', 'swoole-http-server', 'options', 'worker_num'],
|
|
|
|
'task_worker_num' => ['mezzio-swoole', 'swoole-http-server', 'options', 'task_worker_num'],
|
2019-12-28 18:42:21 +03:00
|
|
|
'visits_webhooks' => ['url_shortener', 'visits_webhooks'],
|
2020-02-18 21:34:01 +03:00
|
|
|
'default_short_codes_length' => ['url_shortener', 'default_short_codes_length'],
|
2020-04-12 21:41:23 +03:00
|
|
|
'mercure_public_hub_url' => ['mercure', 'public_hub_url'],
|
|
|
|
'mercure_internal_hub_url' => ['mercure', 'internal_hub_url'],
|
|
|
|
'mercure_jwt_secret' => ['mercure', 'jwt_secret'],
|
2019-04-18 11:37:38 +03:00
|
|
|
];
|
2019-08-07 19:45:28 +03:00
|
|
|
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
|
|
|
|
'delete_short_url_threshold' => [
|
|
|
|
'path' => ['delete_short_urls', 'check_visits_threshold'],
|
|
|
|
'value' => true,
|
|
|
|
],
|
|
|
|
'redis_servers' => [
|
|
|
|
'path' => ['dependencies', 'aliases', 'lock_store'],
|
|
|
|
'value' => 'redis_lock_store',
|
|
|
|
],
|
2019-04-18 11:37:38 +03:00
|
|
|
];
|
|
|
|
private const SIMPLIFIED_MERGEABLE_CONFIG = ['db_config'];
|
|
|
|
|
|
|
|
public function __invoke(array $config): array
|
|
|
|
{
|
2019-11-02 13:30:09 +03:00
|
|
|
$configForExistingKeys = $this->getConfigForKeysInMappingOrderedByMapping($config);
|
2019-04-18 11:37:38 +03:00
|
|
|
|
2019-11-02 13:30:09 +03:00
|
|
|
return reduce_left($configForExistingKeys, function ($value, string $key, $c, PathCollection $collection) {
|
2019-04-18 11:37:38 +03:00
|
|
|
$path = self::SIMPLIFIED_CONFIG_MAPPING[$key];
|
|
|
|
if (contains(self::SIMPLIFIED_MERGEABLE_CONFIG, $key)) {
|
|
|
|
$value = ArrayUtils::merge($collection->getValueInPath($path), $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$collection->setValueInPath($value, $path);
|
2019-08-07 19:45:28 +03:00
|
|
|
if (array_key_exists($key, self::SIMPLIFIED_CONFIG_SIDE_EFFECTS)) {
|
|
|
|
['path' => $sideEffectPath, 'value' => $sideEffectValue] = self::SIMPLIFIED_CONFIG_SIDE_EFFECTS[$key];
|
|
|
|
$collection->setValueInPath($sideEffectValue, $sideEffectPath);
|
2019-04-18 11:37:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
|
|
|
}, new PathCollection($config))->toArray();
|
|
|
|
}
|
2019-11-02 13:30:09 +03:00
|
|
|
|
|
|
|
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));
|
2019-12-30 01:16:55 +03:00
|
|
|
uksort(
|
|
|
|
$configForExistingKeys,
|
2020-01-01 22:48:31 +03:00
|
|
|
fn (string $a, string $b): int => $simplifiedConfigOrder[$a] - $simplifiedConfigOrder[$b],
|
2019-12-30 01:16:55 +03:00
|
|
|
);
|
2019-11-02 13:30:09 +03:00
|
|
|
|
|
|
|
return $configForExistingKeys;
|
|
|
|
}
|
2019-04-18 11:37:38 +03:00
|
|
|
}
|