|
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
|
14 |
|
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
|
14 |
|
$this->validateConfiguration($configuration); |
|
60
|
|
|
|
|
61
|
14 |
|
$this->configuration = $configuration; |
|
62
|
|
|
|
|
63
|
14 |
|
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
|
14 |
|
$dsn = sprintf( |
|
73
|
14 |
|
'mongodb://%s/%s', |
|
74
|
14 |
|
$configuration['host'], |
|
75
|
14 |
|
$configuration['db'] |
|
76
|
14 |
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
14 |
|
if (isset($configuration['options']['replicaSet']) && $configuration['options']['replicaSet']) { |
|
80
|
|
|
$dsn .= "?replicaSet=".$configuration['options']['replicaSet']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
14 |
|
$this->client = new Client( |
|
84
|
14 |
|
$dsn, $configuration['options'], [ |
|
85
|
|
|
'typeMap' => [ |
|
86
|
14 |
|
'root' => 'array', |
|
87
|
|
|
'document' => 'array' |
|
88
|
14 |
|
], |
|
89
|
|
|
] |
|
90
|
14 |
|
); |
|
91
|
|
|
|
|
92
|
14 |
|
$this->database = $this->client->selectDatabase($configuration['db']); |
|
93
|
|
|
|
|
94
|
14 |
|
$this->logger = $logger ?: new NullLogger(); |
|
95
|
14 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $name |
|
99
|
|
|
* @return Repository |
|
100
|
|
|
*/ |
|
101
|
14 |
|
public function getRepository($name) |
|
102
|
|
|
{ |
|
103
|
14 |
|
if (!array_key_exists($name, $this->repositories)) { |
|
104
|
|
|
/** |
|
105
|
|
|
* lazy creation |
|
106
|
|
|
*/ |
|
107
|
14 |
|
$repo = new Repository( |
|
108
|
14 |
|
$name, |
|
109
|
|
|
$this |
|
110
|
14 |
|
); |
|
111
|
|
|
|
|
112
|
14 |
|
if (isset($this->configuration['schema'][$name]['indexes'])) { |
|
113
|
14 |
|
$repo->setIndexes($this->configuration['schema'][$name]['indexes']); |
|
114
|
14 |
|
} |
|
115
|
|
|
|
|
116
|
14 |
|
$this->repositories[$name] = $repo; |
|
117
|
14 |
|
} |
|
118
|
|
|
|
|
119
|
14 |
|
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
|
14 |
|
private function validateConfiguration($configuration) |
|
162
|
|
|
{ |
|
163
|
|
|
/** |
|
164
|
|
|
* validating keys |
|
165
|
|
|
*/ |
|
166
|
14 |
|
foreach (['host', 'db', 'password', 'username', 'schema', 'options'] as $key) { |
|
167
|
14 |
|
if (!array_key_exists($key, $configuration)) { |
|
168
|
|
|
throw new \Exception(sprintf('%s must be present in configuration', $key)); |
|
169
|
|
|
} |
|
170
|
14 |
|
} |
|
171
|
|
|
|
|
172
|
14 |
|
foreach (['host', 'db'] as $key) { |
|
173
|
14 |
|
if (!is_string($configuration[$key]) || empty($configuration[$key])) { |
|
174
|
|
|
throw new \Exception(sprintf('%s must be a not empty string', $key)); |
|
175
|
|
|
} |
|
176
|
14 |
|
} |
|
177
|
|
|
|
|
178
|
14 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Build all indexes |
|
182
|
|
|
* |
|
183
|
|
|
* @param null $callback |
|
184
|
|
|
*/ |
|
185
|
|
|
public function buildIndexes($rebuild = false, $callback = null) |
|
186
|
|
|
{ |
|
187
|
|
|
foreach ($this->configuration['schema'] as $col => $conf) { |
|
188
|
|
|
$this->getRepository($col)->buildIndexes($rebuild, $callback); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return Database |
|
194
|
|
|
*/ |
|
195
|
14 |
|
public function getDatabase() { |
|
196
|
|
|
|
|
197
|
14 |
|
return $this->database; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return LoggerInterface|NullLogger |
|
202
|
|
|
*/ |
|
203
|
14 |
|
public function getLogger() { |
|
204
|
|
|
|
|
205
|
14 |
|
return $this->logger; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|