forked from floriansemm/SolrBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolrConnectionFactory.php
56 lines (45 loc) · 1.31 KB
/
SolrConnectionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace FS\SolrBundle;
class SolrConnectionFactory {
/**
* @var array
*/
private $connections = array();
/**
* @param array $connections
*/
public function __construct(array $connections = array()) {
$this->connections = $connections;
}
/**
* @param string $name
* @throws \RuntimeException if there are no connections
* @throws \InvalidArgumentException if the given connection name is unknown
* @return SolrConnection
*/
public function getConnection($name) {
if (count($this->connections) == 0) {
throw new \RuntimeException('No connections found');
}
if (!array_key_exists($name, $this->connections)) {
throw new \InvalidArgumentException(sprintf('Unknown connection %s', $name));
}
$connectionParams = $this->connections[$name];
return new SolrConnection($connectionParams);;
}
/**
* first connection in connections-stack is default connection
*
* @throws \RuntimeException if there no connection in the stack
* @return SolrConnection
*/
public function getDefaultConnection() {
$keysAsValues = array_keys($this->connections);
$firstConnectionName = array_pop($keysAsValues);
if ($firstConnectionName == '') {
throw new \RuntimeException('Can not find default connection!');
}
return $this->getConnection($firstConnectionName);
}
}
?>