1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
MIT License |
4
|
|
|
Copyright (c) 2010 - 2018 Peter Petermann |
5
|
|
|
|
6
|
|
|
Permission is hereby granted, free of charge, to any person |
7
|
|
|
obtaining a copy of this software and associated documentation |
8
|
|
|
files (the "Software"), to deal in the Software without |
9
|
|
|
restriction, including without limitation the rights to use, |
10
|
|
|
copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
copies of the Software, and to permit persons to whom the |
12
|
|
|
Software is furnished to do so, subject to the following |
13
|
|
|
conditions: |
14
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be |
16
|
|
|
included in all copies or substantial portions of the Software. |
17
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
20
|
|
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
22
|
|
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
23
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
25
|
|
|
OTHER DEALINGS IN THE SOFTWARE. |
26
|
|
|
|
27
|
|
|
*/ |
28
|
|
|
namespace King23\Mongo; |
29
|
|
|
|
30
|
|
|
use Psr\Container\ContainerInterface; |
31
|
|
|
|
32
|
|
|
class MongoService implements MongoServiceInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var \MongoDB |
36
|
|
|
*/ |
37
|
|
|
protected $dbConnection; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClassMapInterface |
41
|
|
|
*/ |
42
|
|
|
protected $classMap; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ContainerInterface |
46
|
|
|
*/ |
47
|
|
|
protected $container; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param \MongoDB $dbConnection |
51
|
|
|
* @param ClassMapInterface $classMap |
52
|
|
|
* @param ContainerInterface $container |
53
|
|
|
*/ |
54
|
|
|
public function __construct(\MongoDB $dbConnection, ClassMapInterface $classMap, ContainerInterface $container) |
55
|
|
|
{ |
56
|
|
|
$this->dbConnection = $dbConnection; |
57
|
|
|
$this->classMap = $classMap; |
58
|
|
|
$this->container = $container; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return \MongoDB |
63
|
|
|
*/ |
64
|
|
|
public function getDB() |
65
|
|
|
{ |
66
|
|
|
return $this->dbConnection; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* convenience method to retrieve object by id, should be used in |
71
|
|
|
* public static method by the derived class |
72
|
|
|
* |
73
|
|
|
* @param string $collection |
74
|
|
|
* @param string $mongoId |
75
|
|
|
* @return MongoObject |
76
|
|
|
* @throws \MongoException |
77
|
|
|
*/ |
78
|
|
|
public function getById($collection, $mongoId) |
79
|
|
|
{ |
80
|
|
|
return $this->getByCriteria($collection, ['_id' => new \MongoId($mongoId)]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* convenience method to retrieve object by criteria, should be used in |
85
|
|
|
* public static method by the derived class |
86
|
|
|
* |
87
|
|
|
* @param string $collection |
88
|
|
|
* @param array $criteria |
89
|
|
|
* @return MongoObject |
90
|
|
|
* @throws \MongoException |
91
|
|
|
*/ |
92
|
|
|
public function getByCriteria($collection, $criteria) |
93
|
|
|
{ |
94
|
|
|
if ($data = $this->findOne($collection, $criteria)) { |
95
|
|
|
|
96
|
|
|
/** @var MongoObject $obj */ |
97
|
|
|
$obj = $this->container->get( |
98
|
|
|
$this->classMap->getClassForResult($collection, $data) |
99
|
|
|
); |
100
|
|
|
$obj->setCollection($collection); |
101
|
|
|
$obj->loadFromArray($data); |
102
|
|
|
|
103
|
|
|
return $obj; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $collection |
111
|
|
|
* @param array $criteria |
112
|
|
|
* @param array $fields |
113
|
|
|
* @return Result |
114
|
|
|
* @throws \Exception |
115
|
|
|
*/ |
116
|
|
|
public function find($collection, array $criteria, array $fields = []) |
117
|
|
|
{ |
118
|
|
|
$data = $this->dbConnection->selectCollection($collection)->find($criteria, $fields); |
119
|
|
|
|
120
|
|
|
/** @var Result $result */ |
121
|
|
|
$result = $this->container->get(Result::class); |
122
|
|
|
$result->setCollection($collection); |
123
|
|
|
$result->setCursor($data); |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Run Aggregation through the Aggregation Pipeline |
130
|
|
|
* |
131
|
|
|
* @param string $collection |
132
|
|
|
* @param array $pipeline |
133
|
|
|
* @param array $options |
134
|
|
|
* @return mixed |
135
|
|
|
* @throws Exception |
136
|
|
|
*/ |
137
|
|
|
public function aggregate($collection, array $pipeline, $options = []) |
138
|
|
|
{ |
139
|
|
|
$data = $this->dbConnection->selectCollection($collection)->aggregate($pipeline); |
140
|
|
|
if ($data['ok'] != 1) { |
141
|
|
|
throw new Exception("Tool Aggregation Error: ".$data['errmsg'], $data['code']); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $data['result']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $collection |
149
|
|
|
* @param string $fieldname |
150
|
|
|
* @param array $criteria |
151
|
|
|
* @return array |
152
|
|
|
* @throws \Exception |
153
|
|
|
*/ |
154
|
|
|
public function distinct($collection, $fieldname, array $criteria = []) |
155
|
|
|
{ |
156
|
|
|
return $this->dbConnection->selectCollection($collection)->distinct($fieldname, $criteria); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* returns the first found matching document |
161
|
|
|
* |
162
|
|
|
* @param string $collection |
163
|
|
|
* @param array $criteria |
164
|
|
|
* @param array $fields |
165
|
|
|
* @return array |
166
|
|
|
* @throws \Exception |
167
|
|
|
*/ |
168
|
|
|
public function findOne($collection, array $criteria, array $fields = []) |
169
|
|
|
{ |
170
|
|
|
return $this->dbConnection->selectCollection($collection)->findOne($criteria, $fields); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* conveniant method to create new instances |
175
|
|
|
* @param string $collection |
176
|
|
|
* @return MongoObject |
177
|
|
|
* @throws \MongoException |
178
|
|
|
*/ |
179
|
|
|
public function newObject($collection) |
180
|
|
|
{ |
181
|
|
|
/** @var MongoObject $obj */ |
182
|
|
|
$obj =$this->container->get($this->classMap->getClassForResult($collection, [])); |
183
|
|
|
$obj->setCollection($collection); |
184
|
|
|
|
185
|
|
|
return $obj; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $input name of the collection from which to map/reduce |
190
|
|
|
* @param string $output name of the collection to write |
191
|
|
|
* @param string $map map method |
192
|
|
|
* @param string $reduce reduce method |
193
|
|
|
* @param array $query criteria to apply |
194
|
|
|
* @param array $additional |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function mapReduce($input, $output, $map, $reduce, $query = null, $additional = array()) |
198
|
|
|
{ |
199
|
|
|
$mongo = $this->getDB(); |
200
|
|
|
$map = new \MongoCode($map); |
201
|
|
|
$reduce = new \MongoCode($reduce); |
202
|
|
|
$cmd = array( |
203
|
|
|
"mapreduce" => $input, |
204
|
|
|
"map" => $map, |
205
|
|
|
"reduce" => $reduce, |
206
|
|
|
"out" => $output |
207
|
|
|
); |
208
|
|
|
// add filter query |
209
|
|
|
if (!is_null($query)) { |
210
|
|
|
$cmd['query'] = $query; |
211
|
|
|
} |
212
|
|
|
$cmd = array_merge($cmd, $additional); |
213
|
|
|
|
214
|
|
|
// execute the mapreduce |
215
|
|
|
return $mongo->command($cmd); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|