|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Service\Connection; |
|
6
|
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager; |
|
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\Driver\PDO\MySQL\Driver; |
|
13
|
|
|
use Doctrine\DBAL\DriverManager; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Alex Patterson <[email protected]> |
|
17
|
|
|
* @package Arp\LaminasDoctrine\Service\Connection |
|
18
|
|
|
*/ |
|
19
|
|
|
final class ConnectionFactory implements ConnectionFactoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ConfigurationManager |
|
23
|
|
|
*/ |
|
24
|
|
|
private ConfigurationManager $configurationManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ConfigurationManager $configurationManager |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(ConfigurationManager $configurationManager) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->configurationManager = $configurationManager; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new connection from the provided $config |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $config |
|
38
|
|
|
* @param Configuration|string|null $configuration |
|
39
|
|
|
* @param EventManager|string|null $eventManager |
|
40
|
|
|
* |
|
41
|
|
|
* @return Connection |
|
42
|
|
|
* |
|
43
|
|
|
* @throws ConnectionFactoryException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create(array $config, $configuration = null, $eventManager = null): Connection |
|
46
|
|
|
{ |
|
47
|
|
|
$params = array_merge( |
|
48
|
|
|
[ |
|
49
|
|
|
'driverClass' => $config['driverClass'] ?? Driver::class, |
|
50
|
|
|
'wrapperClass' => $config['wrapperClass'] ?? null, |
|
51
|
|
|
'pdo' => $config['pdo'] ?? null, |
|
52
|
|
|
], |
|
53
|
|
|
$config['params'] ?? [] |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($config['platform'])) { |
|
57
|
|
|
$platform = null; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
try { |
|
61
|
|
|
if (is_string($configuration)) { |
|
62
|
|
|
$configuration = $this->configurationManager->getConfiguration($configuration); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return DriverManager::getConnection($params, $configuration, $eventManager); |
|
|
|
|
|
|
66
|
|
|
} catch (\Throwable $e) { |
|
67
|
|
|
throw new ConnectionFactoryException( |
|
68
|
|
|
sprintf('Failed to create new connection: %s', $e->getMessage()), |
|
69
|
|
|
$e->getCode(), |
|
70
|
|
|
$e |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|