Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / db / DBConnRef.php
1 <?php
2 /**
3 * Helper class to handle automatically marking connections as reusable (via RAII pattern)
4 * as well handling deferring the actual network connection until the handle is used
5 *
6 * @ingroup Database
7 * @since 1.22
8 */
9 class DBConnRef implements IDatabase {
10 /** @var LoadBalancer */
11 private $lb;
12
13 /** @var DatabaseBase|null */
14 private $conn;
15
16 /** @var array|null */
17 private $params;
18
19 /**
20 * @param LoadBalancer $lb
21 * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
22 */
23 public function __construct( LoadBalancer $lb, $conn ) {
24 $this->lb = $lb;
25 if ( $conn instanceof DatabaseBase ) {
26 $this->conn = $conn;
27 } else {
28 $this->params = $conn;
29 }
30 }
31
32 public function __call( $name, $arguments ) {
33 if ( $this->conn === null ) {
34 list( $db, $groups, $wiki ) = $this->params;
35 $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
36 }
37
38 return call_user_func_array( array( $this->conn, $name ), $arguments );
39 }
40
41 public function __destruct() {
42 if ( $this->conn !== null ) {
43 $this->lb->reuseConnection( $this->conn );
44 }
45 }
46 }