forked from angyvolin/mongodb-php-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
128 lines (111 loc) · 3.06 KB
/
Client.php
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
<?php
namespace Tequila\MongoDB;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
use Tequila\MongoDB\Exception\UnexpectedResultException;
use Tequila\MongoDB\Traits\CommandExecutorTrait;
class Client
{
use CommandExecutorTrait;
/**
* @var Manager
*/
private $manager;
/**
* @var ReadConcern
*/
private $readConcern;
/**
* @var ReadPreference
*/
private $readPreference;
/**
* @var WriteConcern
*/
private $writeConcern;
/**
* @param string $uri
* @param array $uriOptions
* @param array $driverOptions
*/
public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
{
$this->manager = new Manager($uri, $uriOptions, $driverOptions);
$this->readConcern = $this->manager->getReadConcern();
$this->readPreference = $this->manager->getReadPreference();
$this->writeConcern = $this->manager->getWriteConcern();
}
/**
* @param QueryListenerInterface $listener
*/
public function addQueryListener(QueryListenerInterface $listener)
{
$this->manager->addQueryListener($listener);
}
/**
* @param string $databaseName
* @param array $options
*
* @return array
*/
public function dropDatabase($databaseName, array $options = [])
{
$cursor = $this
->getCommandExecutor()
->executeCommand(
$this->manager,
$databaseName,
['dropDatabase' => 1],
$options
);
return $cursor->current();
}
/**
* @return Manager
*/
public function getManager()
{
return $this->manager;
}
/**
* @return array
*/
public function listDatabases()
{
$cursor = $this->manager->executeCommand(
'admin',
new SimpleCommand(['listDatabases' => 1]),
new ReadPreference(ReadPreference::RP_PRIMARY)
);
$cursor->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']);
$result = $cursor->current();
if (!isset($result['databases']) || !is_array($result['databases'])) {
throw new UnexpectedResultException(
'Command "listDatabases" did not return expected "databases" array.'
);
}
return $result['databases'];
}
/**
* @param string $databaseName
* @param string $collectionName
* @param array $options
*
* @return Collection
*/
public function selectCollection($databaseName, $collectionName, array $options = [])
{
return new Collection($this->manager, $databaseName, $collectionName, $options);
}
/**
* @param string $databaseName
* @param $options
*
* @return Database
*/
public function selectDatabase($databaseName, array $options = [])
{
return new Database($this->manager, $databaseName, $options);
}
}