mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 20:11:34 +03:00
Fixed non-existing keys not being set with default values in imported config
This commit is contained in:
parent
ff80f32f72
commit
fa595f7aa3
4 changed files with 100 additions and 33 deletions
|
@ -4,8 +4,11 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\Common;
|
namespace Shlinkio\Shlink\Common;
|
||||||
|
|
||||||
use const JSON_ERROR_NONE;
|
use const JSON_ERROR_NONE;
|
||||||
|
use function array_key_exists;
|
||||||
|
use function array_shift;
|
||||||
use function getenv;
|
use function getenv;
|
||||||
use function in_array;
|
use function in_array;
|
||||||
|
use function is_array;
|
||||||
use function json_last_error;
|
use function json_last_error;
|
||||||
use function json_last_error_msg;
|
use function json_last_error_msg;
|
||||||
use function strtolower;
|
use function strtolower;
|
||||||
|
@ -59,3 +62,39 @@ function json_decode(string $json, int $depth = 512, int $options = 0): array
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function array_path_exists(array $path, array $array): bool
|
||||||
|
{
|
||||||
|
// As soon as a step is not found, the path does not exist
|
||||||
|
$step = array_shift($path);
|
||||||
|
if (! array_key_exists($step, $array)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Once the path is empty, we have found all the parts in the path
|
||||||
|
if (empty($path)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If current value is not an array, then we have not found the path
|
||||||
|
$newArray = $array[$step];
|
||||||
|
if (! is_array($newArray)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_path_exists($path, $newArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
function array_get_path(array $path, array $array)
|
||||||
|
{
|
||||||
|
do {
|
||||||
|
$step = array_shift($path);
|
||||||
|
if (! is_array($array) || ! array_key_exists($step, $array)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$array = $array[$step];
|
||||||
|
} while (! empty($path));
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\Installer\Model;
|
namespace Shlinkio\Shlink\Installer\Model;
|
||||||
|
|
||||||
use Zend\Stdlib\ArraySerializableInterface;
|
use Zend\Stdlib\ArraySerializableInterface;
|
||||||
|
use function Shlinkio\Shlink\Common\array_get_path;
|
||||||
|
use function Shlinkio\Shlink\Common\array_path_exists;
|
||||||
|
|
||||||
final class CustomizableAppConfig implements ArraySerializableInterface
|
final class CustomizableAppConfig implements ArraySerializableInterface
|
||||||
{
|
{
|
||||||
|
@ -112,43 +114,43 @@ final class CustomizableAppConfig implements ArraySerializableInterface
|
||||||
|
|
||||||
public function exchangeArray(array $array): void
|
public function exchangeArray(array $array): void
|
||||||
{
|
{
|
||||||
$this->setApp([
|
$this->setApp($this->mapExistingPathsToKeys([
|
||||||
'SECRET' => $array['app_options']['secret_key'] ?? null,
|
'SECRET' => ['app_options', 'secret_key'],
|
||||||
'DISABLE_TRACK_PARAM' => $array['app_options']['disable_track_param'] ?? null,
|
'DISABLE_TRACK_PARAM' => ['app_options', 'disable_track_param'],
|
||||||
]);
|
], $array));
|
||||||
|
|
||||||
$this->setDatabase($this->deserializeDatabase($array['entity_manager']['connection'] ?? []));
|
$this->setDatabase($this->mapExistingPathsToKeys([
|
||||||
|
'DRIVER' => ['entity_manager', 'connection', 'driver'],
|
||||||
|
'USER' => ['entity_manager', 'connection', 'user'],
|
||||||
|
'PASSWORD' => ['entity_manager', 'connection', 'password'],
|
||||||
|
'NAME' => ['entity_manager', 'connection', 'dbname'],
|
||||||
|
'HOST' => ['entity_manager', 'connection', 'host'],
|
||||||
|
'PORT' => ['entity_manager', 'connection', 'port'],
|
||||||
|
], $array));
|
||||||
|
|
||||||
$this->setLanguage([
|
$this->setLanguage($this->mapExistingPathsToKeys([
|
||||||
'DEFAULT' => $array['translator']['locale'] ?? null,
|
'DEFAULT' => ['translator', 'locale'],
|
||||||
'CLI' => $array['cli']['locale'] ?? null,
|
'CLI' => ['cli', 'locale'],
|
||||||
]);
|
], $array));
|
||||||
|
|
||||||
$this->setUrlShortener([
|
$this->setUrlShortener($this->mapExistingPathsToKeys([
|
||||||
'SCHEMA' => $array['url_shortener']['domain']['schema'] ?? null,
|
'SCHEMA' => ['url_shortener', 'domain', 'schema'],
|
||||||
'HOSTNAME' => $array['url_shortener']['domain']['hostname'] ?? null,
|
'HOSTNAME' => ['url_shortener', 'domain', 'hostname'],
|
||||||
'CHARS' => $array['url_shortener']['shortcode_chars'] ?? null,
|
'CHARS' => ['url_shortener', 'shortcode_chars'],
|
||||||
'VALIDATE_URL' => $array['url_shortener']['validate_url'] ?? true,
|
'VALIDATE_URL' => ['url_shortener', 'validate_url'],
|
||||||
]);
|
], $array));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function deserializeDatabase(array $conn): array
|
private function mapExistingPathsToKeys(array $map, array $config): array
|
||||||
{
|
{
|
||||||
if (! isset($conn['driver'])) {
|
$result = [];
|
||||||
return [];
|
foreach ($map as $key => $path) {
|
||||||
}
|
if (array_path_exists($path, $config)) {
|
||||||
$driver = $conn['driver'];
|
$result[$key] = array_get_path($path, $config);
|
||||||
|
}
|
||||||
$params = ['DRIVER' => $driver];
|
|
||||||
if ($driver !== 'pdo_sqlite') {
|
|
||||||
$params['USER'] = $conn['user'] ?? null;
|
|
||||||
$params['PASSWORD'] = $conn['password'] ?? null;
|
|
||||||
$params['NAME'] = $conn['dbname'] ?? null;
|
|
||||||
$params['HOST'] = $conn['host'] ?? null;
|
|
||||||
$params['PORT'] = $conn['port'] ?? null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $params;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getArrayCopy(): array
|
public function getArrayCopy(): array
|
||||||
|
|
29
module/Installer/test/ConfigProviderTest.php
Normal file
29
module/Installer/test/ConfigProviderTest.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioTest\Shlink\Installer;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Shlinkio\Shlink\Installer\ConfigProvider;
|
||||||
|
|
||||||
|
class ConfigProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ConfigProvider
|
||||||
|
*/
|
||||||
|
protected $configProvider;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->configProvider = new ConfigProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function configIsReturned()
|
||||||
|
{
|
||||||
|
$config = $this->configProvider->__invoke();
|
||||||
|
$this->assertEmpty($config);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,10 +24,7 @@
|
||||||
|
|
||||||
<filter>
|
<filter>
|
||||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||||
<directory suffix=".php">./module/Common/src</directory>
|
<directory suffix=".php">./module/*/src</directory>
|
||||||
<directory suffix=".php">./module/Core/src</directory>
|
|
||||||
<directory suffix=".php">./module/Rest/src</directory>
|
|
||||||
<directory suffix=".php">./module/CLI/src</directory>
|
|
||||||
|
|
||||||
<exclude>
|
<exclude>
|
||||||
<directory suffix=".php">./module/Core/src/Repository</directory>
|
<directory suffix=".php">./module/Core/src/Repository</directory>
|
||||||
|
|
Loading…
Add table
Reference in a new issue