1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pouzor\MongoDBBundle\DocumentManager; |
4
|
|
|
|
5
|
|
|
use MongoDB\Client; |
6
|
|
|
use MongoDB\Database; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\NullLogger; |
9
|
|
|
use Pouzor\MongoDBBundle\Constants\Query; |
10
|
|
|
use Pouzor\MongoDBBundle\Repository\Repository; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DocumentManager |
14
|
|
|
* @package Pouzor\MongoDBBundle\DocumentManager |
15
|
|
|
*/ |
16
|
|
|
class DocumentManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Database |
25
|
|
|
*/ |
26
|
|
|
private $database; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Repository[] |
30
|
|
|
*/ |
31
|
|
|
private $repositories = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $configuration = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $configuration |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
15 |
|
public function __construct(array $configuration = array(), LoggerInterface $logger = null) |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* host: %mongo_host% |
51
|
|
|
* port: %mongo_port% |
52
|
|
|
* db: %mongo_database% |
53
|
|
|
* password: %mongo_password% |
54
|
|
|
* username: %mongo_user% |
55
|
|
|
* schema: "%kernel.root_dir%/config/mongo/default.yml" |
56
|
|
|
* options: ~ |
57
|
|
|
*/ |
58
|
|
|
|
59
|
15 |
|
$this->validateConfiguration($configuration); |
60
|
|
|
|
61
|
15 |
|
$this->configuration = $configuration; |
62
|
|
|
|
63
|
15 |
|
if ($configuration['username']) { |
64
|
|
|
$dsn = sprintf( |
65
|
|
|
'mongodb://%s:%s@%s/%s', |
66
|
|
|
$configuration['username'], |
67
|
|
|
$configuration['password'], |
68
|
|
|
$configuration['host'], |
69
|
|
|
$configuration['db'] |
70
|
|
|
); |
71
|
|
|
} else { |
72
|
15 |
|
$dsn = sprintf( |
73
|
15 |
|
'mongodb://%s/%s', |
74
|
15 |
|
$configuration['host'], |
75
|
15 |
|
$configuration['db'] |
76
|
15 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
15 |
|
if (isset($configuration['options']['replicaSet']) && $configuration['options']['replicaSet']) { |
80
|
|
|
$dsn .= "?replicaSet=".$configuration['options']['replicaSet']; |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
$this->client = new Client( |
84
|
15 |
|
$dsn, $configuration['options'], [ |
85
|
|
|
'typeMap' => [ |
86
|
15 |
|
'root' => 'array', |
87
|
|
|
'document' => 'array' |
88
|
15 |
|
], |
89
|
|
|
] |
90
|
15 |
|
); |
91
|
|
|
|
92
|
15 |
|
$this->database = $this->client->selectDatabase($configuration['db']); |
93
|
|
|
|
94
|
15 |
|
$this->logger = $logger ?: new NullLogger(); |
95
|
15 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $name |
99
|
|
|
* @return Repository |
100
|
|
|
*/ |
101
|
15 |
|
public function getRepository($name) |
102
|
|
|
{ |
103
|
15 |
|
if (!array_key_exists($name, $this->repositories)) { |
104
|
|
|
/** |
105
|
|
|
* lazy creation |
106
|
|
|
*/ |
107
|
15 |
|
$repo = new Repository( |
108
|
15 |
|
$name, |
109
|
|
|
$this |
110
|
15 |
|
); |
111
|
|
|
|
112
|
15 |
|
if (isset($this->configuration['schema'][$name]['indexes'])) { |
113
|
15 |
|
$repo->setIndexes($this->configuration['schema'][$name]['indexes']); |
114
|
15 |
|
} |
115
|
|
|
|
116
|
15 |
|
$this->repositories[$name] = $repo; |
117
|
15 |
|
} |
118
|
|
|
|
119
|
15 |
|
return $this->repositories[$name]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $collection |
124
|
|
|
* @param $id |
125
|
|
|
* @param array $options |
126
|
|
|
* @return null|object |
127
|
|
|
*/ |
128
|
1 |
|
public function find($collection, $id, array $options = []) |
129
|
|
|
{ |
130
|
1 |
|
return $this->getRepository($collection)->find($id, $options); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param $collectionName |
135
|
|
|
* @param array $filter |
136
|
|
|
* @return \MongoDB\DeleteResult |
137
|
|
|
*/ |
138
|
14 |
|
public function removeAll($collectionName, array $filter = []) |
139
|
|
|
{ |
140
|
14 |
|
return $this->getRepository($collectionName)->deleteMany( |
141
|
14 |
|
$filter, |
142
|
|
|
[ |
143
|
|
|
Query::NO_CURSOR_TIMEOUT |
144
|
14 |
|
] |
145
|
14 |
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* |
150
|
|
|
*/ |
151
|
|
|
public function createIndexes() |
152
|
|
|
{ |
153
|
|
|
// TODO |
154
|
|
|
throw new \Exception('Not implemented yet'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $configuration |
159
|
|
|
* @throws \Exception |
160
|
|
|
*/ |
161
|
15 |
|
private function validateConfiguration($configuration) |
162
|
|
|
{ |
163
|
|
|
/** |
164
|
|
|
* validating keys |
165
|
|
|
*/ |
166
|
15 |
|
foreach (['host', 'db', 'password', 'username', 'schema', 'options'] as $key) { |
167
|
15 |
|
if (!array_key_exists($key, $configuration)) { |
168
|
|
|
throw new \Exception(sprintf('%s must be present in configuration', $key)); |
169
|
|
|
} |
170
|
15 |
|
} |
171
|
|
|
|
172
|
15 |
|
foreach (['host', 'db'] as $key) { |
173
|
15 |
|
if (!is_string($configuration[$key]) || empty($configuration[$key])) { |
174
|
|
|
throw new \Exception(sprintf('%s must be a not empty string', $key)); |
175
|
|
|
} |
176
|
15 |
|
} |
177
|
|
|
|
178
|
15 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Build all indexes |
182
|
|
|
* |
183
|
|
|
* @param null $callback |
184
|
|
|
*/ |
185
|
1 |
|
public function buildIndexes($rebuild = false, $callback = null) |
186
|
|
|
{ |
187
|
1 |
|
foreach ($this->configuration['schema'] as $col => $conf) { |
188
|
1 |
|
$this->getRepository($col)->buildIndexes($rebuild, $callback); |
189
|
1 |
|
} |
190
|
1 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return Database |
194
|
|
|
*/ |
195
|
15 |
|
public function getDatabase() { |
196
|
|
|
|
197
|
15 |
|
return $this->database; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return LoggerInterface|NullLogger |
202
|
|
|
*/ |
203
|
15 |
|
public function getLogger() { |
204
|
|
|
|
205
|
15 |
|
return $this->logger; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|