From 479760c0eecd440148cc17e4625bcd6d860d289a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 Apr 2019 10:37:38 +0200 Subject: [PATCH 1/2] Created config post processor that parses a simplified config to what shlink expects --- config/config.php | 4 +- module/Core/src/ConfigPostProcessor.php | 50 +++++++++++ module/Core/test/ConfigPostProcessorTest.php | 93 ++++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 module/Core/src/ConfigPostProcessor.php create mode 100644 module/Core/test/ConfigPostProcessorTest.php 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); + } +} From c993bbd9937bcf8b5f37b7cb5564502e6803bab1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 18 Apr 2019 10:47:26 +0200 Subject: [PATCH 2/2] Updated changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91442b4e..1f10d1fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this It also removes the need of asynchronously and programmatically updating the file, which deprecates the `visit:update-db` command. +* [#373](https://github.com/shlinkio/shlink/issues/373) Added support for a simplified config. Specially useful to use with the docker container. + #### Changed * [#56](https://github.com/shlinkio/shlink/issues/56) Simplified supported cache, requiring APCu always.