Skip to content

Commit

Permalink
Removed extracted code from AbstractMySQLDriver class
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Aug 28, 2024
1 parent ec4fb4e commit 7a9f46e
Showing 1 changed file with 19 additions and 54 deletions.
73 changes: 19 additions & 54 deletions src/Provider/Doctrine/Persistence/Helper/PlatformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\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<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)#i',
$versionString,
$versionParts
);

return $versionParts['major'].'.'.$versionParts['minor'].'.'.$versionParts['patch'];
return version_compare($normalizedVersion, '10.2.7', '>=');
}
}

0 comments on commit 7a9f46e

Please sign in to comment.