From 7a9f46e7e674ce838521a89d9a3885d735667df4 Mon Sep 17 00:00:00 2001 From: Damien Harper Date: Wed, 28 Aug 2024 10:10:28 +0200 Subject: [PATCH] Removed extracted code from `AbstractMySQLDriver` class --- .../Persistence/Helper/PlatformHelper.php | 73 +++++-------------- 1 file changed, 19 insertions(+), 54 deletions(-) diff --git a/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php b/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php index 86a23f9a..f2fc4d3e 100644 --- a/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php +++ b/src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php @@ -5,6 +5,7 @@ namespace DH\Auditor\Provider\Doctrine\Persistence\Helper; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\ServerInfoAwareConnection; abstract class PlatformHelper @@ -28,15 +29,21 @@ public static function isIndexLengthLimited(string $name, Connection $connection $version = self::getServerVersion($connection); if (null === $version) { + // Assume index length is not limited return false; } $mariadb = false !== mb_stripos($version, 'mariadb'); - if ($mariadb && version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.2', '<')) { - return true; - } - return !$mariadb && version_compare(self::getOracleMysqlVersionNumber($version), '5.7.7', '<'); + $reflectedClass = new \ReflectionClass(AbstractMySQLDriver::class); + $reflectedMethod = $reflectedClass->getMethod($mariadb ? 'getMariaDbMysqlVersionNumber' : 'getOracleMysqlVersionNumber'); + $reflectedMethod->setAccessible(true); + + /** @var string $normalizedVersion */ + $normalizedVersion = $reflectedMethod->invoke(null, $version); + $minVersion = $mariadb ? '10.2.2' : '5.7.7'; + + return version_compare($normalizedVersion, $minVersion, '<'); } public static function getServerVersion(Connection $connection): ?string @@ -54,61 +61,19 @@ public static function isJsonSupported(Connection $connection): bool { $version = self::getServerVersion($connection); if (null === $version) { + // Assume JSON is supported return true; } - $mariadb = false !== mb_stripos($version, 'mariadb'); + $reflectedClass = new \ReflectionClass(AbstractMySQLDriver::class); + $reflectedMethod = $reflectedClass->getMethod('getMariaDbMysqlVersionNumber'); + $reflectedMethod->setAccessible(true); + + /** @var string $normalizedVersion */ + $normalizedVersion = $reflectedMethod->invoke(null, $version); - return !($mariadb && version_compare(self::getMariaDbMysqlVersionNumber($version), '10.2.7', '<')); // JSON wasn't supported on MariaDB before 10.2.7 // @see https://mariadb.com/kb/en/json-data-type/ - - // Assume JSON is supported - } - - /** - * Get a normalized 'version number' from the server string - * returned by Oracle MySQL servers. - * - * @param string $versionString Version string returned by the driver, i.e. '5.7.10' - * - * @copyright Doctrine team - */ - public static function getOracleMysqlVersionNumber(string $versionString): string - { - preg_match( - '#^(?P\d+)(?:\.(?P\d+)(?:\.(?P\d+))?)?#', - $versionString, - $versionParts - ); - - $majorVersion = $versionParts['major']; - $minorVersion = $versionParts['minor'] ?? 0; - $patchVersion = $versionParts['patch'] ?? null; - - if ('5' === $majorVersion && '7' === $minorVersion && null === $patchVersion) { - $patchVersion = '9'; - } - - return $majorVersion.'.'.$minorVersion.'.'.$patchVersion; - } - - /** - * Detect MariaDB server version, including hack for some mariadb distributions - * that starts with the prefix '5.5.5-'. - * - * @param string $versionString Version string as returned by mariadb server, i.e. '5.5.5-Mariadb-10.0.8-xenial' - * - * @copyright Doctrine team - */ - public static function getMariaDbMysqlVersionNumber(string $versionString): string - { - preg_match( - '#^(?:5\.5\.5-)?(mariadb-)?(?P\d+)\.(?P\d+)\.(?P\d+)#i', - $versionString, - $versionParts - ); - - return $versionParts['major'].'.'.$versionParts['minor'].'.'.$versionParts['patch']; + return version_compare($normalizedVersion, '10.2.7', '>='); } }