Skip to content

Commit

Permalink
use new mongodb driver (if available) instead of the legacy driver
Browse files Browse the repository at this point in the history
  • Loading branch information
tessus committed Mar 29, 2016
1 parent 69615c0 commit d9f285e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/Analog/Handler/Mongo.php
Expand Up @@ -26,14 +26,28 @@
*/
class Mongo {
public static function init ($server, $database, $collection) {
if ($server instanceof \MongoClient) {
$db = $server->{$database};
if (extension_loaded('mongodb')) {
if ($server instanceof \MongoDB\Driver\Manager) {
$manager = $server;
} else {
$manager = new \MongoDB\Driver\Manager("mongodb://$server");
}
return function ($info) use ($manager, $database, $collection) {
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->insert($info);
$dbAndColl = $database.'.'.$collection;
$manager->executeBulkWrite($dbAndColl, $bulk);
};
} else {
$conn = new \MongoClient ("mongodb://$server");
$db = $conn->{$database};
if ($server instanceof \MongoClient) {
$db = $server->{$database};
} else {
$conn = new \MongoClient ("mongodb://$server");
$db = $conn->{$database};
}
return function ($info) use ($db, $collection) {
$db->{$collection}->insert ($info);
};
}
return function ($info) use ($db, $collection) {
$db->{$collection}->insert ($info);
};
}
}

0 comments on commit d9f285e

Please sign in to comment.