diff --git a/config/config.php b/config/config.php index 2f7839ca..fcee89fb 100644 --- a/config/config.php +++ b/config/config.php @@ -25,4 +25,6 @@ return (new ConfigAggregator\ConfigAggregator([ env('APP_ENV') === 'test' ? new ConfigAggregator\PhpFileProvider('config/test/*.global.php') : new ConfigAggregator\ArrayProvider([]), -], 'data/cache/app_config.php'))->getMergedConfig(); +], 'data/cache/app_config.php', [ + Core\ConfigPostProcessor::class, +]))->getMergedConfig(); diff --git a/module/Core/src/ConfigPostProcessor.php b/module/Core/src/ConfigPostProcessor.php new file mode 100644 index 00000000..c9fb9e8f --- /dev/null +++ b/module/Core/src/ConfigPostProcessor.php @@ -0,0 +1,50 @@ + ['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'], + 'locale' => ['translator', 'locale'], + ]; + private const SIMPLIFIED_CONFIG_TOGGLES = [ + 'not_found_redirect_to' => ['url_shortener', 'not_found_short_url', 'enable_redirection'], + 'delete_short_url_threshold' => ['delete_short_urls', 'check_visits_threshold'], + ]; + 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_TOGGLES)) { + $collection->setValueInPath(true, self::SIMPLIFIED_CONFIG_TOGGLES[$key]); + } + + return $collection; + }, new PathCollection($config))->toArray(); + } +} diff --git a/module/Core/test/ConfigPostProcessorTest.php b/module/Core/test/ConfigPostProcessorTest.php new file mode 100644 index 00000000..2a676807 --- /dev/null +++ b/module/Core/test/ConfigPostProcessorTest.php @@ -0,0 +1,93 @@ +postProcessor = new ConfigPostProcessor(); + } + + /** @test */ + public function properlyMapsSimplifiedConfig(): void + { + $config = [ + 'app_options' => [ + 'disable_track_param' => 'foo', + ], + + 'entity_manager' => [ + 'connection' => [ + 'driver' => 'mysql', + 'host' => 'shlink_db', + 'port' => '3306', + ], + ], + ]; + $simplified = [ + 'disable_track_param' => 'bar', + 'short_domain_schema' => 'https', + 'short_domain_host' => 'doma.in', + 'validate_url' => false, + 'delete_short_url_threshold' => 50, + 'locale' => 'es', + 'not_found_redirect_to' => 'foobar.com', + 'db_config' => [ + 'dbname' => 'shlink', + 'user' => 'foo', + 'password' => 'bar', + 'port' => '1234', + ], + ]; + $expected = [ + 'app_options' => [ + 'disable_track_param' => 'bar', + ], + + 'entity_manager' => [ + 'connection' => [ + 'driver' => 'mysql', + 'host' => 'shlink_db', + 'dbname' => 'shlink', + 'user' => 'foo', + 'password' => 'bar', + 'port' => '1234', + ], + ], + + 'url_shortener' => [ + 'domain' => [ + 'schema' => 'https', + 'hostname' => 'doma.in', + ], + 'validate_url' => false, + 'not_found_short_url' => [ + 'redirect_to' => 'foobar.com', + 'enable_redirection' => true, + ], + ], + + 'translator' => [ + 'locale' => 'es', + ], + + 'delete_short_urls' => [ + 'visits_threshold' => 50, + 'check_visits_threshold' => true, + ], + ]; + + $result = ($this->postProcessor)(array_merge($config, $simplified)); + + $this->assertEquals(array_merge($expected, $simplified), $result); + } +}