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
|
|
|
/** |
16
|
|
|
* @deprecated |
17
|
|
|
* @author Alex Patterson <[email protected]> |
18
|
|
|
* @package Arp\LaminasDoctrine\Service\Connection |
19
|
|
|
*/ |
20
|
|
|
final class ConnectionFactory implements ConnectionFactoryInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ConfigurationManagerInterface |
24
|
|
|
*/ |
25
|
|
|
private ConfigurationManagerInterface $configurationManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Closure |
29
|
|
|
*/ |
30
|
|
|
private \Closure $factoryWrapper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array<mixed> |
34
|
|
|
*/ |
35
|
|
|
private array $defaultConfig; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ConfigurationManagerInterface $configurationManager |
39
|
|
|
* @param callable|null $factory |
40
|
|
|
* @param array<mixed> $defaultConfig |
41
|
|
|
* |
42
|
|
|
* @noinspection ProperNullCoalescingOperatorUsageInspection [$this, 'doCreate'] is of type callable |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
ConfigurationManagerInterface $configurationManager, |
46
|
|
|
?callable $factory = null, |
47
|
|
|
array $defaultConfig = [] |
48
|
|
|
) { |
49
|
|
|
$this->configurationManager = $configurationManager; |
50
|
|
|
$this->factoryWrapper = \Closure::fromCallable($factory ?? [$this, 'doCreate']); |
51
|
|
|
$this->defaultConfig = $defaultConfig; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a new connection from the provided $config |
56
|
|
|
* |
57
|
|
|
* @param array<mixed> $config |
58
|
|
|
* @param Configuration|string|null $configuration |
59
|
|
|
* @param EventManager|null $eventManager |
60
|
|
|
* |
61
|
|
|
* @return Connection |
62
|
|
|
* |
63
|
|
|
* @throws ConnectionFactoryException |
64
|
|
|
*/ |
65
|
|
|
public function create(array $config, $configuration = null, ?EventManager $eventManager = null): Connection |
66
|
|
|
{ |
67
|
|
|
$config = array_replace_recursive($this->defaultConfig, $config); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
if (is_string($configuration)) { |
71
|
|
|
$configuration = $this->configurationManager->getConfiguration($configuration); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return call_user_func($this->factoryWrapper, $config, $configuration, $eventManager); |
75
|
|
|
} catch (\Exception $e) { |
76
|
|
|
throw new ConnectionFactoryException( |
|
|
|
|
77
|
|
|
sprintf('Failed to create new connection: %s', $e->getMessage()), |
78
|
|
|
$e->getCode(), |
79
|
|
|
$e |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Default factory creation callable |
86
|
|
|
* |
87
|
|
|
* @param array<mixed> $config |
88
|
|
|
* @param Configuration|null $configuration |
89
|
|
|
* @param EventManager|null $eventManager |
90
|
|
|
* |
91
|
|
|
* @return Connection |
92
|
|
|
* |
93
|
|
|
* @throws Exception |
94
|
|
|
* @noinspection PhpUnusedPrivateMethodInspection |
95
|
|
|
*/ |
96
|
|
|
private function doCreate( |
97
|
|
|
array $config, |
98
|
|
|
?Configuration $configuration, |
99
|
|
|
?EventManager $eventManager = null |
100
|
|
|
): Connection { |
101
|
|
|
return DriverManager::getConnection($config, $configuration, $eventManager); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.