Created a config prost-processor which adds the base path on every applicable configuration

This commit is contained in:
Alejandro Celaya 2019-09-13 20:03:53 +02:00
parent 76541d5563
commit d7a3aeb0a2
5 changed files with 52 additions and 4 deletions

View file

@ -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',

View file

@ -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();

View file

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Config;
use function Functional\map;
class BasePathPrefixer
{
public function __invoke(array $config): array
{
$basePath = $config['router']['base_path'] ?? '';
$config['routes'] = $this->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;
});
}
}

View file

@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core;
namespace Shlinkio\Shlink\Core\Config;
use Shlinkio\Shlink\Installer\Util\PathCollection;
use Zend\Stdlib\ArrayUtils;
@ -22,6 +22,7 @@ class SimplifiedConfigParser
'db_config' => ['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' => [

View file

@ -1,10 +1,10 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core;
namespace ShlinkioTest\Shlink\Core\Config;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\SimplifiedConfigParser;
use Shlinkio\Shlink\Core\Config\SimplifiedConfigParser;
use function array_merge;
@ -50,6 +50,7 @@ class SimplifiedConfigParserTest extends TestCase
'password' => '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));