mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Created a config prost-processor which adds the base path on every applicable configuration
This commit is contained in:
parent
76541d5563
commit
d7a3aeb0a2
@ -6,6 +6,8 @@ use Zend\Expressive\Router\FastRouteRouter;
|
|||||||
return [
|
return [
|
||||||
|
|
||||||
'router' => [
|
'router' => [
|
||||||
|
'base_path' => '',
|
||||||
|
|
||||||
'fastroute' => [
|
'fastroute' => [
|
||||||
FastRouteRouter::CONFIG_CACHE_ENABLED => true,
|
FastRouteRouter::CONFIG_CACHE_ENABLED => true,
|
||||||
FastRouteRouter::CONFIG_CACHE_FILE => 'data/cache/fastroute_cached_routes.php',
|
FastRouteRouter::CONFIG_CACHE_FILE => 'data/cache/fastroute_cached_routes.php',
|
||||||
|
@ -28,5 +28,6 @@ return (new ConfigAggregator\ConfigAggregator([
|
|||||||
? new ConfigAggregator\PhpFileProvider('config/test/*.global.php')
|
? new ConfigAggregator\PhpFileProvider('config/test/*.global.php')
|
||||||
: new ConfigAggregator\ZendConfigProvider('config/params/{generated_config.php,*.config.{php,json}}'),
|
: new ConfigAggregator\ZendConfigProvider('config/params/{generated_config.php,*.config.{php,json}}'),
|
||||||
], 'data/cache/app_config.php', [
|
], 'data/cache/app_config.php', [
|
||||||
Core\SimplifiedConfigParser::class,
|
Core\Config\SimplifiedConfigParser::class,
|
||||||
|
Core\Config\BasePathPrefixer::class,
|
||||||
]))->getMergedConfig();
|
]))->getMergedConfig();
|
||||||
|
39
module/Core/src/Config/BasePathPrefixer.php
Normal file
39
module/Core/src/Config/BasePathPrefixer.php
Normal 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core;
|
namespace Shlinkio\Shlink\Core\Config;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
||||||
use Zend\Stdlib\ArrayUtils;
|
use Zend\Stdlib\ArrayUtils;
|
||||||
@ -22,6 +22,7 @@ class SimplifiedConfigParser
|
|||||||
'db_config' => ['entity_manager', 'connection'],
|
'db_config' => ['entity_manager', 'connection'],
|
||||||
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
'delete_short_url_threshold' => ['delete_short_urls', 'visits_threshold'],
|
||||||
'redis_servers' => ['redis', 'servers'],
|
'redis_servers' => ['redis', 'servers'],
|
||||||
|
'base_path' => ['router', 'base_path'],
|
||||||
];
|
];
|
||||||
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
|
private const SIMPLIFIED_CONFIG_SIDE_EFFECTS = [
|
||||||
'not_found_redirect_to' => [
|
'not_found_redirect_to' => [
|
@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Core;
|
namespace ShlinkioTest\Shlink\Core\Config;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Shlinkio\Shlink\Core\SimplifiedConfigParser;
|
use Shlinkio\Shlink\Core\Config\SimplifiedConfigParser;
|
||||||
|
|
||||||
use function array_merge;
|
use function array_merge;
|
||||||
|
|
||||||
@ -50,6 +50,7 @@ class SimplifiedConfigParserTest extends TestCase
|
|||||||
'password' => 'bar',
|
'password' => 'bar',
|
||||||
'port' => '1234',
|
'port' => '1234',
|
||||||
],
|
],
|
||||||
|
'base_path' => '/foo/bar',
|
||||||
];
|
];
|
||||||
$expected = [
|
$expected = [
|
||||||
'app_options' => [
|
'app_options' => [
|
||||||
@ -96,6 +97,10 @@ class SimplifiedConfigParserTest extends TestCase
|
|||||||
'tcp://1.2.2.2:2222',
|
'tcp://1.2.2.2:2222',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'router' => [
|
||||||
|
'base_path' => '/foo/bar',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
$result = ($this->postProcessor)(array_merge($config, $simplified));
|
$result = ($this->postProcessor)(array_merge($config, $simplified));
|
Loading…
Reference in New Issue
Block a user