Skip to content

Commit 539504b

Browse files
committed
Connection: update factory
1 parent 9996070 commit 539504b

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/ConnectionFactory.php

+39-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace Nettrine\DBAL;
44

5-
use Doctrine\Common\EventManager;
65
use Doctrine\DBAL\Configuration;
76
use Doctrine\DBAL\Connection;
7+
use Doctrine\DBAL\ConnectionException;
88
use Doctrine\DBAL\DriverManager;
9+
use Doctrine\DBAL\Exception\DriverException;
10+
use Doctrine\DBAL\Platforms\AbstractPlatform;
911
use Doctrine\DBAL\Types\Type;
1012

13+
/**
14+
* @see https://github.com/doctrine/DoctrineBundle
15+
* @phpstan-import-type Params from DriverManager
16+
*/
1117
class ConnectionFactory
1218
{
1319

@@ -30,25 +36,52 @@ public function __construct(array $typesConfig = [], array $typesMapping = [])
3036
}
3137

3238
/**
33-
* @param mixed[] $params
39+
* @phpstan-param Params $params
40+
* @param array<string, string> $typesMapping
3441
*/
35-
public function createConnection(array $params, ?Configuration $config = null, ?EventManager $em = null): Connection
42+
public function createConnection(
43+
array $params,
44+
?Configuration $config = null,
45+
array $typesMapping = []
46+
): Connection
3647
{
3748
if (!$this->initialized) {
3849
$this->initializeTypes();
3950
}
4051

41-
/** @phpstan-ignore-next-line */
42-
$connection = DriverManager::getConnection($params, $config, $em);
43-
$platform = $connection->getDatabasePlatform();
52+
$config ??= new Configuration();
53+
$connection = DriverManager::getConnection($params, $config);
54+
$platform = $this->getDatabasePlatform($connection);
4455

56+
// Register types mapping (global)
4557
foreach ($this->typesMapping as $dbType => $doctrineType) {
4658
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
4759
}
4860

61+
// Register types mapping (local)
62+
foreach ($typesMapping as $dbType => $doctrineType) {
63+
$platform->registerDoctrineTypeMapping($dbType, $doctrineType);
64+
}
65+
4966
return $connection;
5067
}
5168

69+
private function getDatabasePlatform(Connection $connection): AbstractPlatform
70+
{
71+
try {
72+
return $connection->getDatabasePlatform();
73+
} catch (DriverException $driverException) {
74+
throw new ConnectionException(
75+
'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL .
76+
"You can circumvent this by setting a 'serverVersion' configuration value" . PHP_EOL . PHP_EOL .
77+
'For further information have a look at:' . PHP_EOL .
78+
'https://github.com/doctrine/DoctrineBundle/issues/673',
79+
0,
80+
$driverException,
81+
);
82+
}
83+
}
84+
5285
private function initializeTypes(): void
5386
{
5487
foreach ($this->typesConfig as $type => $class) {

0 commit comments

Comments
 (0)