1 | <?php |
||
2 | |||
3 | namespace Brouzie\Sphinxy; |
||
4 | |||
5 | class Registry |
||
6 | { |
||
7 | /** |
||
8 | * @var Connection[] |
||
9 | */ |
||
10 | private $connections; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $defaultConnection; |
||
16 | |||
17 | /** |
||
18 | * @param Connection[] $connections |
||
19 | * @param string $defaultConnection |
||
20 | */ |
||
21 | public function __construct($connections, $defaultConnection) |
||
22 | { |
||
23 | $this->connections = $connections; |
||
24 | $this->defaultConnection = $defaultConnection; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Gets the default connection name. |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public function getDefaultConnectionName() |
||
33 | { |
||
34 | return $this->defaultConnection; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Gets the named connection. |
||
39 | * |
||
40 | * @param string $name the connection name (null for the default one) |
||
41 | * |
||
42 | * @return Connection |
||
43 | */ |
||
44 | public function getConnection($name = null) |
||
45 | { |
||
46 | if (null === $name) { |
||
47 | $name = $this->defaultConnection; |
||
48 | } |
||
49 | |||
50 | if (!isset($this->connections[$name])) { |
||
51 | throw new \InvalidArgumentException(sprintf('Sphinxy Connection named "%s" does not exist.', $name)); |
||
52 | } |
||
53 | |||
54 | return $this->connections[$name]; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Gets an array of all registered connections. |
||
59 | * |
||
60 | * @return Connection[] |
||
61 | */ |
||
62 | public function getConnections() |
||
63 | { |
||
64 | return $this->connections; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Gets all connection names. |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getConnectionNames() |
||
73 | { |
||
74 | return array_keys($this->connections); |
||
75 | } |
||
76 | } |
||
77 |