Merge "RevisionStoreDbTestBase, remove redundant needsDB override"
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactorySingle.php
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 namespace Wikimedia\Rdbms;
25
26 use InvalidArgumentException;
27 use BadMethodCallException;
28
29 /**
30 * An LBFactory class that always returns a single database object.
31 */
32 class LBFactorySingle extends LBFactory {
33 /** @var LoadBalancerSingle */
34 private $lb;
35
36 /**
37 * @param array $conf An associative array with one member:
38 * - connection: The IDatabase connection object
39 */
40 public function __construct( array $conf ) {
41 parent::__construct( $conf );
42
43 if ( !isset( $conf['connection'] ) ) {
44 throw new InvalidArgumentException( "Missing 'connection' argument." );
45 }
46
47 $lb = new LoadBalancerSingle( array_merge( $this->baseLoadBalancerParams(), $conf ) );
48 $this->initLoadBalancer( $lb );
49 $this->lb = $lb;
50 }
51
52 /**
53 * @param IDatabase $db Live connection handle
54 * @param array $params Parameter map to LBFactorySingle::__constructs()
55 * @return LBFactorySingle
56 * @since 1.28
57 */
58 public static function newFromConnection( IDatabase $db, array $params = [] ) {
59 return new static( array_merge(
60 [ 'localDomain' => $db->getDomainID() ],
61 $params,
62 [ 'connection' => $db ]
63 ) );
64 }
65
66 /**
67 * @param bool|string $domain (unused)
68 * @return LoadBalancerSingle
69 */
70 public function newMainLB( $domain = false ) {
71 return $this->lb;
72 }
73
74 /**
75 * @param bool|string $domain (unused)
76 * @return LoadBalancerSingle
77 */
78 public function getMainLB( $domain = false ) {
79 return $this->lb;
80 }
81
82 public function newExternalLB( $cluster ) {
83 throw new BadMethodCallException( "Method is not supported." );
84 }
85
86 public function getExternalLB( $cluster ) {
87 throw new BadMethodCallException( "Method is not supported." );
88 }
89
90 /**
91 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
92 */
93 public function getAllMainLBs() {
94 return [ 'DEFAULT' => $this->lb ];
95 }
96
97 /**
98 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
99 */
100 public function getAllExternalLBs() {
101 return [];
102 }
103
104 /**
105 * @param string|callable $callback
106 * @param array $params
107 */
108 public function forEachLB( $callback, array $params = [] ) {
109 if ( isset( $this->lb ) ) { // may not be set during _destruct()
110 $callback( $this->lb, ...$params );
111 }
112 }
113 }