From d7a3aeb0a26ae856c2c6b0a75660c4277f6c2003 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 13 Sep 2019 20:03:53 +0200 Subject: [PATCH] Created a config prost-processor which adds the base path on every applicable configuration --- config/autoload/router.global.php | 2 + config/config.php | 3 +- module/Core/src/Config/BasePathPrefixer.php | 39 +++++++++++++++++++ .../{ => Config}/SimplifiedConfigParser.php | 3 +- .../SimplifiedConfigParserTest.php | 9 ++++- 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 module/Core/src/Config/BasePathPrefixer.php rename module/Core/src/{ => Config}/SimplifiedConfigParser.php (96%) rename module/Core/test/{ => Config}/SimplifiedConfigParserTest.php (92%) diff --git a/config/autoload/router.global.php b/config/autoload/router.global.php index deb875f3..24f40e71 100644 --- a/config/autoload/router.global.php +++ b/config/autoload/router.global.php @@ -6,6 +6,8 @@ use Zend\Expressive\Router\FastRouteRouter; return [ 'router' => [ + 'base_path' => '', + 'fastroute' => [ FastRouteRouter::CONFIG_CACHE_ENABLED => true, FastRouteRouter::CONFIG_CACHE_FILE => 'data/cache/fastroute_cached_routes.php', diff --git a/config/config.php b/config/config.php index 8cc7c38a..3a44fb17 100644 --- a/config/config.php +++ b/config/config.php @@ -28,5 +28,6 @@ return (new ConfigAggregator\ConfigAggregator([ ? new ConfigAggregator\PhpFileProvider('config/test/*.global.php') : new ConfigAggregator\ZendConfigProvider('config/params/{generated_config.php,*.config.{php,json}}'), ], 'data/cache/app_config.php', [ - Core\SimplifiedConfigParser::class, + Core\Config\SimplifiedConfigParser::class, + Core\Config\BasePathPrefixer::class, ]))->getMergedConfig(); diff --git a/module/Core/src/Config/BasePathPrefixer.php b/module/Core/src/Config/BasePathPrefixer.php new file mode 100644 index 00000000..28d8c17e --- /dev/null +++ b/module/Core/src/Config/BasePathPrefixer.php @@ -0,0 +1,39 @@ +prefixRoutesWithBasePath($config, $basePath); + $config['middleware_pipeline'] = $this->prefixMiddlewarePathsWithBasePath($config, $basePath); + $config['url_shortener']['domain']['hostname'] .= $basePath; + + return $config; + } + + private function prefixRoutesWithBasePath(array $config, string $basePath): array + { + return map($config['routes'] ?? [], function (array $route) use ($basePath) { + $route['path'] = $basePath . $route['path']; + return $route; + }); + } + + private function prefixMiddlewarePathsWithBasePath(array $config, string $basePath): array + { + return map($config['middleware_pipeline'] ?? [], function (array $middleware) use ($basePath) { + if (! isset($middleware['path'])) { + return $middleware; + } + + $middleware['path'] = $basePath . $middleware['path']; + return $middleware; + }); + } +} diff --git a/module/Core/src/SimplifiedConfigParser.php b/module/Core/src/Config/SimplifiedConfigParser.php similarity index 96% rename from module/Core/src/SimplifiedConfigParser.php rename to module/Core/src/Config/SimplifiedConfigParser.php index 42c5d205..2fec5968 100644 --- a/module/Core/src/SimplifiedConfigParser.php +++ b/module/Core/src/Config/SimplifiedConfigParser.php @@ -1,7 +1,7 @@ ['entity_manager', 'connection'], 'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'], 'redis_servers' => ['redis', 'servers'], + 'base_path' => ['router', 'base_path'], ]; private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [ 'not_found_redirect_to' => [ diff --git a/module/Core/test/SimplifiedConfigParserTest.php b/module/Core/test/Config/SimplifiedConfigParserTest.php similarity index 92% rename from module/Core/test/SimplifiedConfigParserTest.php rename to module/Core/test/Config/SimplifiedConfigParserTest.php index 957c7a24..68361027 100644 --- a/module/Core/test/SimplifiedConfigParserTest.php +++ b/module/Core/test/Config/SimplifiedConfigParserTest.php @@ -1,10 +1,10 @@ 'bar', 'port' => '1234', ], + 'base_path' => '/foo/bar', ]; $expected = [ 'app_options' => [ @@ -96,6 +97,10 @@ class SimplifiedConfigParserTest extends TestCase 'tcp://1.2.2.2:2222', ], ], + + 'router' => [ + 'base_path' => '/foo/bar', + ], ]; $result = ($this->postProcessor)(array_merge($config, $simplified));