Completed
Branch master (bbf110)
by
unknown
25:51
created

LoadBalancerSingle::__construct()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 8.7624
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 DatabaseBase connection object
34
	 */
35
	public function __construct( array $conf ) {
36
		parent::__construct( $conf );
37
38
		$this->lb = new LoadBalancerSingle( [
39
			'readOnlyReason' => $this->readOnlyReason,
40
			'trxProfiler' => $this->trxProfiler,
41
			'srvCache' => $this->srvCache,
42
			'wanCache' => $this->wanCache
43
		] + $conf );
44
	}
45
46
	/**
47
	 * @param bool|string $wiki
48
	 * @return LoadBalancerSingle
49
	 */
50
	public function newMainLB( $wiki = false ) {
51
		return $this->lb;
52
	}
53
54
	/**
55
	 * @param bool|string $wiki
56
	 * @return LoadBalancerSingle
57
	 */
58
	public function getMainLB( $wiki = false ) {
59
		return $this->lb;
60
	}
61
62
	/**
63
	 * @param string $cluster External storage cluster, or false for core
64
	 * @param bool|string $wiki Wiki ID, or false for the current wiki
65
	 * @return LoadBalancerSingle
66
	 */
67
	protected function newExternalLB( $cluster, $wiki = false ) {
68
		return $this->lb;
69
	}
70
71
	/**
72
	 * @param string $cluster External storage cluster, or false for core
73
	 * @param bool|string $wiki Wiki ID, or false for the current wiki
74
	 * @return LoadBalancerSingle
75
	 */
76
	public function &getExternalLB( $cluster, $wiki = false ) {
77
		return $this->lb;
78
	}
79
80
	/**
81
	 * @param string|callable $callback
82
	 * @param array $params
83
	 */
84
	public function forEachLB( $callback, array $params = [] ) {
85
		call_user_func_array( $callback, array_merge( [ $this->lb ], $params ) );
86
	}
87
}
88
89
/**
90
 * Helper class for LBFactorySingle.
91
 */
92
class LoadBalancerSingle extends LoadBalancer {
93
	/** @var DatabaseBase */
94
	private $db;
95
96
	/**
97
	 * @param array $params
98
	 */
99
	public function __construct( array $params ) {
100
		$this->db = $params['connection'];
101
102
		parent::__construct( [
103
			'servers' => [
104
				[
105
					'type' => $this->db->getType(),
106
					'host' => $this->db->getServer(),
107
					'dbname' => $this->db->getDBname(),
108
					'load' => 1,
109
				]
110
			],
111
			'trxProfiler' => isset( $params['trxProfiler'] ) ? $params['trxProfiler'] : null,
112
			'srvCache' => isset( $params['srvCache'] ) ? $params['srvCache'] : null,
113
			'wanCache' => isset( $params['wanCache'] ) ? $params['wanCache'] : null
114
		] );
115
116
		if ( isset( $params['readOnlyReason'] ) ) {
117
			$this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
118
		}
119
	}
120
121
	/**
122
	 *
123
	 * @param string $server
124
	 * @param bool $dbNameOverride
125
	 *
126
	 * @return DatabaseBase
127
	 */
128
	protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
129
		return $this->db;
130
	}
131
}
132