Refactor ENGINE_MAP
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
This commit is contained in:
parent
60ddd910f7
commit
e5db99a32e
@ -1832,10 +1832,10 @@ final class Settings
|
||||
/**
|
||||
* You can select here which functions will be used for character set conversion.
|
||||
* Possible values are:
|
||||
* auto - automatically use available one (first is tested iconv, then mbstring)
|
||||
* iconv - use iconv or libiconv functions
|
||||
* mb - use mbstring extension
|
||||
* none - disable encoding conversion
|
||||
* auto - automatically use available one (first is tested iconv, then mbstring)
|
||||
* iconv - use iconv or libiconv functions
|
||||
* mbstring - use mbstring extension
|
||||
* none - disable encoding conversion
|
||||
*
|
||||
* ```php
|
||||
* $cfg['RecodingEngine'] = 'auto';
|
||||
@ -1843,7 +1843,7 @@ final class Settings
|
||||
*
|
||||
* @link https://docs.phpmyadmin.net/en/latest/config.html#cfg_RecodingEngine
|
||||
*
|
||||
* @psalm-var 'auto'|'iconv'|'mb'|'none'
|
||||
* @psalm-var 'auto'|'iconv'|'mbstring'|'none'
|
||||
*/
|
||||
public string $RecodingEngine;
|
||||
|
||||
@ -4656,7 +4656,7 @@ final class Settings
|
||||
/**
|
||||
* @param array<int|string, mixed> $settings
|
||||
*
|
||||
* @psalm-return 'auto'|'iconv'|'mb'|'none'
|
||||
* @psalm-return 'auto'|'iconv'|'mbstring'|'none'
|
||||
*/
|
||||
private function setRecodingEngine(array $settings): string
|
||||
{
|
||||
@ -4667,6 +4667,10 @@ final class Settings
|
||||
return 'auto';
|
||||
}
|
||||
|
||||
if ($settings['RecodingEngine'] === 'mb') {
|
||||
return 'mbstring';
|
||||
}
|
||||
|
||||
return $settings['RecodingEngine'];
|
||||
}
|
||||
|
||||
|
||||
@ -32,27 +32,16 @@ use function unlink;
|
||||
*/
|
||||
class Encoding
|
||||
{
|
||||
/**
|
||||
* None encoding conversion engine
|
||||
*/
|
||||
public const ENGINE_NONE = 0;
|
||||
|
||||
/**
|
||||
* iconv encoding conversion engine
|
||||
*/
|
||||
public const ENGINE_ICONV = 1;
|
||||
|
||||
/**
|
||||
* mbstring encoding conversion engine
|
||||
*/
|
||||
public const ENGINE_MB = 3;
|
||||
public const ENGINE_NONE = 'none';
|
||||
public const ENGINE_ICONV = 'iconv';
|
||||
public const ENGINE_MBSTRING = 'mbstring';
|
||||
|
||||
/**
|
||||
* Chosen encoding engine
|
||||
*
|
||||
* @var self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MB|null
|
||||
* @var self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MBSTRING|null
|
||||
*/
|
||||
private static int|null $engine = null;
|
||||
private static string|null $engine = null;
|
||||
|
||||
/**
|
||||
* Map of conversion engine configurations
|
||||
@ -64,15 +53,15 @@ class Encoding
|
||||
* - extension name to warn when missing
|
||||
*/
|
||||
private const ENGINE_MAP = [
|
||||
'iconv' => ['iconv', self::ENGINE_ICONV, 'iconv'],
|
||||
'mb' => ['mb_convert_encoding', self::ENGINE_MB, 'mbstring'],
|
||||
'none' => ['isset', self::ENGINE_NONE, ''],
|
||||
'iconv' => 'iconv',
|
||||
'mbstring' => 'mb_convert_encoding',
|
||||
'none' => 'isset',
|
||||
];
|
||||
|
||||
/**
|
||||
* Order of automatic detection of engines
|
||||
*/
|
||||
private const ENGINE_ORDER = ['iconv', 'mb'];
|
||||
private const ENGINE_ORDER = ['iconv', 'mbstring'];
|
||||
|
||||
/**
|
||||
* Kanji encodings list
|
||||
@ -88,19 +77,19 @@ class Encoding
|
||||
|
||||
/* Use user configuration */
|
||||
if (isset(self::ENGINE_MAP[$engine])) {
|
||||
if (function_exists(self::ENGINE_MAP[$engine][0])) {
|
||||
self::$engine = self::ENGINE_MAP[$engine][1];
|
||||
if (function_exists(self::ENGINE_MAP[$engine])) {
|
||||
self::$engine = $engine;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Core::warnMissingExtension(self::ENGINE_MAP[$engine][2]);
|
||||
Core::warnMissingExtension($engine);
|
||||
}
|
||||
|
||||
/* Autodetection */
|
||||
foreach (self::ENGINE_ORDER as $engine) {
|
||||
if (function_exists(self::ENGINE_MAP[$engine][0])) {
|
||||
self::$engine = self::ENGINE_MAP[$engine][1];
|
||||
if (function_exists(self::ENGINE_MAP[$engine])) {
|
||||
self::$engine = $engine;
|
||||
|
||||
return;
|
||||
}
|
||||
@ -113,9 +102,9 @@ class Encoding
|
||||
/**
|
||||
* Setter for engine. Use with caution, mostly useful for testing.
|
||||
*
|
||||
* @param self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MB $engine
|
||||
* @param self::ENGINE_NONE|self::ENGINE_ICONV|self::ENGINE_MBSTRING $engine
|
||||
*/
|
||||
public static function setEngine(int $engine): void
|
||||
public static function setEngine(string $engine): void
|
||||
{
|
||||
self::$engine = $engine;
|
||||
}
|
||||
@ -163,7 +152,7 @@ class Encoding
|
||||
|
||||
return match (self::$engine) {
|
||||
self::ENGINE_ICONV => iconv($srcCharset, $destCharset . $iconvExtraParams, $what),
|
||||
self::ENGINE_MB => mb_convert_encoding($what, $destCharset, $srcCharset),
|
||||
self::ENGINE_MBSTRING => mb_convert_encoding($what, $destCharset, $srcCharset),
|
||||
default => $what,
|
||||
};
|
||||
}
|
||||
@ -301,7 +290,7 @@ class Encoding
|
||||
/**
|
||||
* Lists available encodings.
|
||||
*
|
||||
* @return mixed[]
|
||||
* @return string[]
|
||||
*/
|
||||
public static function listEncodings(): array
|
||||
{
|
||||
@ -311,7 +300,7 @@ class Encoding
|
||||
|
||||
/* Most engines do not support listing */
|
||||
$config = Config::getInstance();
|
||||
if (self::$engine != self::ENGINE_MB) {
|
||||
if (self::$engine != self::ENGINE_MBSTRING) {
|
||||
return array_filter($config->settings['AvailableCharsets'], static function (string $charset): bool {
|
||||
// Removes any ignored character
|
||||
$normalizedCharset = strtoupper((string) preg_replace(['/[^A-Za-z0-9\-\/]/'], '', $charset));
|
||||
|
||||
@ -707,7 +707,7 @@ class SettingsTest extends TestCase
|
||||
['DefaultTabServer', 'status', '/server/status'],
|
||||
['DefaultTabDatabase', 'search', '/database/search'],
|
||||
['DefaultTabTable', 'search', '/table/search'],
|
||||
['RecodingEngine', 'mb', 'mb'],
|
||||
['RecodingEngine', 'mb', 'mbstring'],
|
||||
['RowActionLinks', 'both', 'both'],
|
||||
],
|
||||
],
|
||||
|
||||
@ -109,7 +109,7 @@ class EncodingTest extends AbstractTestCase
|
||||
|
||||
public function testMbstring(): void
|
||||
{
|
||||
Encoding::setEngine(Encoding::ENGINE_MB);
|
||||
Encoding::setEngine(Encoding::ENGINE_MBSTRING);
|
||||
self::assertSame(
|
||||
"This is the Euro symbol '?'.",
|
||||
Encoding::convertString(
|
||||
|
||||
Loading…
Reference in New Issue
Block a user