-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for new MongoDB driver (#177)
- Loading branch information
Showing
2 changed files
with
73 additions
and
25 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,8 @@ class Mongo { | |
$dsn, | ||
//! MongoDB object | ||
$db, | ||
//! Legacy flag | ||
$legacy, | ||
//! MongoDB log | ||
$log; | ||
|
||
|
@@ -63,7 +65,7 @@ function uuid() { | |
**/ | ||
function log($flag=TRUE) { | ||
if ($flag) { | ||
$cursor=$this->selectcollection('system.profile')->find(); | ||
$cursor=$this->db->selectcollection('system.profile')->find(); | ||
foreach (iterator_to_array($cursor) as $frame) | ||
if (!preg_match('/\.system\..+$/',$frame['ns'])) | ||
$this->log.=date('r',$frame['ts']->sec).' ('. | ||
|
@@ -76,7 +78,10 @@ function log($flag=TRUE) { | |
PHP_EOL; | ||
} else { | ||
$this->log=FALSE; | ||
$this->setprofilinglevel(-1); | ||
if ($legacy) | ||
$this->db->setprofilinglevel(-1); | ||
else | ||
$this->db->command(['profile'=>-1]); | ||
} | ||
return $this->log; | ||
} | ||
|
@@ -87,8 +92,12 @@ function log($flag=TRUE) { | |
**/ | ||
function drop() { | ||
$out=$this->db->drop(); | ||
if ($this->log!==FALSE) | ||
$this->setprofilinglevel(2); | ||
if ($this->log!==FALSE) { | ||
if ($legacy) | ||
$this->db->setprofilinglevel(2); | ||
else | ||
$this->db->command(['profile'=>2]); | ||
} | ||
return $out; | ||
} | ||
|
||
|
@@ -102,6 +111,14 @@ function __call($func,array $args) { | |
return call_user_func_array([$this->db,$func],$args); | ||
} | ||
|
||
/** | ||
* Return TRUE if legacy driver is loaded | ||
* @return bool | ||
**/ | ||
function legacy() { | ||
return $this->legacy; | ||
} | ||
|
||
//! Prohibit cloning | ||
private function __clone() { | ||
} | ||
|
@@ -114,9 +131,14 @@ private function __clone() { | |
**/ | ||
function __construct($dsn,$dbname,array $options=NULL) { | ||
$this->uuid=\Base::instance()->hash($this->dsn=$dsn); | ||
$class=class_exists('\MongoClient')?'\MongoClient':'\Mongo'; | ||
$this->db=new \MongoDB(new $class($dsn,$options?:[]),$dbname); | ||
$this->setprofilinglevel(2); | ||
if ($this->legacy=class_exists('\MongoClient')) { | ||
$this->db=new \MongoDB(new $class($dsn,$options?:[]),$dbname); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bcosca
Author
Collaborator
|
||
$this->db->setprofilinglevel(2); | ||
} | ||
else { | ||
$this->db=(new \MongoDB\Client($dsn,$options?:[]))->$dbname; | ||
$this->db->command(['profile'=>2]); | ||
} | ||
} | ||
|
||
} |
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
$class is not defined here, I guess the previous check was to also support the even older Mongo class, pre MongoClient.