1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Simple generator of database connections that always returns the same object. |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License along |
16
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
17
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
19
|
|
|
* |
20
|
|
|
* @file |
21
|
|
|
* @ingroup Database |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* An LBFactory class that always returns a single database object. |
26
|
|
|
*/ |
27
|
|
|
class LBFactorySingle extends LBFactory { |
28
|
|
|
/** @var LoadBalancerSingle */ |
29
|
|
|
private $lb; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $conf An associative array with one member: |
33
|
|
|
* - connection: The IDatabase connection object |
34
|
|
|
*/ |
35
|
|
|
public function __construct( array $conf ) { |
36
|
|
|
parent::__construct( $conf ); |
37
|
|
|
|
38
|
|
|
if ( !isset( $conf['connection'] ) ) { |
39
|
|
|
throw new InvalidArgumentException( "Missing 'connection' argument." ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$lb = new LoadBalancerSingle( array_merge( $this->baseLoadBalancerParams(), $conf ) ); |
43
|
|
|
$this->initLoadBalancer( $lb ); |
44
|
|
|
$this->lb = $lb; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param IDatabase $db Live connection handle |
49
|
|
|
* @param array $params Parameter map to LBFactorySingle::__constructs() |
50
|
|
|
* @return LBFactorySingle |
51
|
|
|
* @since 1.28 |
52
|
|
|
*/ |
53
|
|
|
public static function newFromConnection( IDatabase $db, array $params = [] ) { |
54
|
|
|
return new static( [ 'connection' => $db ] + $params ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool|string $domain (unused) |
59
|
|
|
* @return LoadBalancerSingle |
60
|
|
|
*/ |
61
|
|
|
public function newMainLB( $domain = false ) { |
62
|
|
|
return $this->lb; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param bool|string $domain (unused) |
67
|
|
|
* @return LoadBalancerSingle |
68
|
|
|
*/ |
69
|
|
|
public function getMainLB( $domain = false ) { |
70
|
|
|
return $this->lb; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function newExternalLB( $cluster ) { |
74
|
|
|
throw new BadMethodCallException( "Method is not supported." ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getExternalLB( $cluster ) { |
78
|
|
|
throw new BadMethodCallException( "Method is not supported." ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer) |
83
|
|
|
*/ |
84
|
|
|
public function getAllMainLBs() { |
85
|
|
|
return [ 'DEFAULT' => $this->lb ]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer) |
90
|
|
|
*/ |
91
|
|
|
public function getAllExternalLBs() { |
92
|
|
|
return []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string|callable $callback |
97
|
|
|
* @param array $params |
98
|
|
|
*/ |
99
|
|
|
public function forEachLB( $callback, array $params = [] ) { |
100
|
|
|
call_user_func_array( $callback, array_merge( [ $this->lb ], $params ) ); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|