Repository navigation
Expand file tree
/
Copy pathMongoDb.php
More file actions
135 lines (120 loc) · 3.6 KB
/
Copy pathMongoDb.php
File metadata and controls
135 lines (120 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Codeception\Lib\Driver;
use Codeception\Exception\ModuleConfigException;
use Codeception\Exception\ModuleException;
class MongoDb
{
private $dbh;
private $dsn;
private $dbName;
private $host;
private $user;
private $password;
private $client;
public static function connect($dsn, $user, $password)
{
throw new \Exception(__CLASS__ . '::connect() - hm, it looked like this method had become obsolete...');
}
/**
* $dsn has to contain db_name after the host. E.g. "mongodb://localhost:27017/mongo_test_db"
*
* @static
*
* @param $dsn
* @param $user
* @param $password
*
* @throws ModuleConfigException
* @throws \Exception
*/
public function __construct($dsn, $user, $password)
{
/* defining DB name */
$this->dbName = substr($dsn, strrpos($dsn, '/') + 1);
if (strlen($this->dbName) == 0) {
throw new ModuleConfigException($this, 'Please specify valid $dsn with DB name after the host:port');
}
/* defining host */
if (false !== strpos($dsn, 'mongodb://')) {
$this->host = str_replace('mongodb://', '', $dsn);
} else {
$this->host = $dsn;
}
$this->host = rtrim(str_replace($this->dbName, '', $this->host), '/');
$options = [
'connect' => true
];
if ($user && $password) {
$options += [
'username' => $user,
'password' => $password
];
}
try {
$this->client = new \MongoClient($dsn, $options);
$this->dbh = $this->client->selectDB($this->dbName);
} catch (\MongoConnectionException $e) {
throw new ModuleException($this, sprintf('Failed to open Mongo connection: %s', $e->getMessage()));
}
$this->dsn = $dsn;
$this->user = $user;
$this->password = $password;
}
/**
* @static
*
* @param $dsn
* @param $user
* @param $password
*
* @return MongoDb
*/
public static function create($dsn, $user, $password)
{
return new MongoDb($dsn, $user, $password);
}
public function cleanup()
{
try {
$list = $this->dbh->listCollections();
} catch (\MongoException $e) {
throw new \Exception(sprintf('Failed to list collections of the DB: %s', $e->getMessage()));
}
foreach ($list as $collection) {
try {
$collection->drop();
} catch (\MongoException $e) {
throw new \Exception(sprintf('Failed to drop collection: %s', $e->getMessage()));
}
}
}
/**
* dump file has to be a javascript document where one can use all the mongo shell's commands
* just FYI: this file can be easily created be RockMongo's export button
*
* @param $dumpFile
*/
public function load($dumpFile)
{
if ($this->user && $this->password) {
$cmd = sprintf(
'mongo %s --username %s --password %s %s',
$this->host . '/' . $this->dbName,
$this->user,
$this->password,
escapeshellarg($dumpFile)
);
} else {
$cmd = sprintf('mongo %s %s', $this->host . '/' . $this->dbName, escapeshellarg($dumpFile));
}
shell_exec($cmd);
}
public function getDbh()
{
return $this->dbh;
}
public function setDatabase($dbName)
{
$this->dbh = $this->client->selectDB($dbName);
}
}