Skip to content

Commit

Permalink
initial work for DBAL 4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Oct 18, 2023
1 parent 5e55994 commit ec2338e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
28 changes: 16 additions & 12 deletions ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\MalformedDsnException;
use Doctrine\DBAL\Exception\InvalidWrapperClass;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use RuntimeException;

use function array_merge;
use function class_exists;
use function is_subclass_of;
use function trigger_deprecation;

Expand Down Expand Up @@ -92,6 +93,10 @@ public function createConnection(array $params, ?Configuration $config = null, ?

if (isset($params['wrapperClass'])) {
if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
if (class_exists(InvalidWrapperClass::class)) {
throw InvalidWrapperClass::new($params['wrapperClass']);
}

throw DBALException::invalidWrapperClass($params['wrapperClass']);
}

Expand All @@ -102,7 +107,7 @@ public function createConnection(array $params, ?Configuration $config = null, ?
$connection = DriverManager::getConnection($params, $config, $eventManager);
$params = $this->addDatabaseSuffix(array_merge($connection->getParams(), $overriddenOptions));
$driver = $connection->getDriver();
$platform = $driver->getDatabasePlatform();
$platform = $driver->getDatabasePlatform(new Connection\StaticServerVersionProvider(''));

Check failure on line 110 in ConnectionFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (7.4)

TooManyArguments

ConnectionFactory.php:110:36: TooManyArguments: Too many arguments for method Doctrine\DBAL\Driver::getdatabaseplatform - saw 1 (see https://psalm.dev/026)

Check failure on line 110 in ConnectionFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (7.4)

UndefinedClass

ConnectionFactory.php:110:60: UndefinedClass: Class, interface or enum named Doctrine\DBAL\Connection\StaticServerVersionProvider does not exist (see https://psalm.dev/019)

if (! isset($params['charset'])) {
if ($platform instanceof AbstractMySQLPlatform) {
Expand Down Expand Up @@ -162,7 +167,10 @@ private function getDatabasePlatform(Connection $connection): AbstractPlatform
try {
return $connection->getDatabasePlatform();
} catch (DriverException $driverException) {
throw new DBALException(
//TODO: what more specific exception class should we throw with DBAL 4?
$class = class_exists(DBALException::class) ? DBALException::class : RuntimeException::class;

throw new $class(
'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
'For further information have a look at:' . PHP_EOL .
Expand Down Expand Up @@ -226,19 +234,15 @@ private function addDatabaseSuffix(array $params): array
* URL extracted into individual parameter parts.
* @psalm-return Params
*
* @throws Exception
* @throws DBALException
*/
private function parseDatabaseUrl(array $params): array
{
if (! isset($params['url'])) {
return $params;
}

try {
$parsedParams = $this->dsnParser->parse($params['url']);
} catch (MalformedDsnException $e) {
throw new Exception('Malformed parameter "url".', 0, $e);
}
$parsedParams = $this->dsnParser->parse($params['url']);

if (isset($parsedParams['driver'])) {
// The requested driver from the URL scheme takes precedence
Expand All @@ -248,10 +252,10 @@ private function parseDatabaseUrl(array $params): array

$params = array_merge($params, $parsedParams);

// If a schemeless connection URL is given, we require a default driver or default custom driver
// If a schemaless connection URL is given, we require a default driver or default custom driver
// as connection parameter.
if (! isset($params['driverClass']) && ! isset($params['driver'])) {
throw Exception::driverRequired($params['url']);
throw DBALException\DriverRequired::new($params['url']);

Check failure on line 258 in ConnectionFactory.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (7.4)

UndefinedClass

ConnectionFactory.php:258:19: UndefinedClass: Class, interface or enum named Doctrine\DBAL\Exception\DriverRequired does not exist (see https://psalm.dev/019)
}

unset($params['url']);
Expand Down
6 changes: 6 additions & 0 deletions Tests/Dbal/Logging/BacktraceLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests\Dbal\Logging;

use Doctrine\Bundle\DoctrineBundle\Dbal\Logging\BacktraceLogger;
use Doctrine\DBAL\Logging\DebugStack;
use PHPUnit\Framework\TestCase;

use function class_exists;
use function current;

/** @group legacy */
class BacktraceLoggerTest extends TestCase
{
public function testBacktraceLogged(): void
{
if (! class_exists(DebugStack::class)) {
self::markTestSkipped('Needs DBAL < 4');
}

$logger = new BacktraceLogger();
$logger->startQuery('SELECT column FROM table');
$currentQuery = current($logger->queries);
Expand Down

0 comments on commit ec2338e

Please sign in to comment.