Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| LBFactorySingle | |
0.00% |
0 / 30 |
|
0.00% |
0 / 11 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| newFromConnection | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| newDisabled | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| newMainLB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMainLB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newExternalLB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExternalLB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllMainLBs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllExternalLBs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLBsForOwner | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| __destruct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | namespace Wikimedia\Rdbms; |
| 7 | |
| 8 | use BadMethodCallException; |
| 9 | use InvalidArgumentException; |
| 10 | |
| 11 | /** |
| 12 | * LoadBalancer manager for sites with one "main" cluster using only injected database connections |
| 13 | * |
| 14 | * This class assumes that there are no "external" clusters. |
| 15 | * |
| 16 | * LoadBalancerDisabled will be used if a null connection handle is injected. |
| 17 | * |
| 18 | * @see ILBFactory |
| 19 | * @ingroup Database |
| 20 | */ |
| 21 | class LBFactorySingle extends LBFactory { |
| 22 | /** @var LoadBalancerSingle|LoadBalancerDisabled */ |
| 23 | private $mainLB; |
| 24 | |
| 25 | /** |
| 26 | * @note Use of {@link newFromConnection} is preferable |
| 27 | * |
| 28 | * @param array $conf An associative array containing one of the following: |
| 29 | * - connection: The IDatabase connection handle to use; null to disable access |
| 30 | */ |
| 31 | public function __construct( array $conf ) { |
| 32 | parent::__construct( $conf ); |
| 33 | |
| 34 | if ( !array_key_exists( 'connection', $conf ) ) { |
| 35 | throw new InvalidArgumentException( "Missing 'connection' argument." ); |
| 36 | } |
| 37 | |
| 38 | $conn = $conf['connection']; |
| 39 | if ( $conn ) { |
| 40 | $mainLB = new LoadBalancerSingle( array_merge( |
| 41 | $this->baseLoadBalancerParams(), |
| 42 | [ 'connection' => $conn ] |
| 43 | ) ); |
| 44 | } else { |
| 45 | $mainLB = new LoadBalancerDisabled( $this->baseLoadBalancerParams() ); |
| 46 | } |
| 47 | $this->initLoadBalancer( $mainLB ); |
| 48 | |
| 49 | $this->mainLB = $mainLB; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param IDatabase $db Live connection handle |
| 54 | * @param array $params Parameter map to LBFactorySingle::__construct() |
| 55 | * @since 1.28 |
| 56 | */ |
| 57 | public static function newFromConnection( IDatabase $db, array $params = [] ): static { |
| 58 | return new static( [ |
| 59 | 'localDomain' => $db->getDomainID(), |
| 60 | ...$params, |
| 61 | 'connection' => $db, |
| 62 | ] ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param array $params Parameter map to LBFactorySingle::__construct() |
| 67 | * @since 1.40 |
| 68 | */ |
| 69 | public static function newDisabled( array $params = [] ): static { |
| 70 | return new static( array_merge( |
| 71 | $params, |
| 72 | [ 'connection' => null ] |
| 73 | ) ); |
| 74 | } |
| 75 | |
| 76 | /** @inheritDoc */ |
| 77 | public function newMainLB( $domain = false ): ILoadBalancerForOwner { |
| 78 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
| 79 | throw new BadMethodCallException( "Method is not supported." ); |
| 80 | } |
| 81 | |
| 82 | /** @inheritDoc */ |
| 83 | public function getMainLB( $domain = false ): ILoadBalancer { |
| 84 | return $this->mainLB; |
| 85 | } |
| 86 | |
| 87 | /** @inheritDoc */ |
| 88 | public function newExternalLB( $cluster ): ILoadBalancerForOwner { |
| 89 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
| 90 | throw new BadMethodCallException( "Method is not supported." ); |
| 91 | } |
| 92 | |
| 93 | /** @inheritDoc */ |
| 94 | public function getExternalLB( $cluster ): ILoadBalancer { |
| 95 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
| 96 | throw new BadMethodCallException( "Method is not supported." ); |
| 97 | } |
| 98 | |
| 99 | public function getAllMainLBs(): array { |
| 100 | return [ self::CLUSTER_MAIN_DEFAULT => $this->mainLB ]; |
| 101 | } |
| 102 | |
| 103 | public function getAllExternalLBs(): array { |
| 104 | return []; |
| 105 | } |
| 106 | |
| 107 | /** @inheritDoc */ |
| 108 | protected function getLBsForOwner() { |
| 109 | if ( $this->mainLB !== null ) { |
| 110 | yield $this->mainLB; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public function __destruct() { |
| 115 | // do nothing since the connection was injected |
| 116 | } |
| 117 | } |