1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Registry; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Registry\ConnectionRegistry; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use function sprintf; |
12
|
|
|
|
13
|
|
|
class Psr11ConnectionRegistry implements ConnectionRegistry |
14
|
|
|
{ |
15
|
|
|
/** @var ContainerInterface */ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $defaultConnectionName; |
20
|
|
|
|
21
|
|
|
/** @var string[] */ |
22
|
|
|
private $connectionNames; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string[] $connectionNames |
26
|
|
|
*/ |
27
|
|
|
public function __construct(ContainerInterface $container, string $defaultConnectionName, array $connectionNames) |
28
|
|
|
{ |
29
|
|
|
$this->container = $container; |
30
|
|
|
$this->defaultConnectionName = $defaultConnectionName; |
31
|
|
|
$this->connectionNames = $connectionNames; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDefaultConnectionName() : string |
35
|
|
|
{ |
36
|
|
|
return $this->defaultConnectionName; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getConnection(?string $name = null) : Connection |
40
|
|
|
{ |
41
|
|
|
$name = $name ?? $this->defaultConnectionName; |
42
|
|
|
|
43
|
|
|
if (! $this->container->has($name)) { |
44
|
|
|
throw new InvalidArgumentException(sprintf('Connection with name "%s" does not exist.', $name)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->container->get($name); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function getConnections() : array |
54
|
|
|
{ |
55
|
|
|
$connections = []; |
56
|
|
|
|
57
|
|
|
foreach ($this->connectionNames as $connectionName) { |
58
|
|
|
$connections[$connectionName] = $this->container->get($connectionName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $connections; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
public function getConnectionNames() : array |
68
|
|
|
{ |
69
|
|
|
return $this->connectionNames; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|