mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-27 17:31:20 -06:00
Created config post processor that parses a simplified config to what shlink expects
This commit is contained in:
parent
e186237410
commit
479760c0ee
@ -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();
|
||||
|
50
module/Core/src/ConfigPostProcessor.php
Normal file
50
module/Core/src/ConfigPostProcessor.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core;
|
||||
|
||||
use Shlinkio\Shlink\Installer\Util\PathCollection;
|
||||
use Zend\Stdlib\ArrayUtils;
|
||||
|
||||
use function array_intersect_key;
|
||||
use function array_key_exists;
|
||||
use function Functional\contains;
|
||||
use function Functional\reduce_left;
|
||||
|
||||
class ConfigPostProcessor
|
||||
{
|
||||
private const SIMPLIFIED_CONFIG_MAPPING = [
|
||||
'disable_track_param' => ['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();
|
||||
}
|
||||
}
|
93
module/Core/test/ConfigPostProcessorTest.php
Normal file
93
module/Core/test/ConfigPostProcessorTest.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\ConfigPostProcessor;
|
||||
|
||||
use function array_merge;
|
||||
|
||||
class ConfigPostProcessorTest extends TestCase
|
||||
{
|
||||
private $postProcessor;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user