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