Created
February 12, 2013 20:33
-
-
Save nodesocket/4773126 to your computer and use it in GitHub Desktop.
MongoConnection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MongoConnection { | |
/** | |
* @staticvar Resource | |
*/ | |
private static $mongo_client; | |
private static $mongo_db; | |
private static $mongo_collection; | |
public static function connect($timeout = 10000, $debug = false) { | |
if($debug) { | |
MongoLog::setLevel(MongoLog::ALL); | |
MongoLog::setModule(MongoLog::ALL); | |
} | |
try { | |
$username = MongoConfiguration::username; | |
$replica_set = MongoConfiguration::replica_set; | |
//// | |
// Auth | |
//// | |
if(!empty($username)) { | |
//// | |
// Replica set | |
//// | |
if(!empty($replica_set)) { | |
MongoConnection::$mongo_client = new MongoClient("mongodb://" . MongoConfiguration::username . ":" . MongoConfiguration::password . "@" . MongoConfiguration::hosts . "/" . MongoConfiguration::database, array("timeout" => $timeout, "replicaSet" => MongoConfiguration::replica_set)); | |
} else { | |
MongoConnection::$mongo_client = new MongoClient("mongodb://" . MongoConfiguration::username . ":" . MongoConfiguration::password . "@" . MongoConfiguration::hosts . "/" . MongoConfiguration::database, array("timeout" => $timeout)); | |
} | |
} | |
//// | |
// No auth | |
//// | |
else { | |
//// | |
// Replica set | |
//// | |
if(!empty($replica_set)) { | |
MongoConnection::$mongo_client = new MongoClient("mongodb://" . MongoConfiguration::hosts . "/" . MongoConfiguration::database, array("timeout" => $timeout, "replicaSet" => MongoConfiguration::replica_set)); | |
} else { | |
MongoConnection::$mongo_client = new MongoClient("mongodb://" . MongoConfiguration::hosts . "/" . MongoConfiguration::database, array("timeout" => $timeout)); | |
} | |
} | |
} catch(Exception $mongoException) { | |
return false; | |
} | |
if(empty(MongoConnection::$mongo_client)) { | |
return false; | |
} | |
try { | |
MongoConnection::$mongo_db = MongoConnection::$mongo_client->selectDB(MongoConfiguration::database); | |
} catch(Exception $mongoException) { | |
return false; | |
} | |
if(empty(MongoConnection::$mongo_db)) { | |
return false; | |
} | |
return true; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment