aboutsummaryrefslogtreecommitdiffstats
path: root/includes/db/DBConnRef.php
diff options
context:
space:
mode:
authorAaron Schulz <aschulz@wikimedia.org>2015-04-24 10:00:22 -0700
committerAaron Schulz <aschulz@wikimedia.org>2015-04-24 18:02:50 +0000
commitfb10df98df5cad78312e270f2d59733c5d2a3ce5 (patch)
treefff824b9074335d230fe8ecd20b8ee2663ecd06a /includes/db/DBConnRef.php
parent9cf5619a316bac35fe5f05fa3fe7e494a4338f57 (diff)
downloadmediawikicore-fb10df98df5cad78312e270f2d59733c5d2a3ce5.tar.gz
mediawikicore-fb10df98df5cad78312e270f2d59733c5d2a3ce5.zip
Moved DBConnRef to a separate file
Change-Id: I9c8570aefb8927a3d69b7fd446165f6e8661e84d
Diffstat (limited to 'includes/db/DBConnRef.php')
-rw-r--r--includes/db/DBConnRef.php46
1 files changed, 46 insertions, 0 deletions
diff --git a/includes/db/DBConnRef.php b/includes/db/DBConnRef.php
new file mode 100644
index 000000000000..7045494abd18
--- /dev/null
+++ b/includes/db/DBConnRef.php
@@ -0,0 +1,46 @@
+<?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 );
+ }
+ }
+}