|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Service\Connection; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface; |
|
8
|
|
|
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException; |
|
9
|
|
|
use Doctrine\Common\EventManager; |
|
10
|
|
|
use Doctrine\DBAL\Configuration; |
|
11
|
|
|
use Doctrine\DBAL\Connection; |
|
12
|
|
|
use Doctrine\DBAL\DriverManager; |
|
13
|
|
|
use Doctrine\DBAL\Exception; |
|
14
|
|
|
|
|
15
|
|
|
final class ConnectionFactory implements ConnectionFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Closure |
|
19
|
|
|
*/ |
|
20
|
|
|
private \Closure $factoryWrapper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array<mixed> |
|
24
|
|
|
*/ |
|
25
|
|
|
private array $defaultConfig; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array<mixed> $defaultConfig |
|
29
|
|
|
* |
|
30
|
|
|
* @noinspection ProperNullCoalescingOperatorUsageInspection |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( |
|
33
|
|
|
private readonly ConfigurationManagerInterface $configurationManager, |
|
34
|
|
|
?callable $factory = null, |
|
35
|
|
|
array $defaultConfig = [] |
|
36
|
|
|
) { |
|
37
|
|
|
$this->factoryWrapper = ($factory ?? [$this, 'doCreate'])(...); |
|
|
|
|
|
|
38
|
|
|
$this->defaultConfig = $defaultConfig; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array<mixed> $config |
|
43
|
|
|
* @param Configuration|string|null $configuration |
|
44
|
|
|
* |
|
45
|
|
|
* @throws ConnectionFactoryException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create(array $config, $configuration = null, ?EventManager $eventManager = null): Connection |
|
48
|
|
|
{ |
|
49
|
|
|
$config = array_replace_recursive($this->defaultConfig, $config); |
|
50
|
|
|
|
|
51
|
|
|
try { |
|
52
|
|
|
if (is_string($configuration)) { |
|
53
|
|
|
$configuration = $this->configurationManager->getConfiguration($configuration); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return call_user_func($this->factoryWrapper, $config, $configuration, $eventManager); |
|
57
|
|
|
} catch (\Exception $e) { |
|
58
|
|
|
throw new ConnectionFactoryException( |
|
59
|
|
|
sprintf('Failed to create new connection: %s', $e->getMessage()), |
|
60
|
|
|
$e->getCode(), |
|
61
|
|
|
$e |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array<mixed> $config |
|
68
|
|
|
* |
|
69
|
|
|
* @throws Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
private function doCreate( |
|
72
|
|
|
array $config, |
|
73
|
|
|
?Configuration $configuration, |
|
74
|
|
|
?EventManager $eventManager = null |
|
75
|
|
|
): Connection { |
|
76
|
|
|
$connection = DriverManager::getConnection($config, $configuration, $eventManager); |
|
77
|
|
|
|
|
78
|
|
|
if (!empty($config['doctrine_type_mappings'])) { |
|
79
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
80
|
|
|
foreach ($config['doctrine_type_mappings'] as $databaseType => $doctrineType) { |
|
81
|
|
|
/** @noinspection NullPointerExceptionInspection */ |
|
82
|
|
|
$platform->registerDoctrineTypeMapping($databaseType, $doctrineType); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $connection; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|