wiki.techinc.nl/includes/db/DBConnRef.php
Aaron Schulz fb10df98df Moved DBConnRef to a separate file
Change-Id: I9c8570aefb8927a3d69b7fd446165f6e8661e84d
2015-04-24 18:02:50 +00:00

46 lines
1.1 KiB
PHP

<?php
/**
* Helper class to handle automatically marking connections as reusable (via RAII pattern)
* as well handling deferring the actual network connection until the handle is used
*
* @ingroup Database
* @since 1.22
*/
class DBConnRef implements IDatabase {
/** @var LoadBalancer */
private $lb;
/** @var DatabaseBase|null */
private $conn;
/** @var array|null */
private $params;
/**
* @param LoadBalancer $lb
* @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
*/
public function __construct( LoadBalancer $lb, $conn ) {
$this->lb = $lb;
if ( $conn instanceof DatabaseBase ) {
$this->conn = $conn;
} else {
$this->params = $conn;
}
}
public function __call( $name, $arguments ) {
if ( $this->conn === null ) {
list( $db, $groups, $wiki ) = $this->params;
$this->conn = $this->lb->getConnection( $db, $groups, $wiki );
}
return call_user_func_array( array( $this->conn, $name ), $arguments );
}
public function __destruct() {
if ( $this->conn !== null ) {
$this->lb->reuseConnection( $this->conn );
}
}
}