use EmptyBagOStuff;
use WANObjectCache;
use ArrayUtils;
+use UnexpectedValueException;
use InvalidArgumentException;
use RuntimeException;
use Exception;
private $srvCache;
/** @var WANObjectCache */
private $wanCache;
- /** @var object|string Class name or object With profileIn/profileOut methods */
+ /** @var mixed Class name or object With profileIn/profileOut methods */
private $profiler;
/** @var TransactionProfiler */
private $trxProfiler;
$this->maxLag = $params['maxLag'];
}
- if ( isset( $params['loadMonitor'] ) ) {
- $this->loadMonitorConfig = $params['loadMonitor'];
- } else {
- $this->loadMonitorConfig = [ 'class' => 'LoadMonitorNull' ];
- }
+ $this->loadMonitorConfig = $params['loadMonitor'] ?? [ 'class' => 'LoadMonitorNull' ];
$this->loadMonitorConfig += [ 'lagWarnThreshold' => $this->maxLag ];
foreach ( $params['servers'] as $i => $server ) {
}
}
- if ( isset( $params['srvCache'] ) ) {
- $this->srvCache = $params['srvCache'];
- } else {
- $this->srvCache = new EmptyBagOStuff();
- }
- if ( isset( $params['wanCache'] ) ) {
- $this->wanCache = $params['wanCache'];
- } else {
- $this->wanCache = WANObjectCache::newEmpty();
- }
+ $this->srvCache = $params['srvCache'] ?? new EmptyBagOStuff();
+ $this->wanCache = $params['wanCache'] ?? WANObjectCache::newEmpty();
$this->profiler = $params['profiler'] ?? null;
- if ( isset( $params['trxProfiler'] ) ) {
- $this->trxProfiler = $params['trxProfiler'];
- } else {
- $this->trxProfiler = new TransactionProfiler();
- }
+ $this->trxProfiler = $params['trxProfiler'] ?? new TransactionProfiler();
$this->errorLogger = $params['errorLogger'] ?? function ( Exception $e ) {
trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
}
}
+ // Final sanity check to make sure the right domain is selected
+ if (
+ $conn instanceof IDatabase &&
+ !$this->localDomain->isCompatible( $conn->getDomainID() )
+ ) {
+ throw new UnexpectedValueException(
+ "Got connection to '{$conn->getDomainID()}', " .
+ "but expected local domain ('{$this->localDomain}')." );
+ }
+
return $conn;
}
}
}
- // Increment reference count
if ( $conn instanceof IDatabase ) {
+ // Final sanity check to make sure the right domain is selected
+ if ( !$domainInstance->isCompatible( $conn->getDomainID() ) ) {
+ throw new UnexpectedValueException(
+ "Got connection to '{$conn->getDomainID()}', but expected '$domain'." );
+ }
+ // Increment reference count
$refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
$conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
}
}
public function getServerName( $i ) {
- if ( isset( $this->servers[$i]['hostName'] ) ) {
- $name = $this->servers[$i]['hostName'];
- } elseif ( isset( $this->servers[$i]['host'] ) ) {
- $name = $this->servers[$i]['host'];
- } else {
- $name = '';
- }
+ $name = $this->servers[$i]['hostName'] ?? $this->servers[$i]['host'] ?? '';
return ( $name != '' ) ? $name : 'localhost';
}
public function getServerInfo( $i ) {
- if ( isset( $this->servers[$i] ) ) {
- return $this->servers[$i];
- } else {
- return false;
- }
+ return $this->servers[$i] ?? false;
}
public function getServerType( $i ) {