1 | <?php |
||
25 | abstract class AbstractCursor |
||
26 | { |
||
27 | use ReadPreference; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $batchSize; |
||
33 | |||
34 | /** |
||
35 | * @var Collection |
||
36 | */ |
||
37 | protected $collection; |
||
38 | |||
39 | /** |
||
40 | * @var \MongoClient |
||
41 | */ |
||
42 | protected $connection; |
||
43 | |||
44 | /** |
||
45 | * @var Cursor |
||
46 | */ |
||
47 | protected $cursor; |
||
48 | |||
49 | /** |
||
50 | * @var \MongoDB\Database |
||
51 | */ |
||
52 | protected $db; |
||
53 | |||
54 | /** |
||
55 | * @var \IteratorIterator |
||
56 | */ |
||
57 | protected $iterator; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $ns; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $optionNames = [ |
||
68 | 'batchSize', |
||
69 | 'readPreference', |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * @return Cursor |
||
74 | */ |
||
75 | abstract protected function ensureCursor(); |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | abstract protected function getCursorInfo(); |
||
81 | |||
82 | /** |
||
83 | * Create a new cursor |
||
84 | * @link http://www.php.net/manual/en/mongocursor.construct.php |
||
85 | * @param \MongoClient $connection Database connection. |
||
86 | * @param string $ns Full name of database and collection. |
||
87 | */ |
||
88 | 39 | public function __construct(\MongoClient $connection, $ns) |
|
89 | { |
||
90 | 39 | $this->connection = $connection; |
|
91 | 39 | $this->ns = $ns; |
|
92 | |||
93 | 39 | $nsParts = explode('.', $ns); |
|
94 | 39 | $dbName = array_shift($nsParts); |
|
95 | 39 | $collectionName = implode('.', $nsParts); |
|
96 | |||
97 | 39 | $this->db = $connection->selectDB($dbName)->getDb(); |
|
98 | |||
99 | 39 | if ($collectionName) { |
|
100 | 31 | $this->collection = $connection->selectCollection($dbName, $collectionName)->getCollection(); |
|
101 | 31 | } |
|
102 | 39 | } |
|
103 | |||
104 | /** |
||
105 | * Returns the current element |
||
106 | * @link http://www.php.net/manual/en/mongocursor.current.php |
||
107 | * @return array |
||
108 | */ |
||
109 | 17 | public function current() |
|
110 | { |
||
111 | 17 | $document = $this->ensureIterator()->current(); |
|
112 | 17 | if ($document !== null) { |
|
113 | 17 | $document = TypeConverter::toLegacy($document); |
|
114 | 17 | } |
|
115 | |||
116 | 17 | return $document; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the current result's _id |
||
121 | * @link http://www.php.net/manual/en/mongocursor.key.php |
||
122 | * @return string The current result's _id as a string. |
||
123 | */ |
||
124 | 10 | public function key() |
|
128 | |||
129 | /** |
||
130 | * Advances the cursor to the next result |
||
131 | * @link http://www.php.net/manual/en/mongocursor.next.php |
||
132 | * @throws \MongoConnectionException |
||
133 | * @throws \MongoCursorTimeoutException |
||
134 | * @return void |
||
135 | */ |
||
136 | 17 | public function next() |
|
140 | |||
141 | /** |
||
142 | * Returns the cursor to the beginning of the result set |
||
143 | * @throws \MongoConnectionException |
||
144 | * @throws \MongoCursorTimeoutException |
||
145 | * @return void |
||
146 | */ |
||
147 | 35 | public function rewind() |
|
148 | { |
||
149 | // We can recreate the cursor to allow it to be rewound |
||
150 | 35 | $this->reset(); |
|
151 | 35 | $this->ensureIterator()->rewind(); |
|
152 | 33 | } |
|
153 | |||
154 | /** |
||
155 | * Checks if the cursor is reading a valid result. |
||
156 | * @link http://www.php.net/manual/en/mongocursor.valid.php |
||
157 | * @return boolean If the current result is not null. |
||
158 | */ |
||
159 | 33 | public function valid() |
|
163 | |||
164 | /** |
||
165 | * Limits the number of elements returned in one batch. |
||
166 | * |
||
167 | * @link http://docs.php.net/manual/en/mongocursor.batchsize.php |
||
168 | * @param int $batchSize The number of results to return per batch |
||
169 | * @return $this Returns this cursor. |
||
170 | */ |
||
171 | 1 | public function batchSize($batchSize) |
|
172 | { |
||
173 | 1 | $this->batchSize = $batchSize; |
|
174 | |||
175 | 1 | return $this; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Checks if there are documents that have not been sent yet from the database for this cursor |
||
180 | * @link http://www.php.net/manual/en/mongocursor.dead.php |
||
181 | * @return boolean Returns if there are more results that have not been sent to the client, yet. |
||
182 | */ |
||
183 | public function dead() |
||
187 | |||
188 | /** |
||
189 | * @return array |
||
190 | */ |
||
191 | 2 | public function info() |
|
195 | |||
196 | /** |
||
197 | * @link http://www.php.net/manual/en/mongocursor.setreadpreference.php |
||
198 | * @param string $readPreference |
||
199 | * @param array $tags |
||
200 | * @return $this Returns this cursor. |
||
201 | */ |
||
202 | 39 | public function setReadPreference($readPreference, $tags = null) |
|
203 | { |
||
204 | 39 | $this->setReadPreferenceFromParameters($readPreference, $tags); |
|
205 | |||
206 | 39 | return $this; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Sets a client-side timeout for this query |
||
211 | * @link http://www.php.net/manual/en/mongocursor.timeout.php |
||
212 | * @param int $ms The number of milliseconds for the cursor to wait for a response. By default, the cursor will wait forever. |
||
213 | * @return $this Returns this cursor |
||
214 | */ |
||
215 | public function timeout($ms) |
||
219 | |||
220 | /** |
||
221 | * Applies all options set on the cursor, overwriting any options that have already been set |
||
222 | * |
||
223 | * @param array $optionNames Array of option names to be applied (will be read from properties) |
||
224 | * @return array |
||
225 | */ |
||
226 | 36 | protected function getOptions($optionNames = null) |
|
227 | { |
||
228 | 36 | $options = []; |
|
229 | |||
230 | 36 | if ($optionNames === null) { |
|
231 | 35 | $optionNames = $this->optionNames; |
|
232 | 35 | } |
|
233 | |||
234 | 36 | foreach ($optionNames as $option) { |
|
235 | 36 | $converter = 'convert' . ucfirst($option); |
|
236 | 36 | $value = method_exists($this, $converter) ? $this->$converter() : $this->$option; |
|
237 | |||
238 | 36 | if ($value === null) { |
|
239 | 36 | continue; |
|
240 | } |
||
241 | |||
242 | 36 | $options[$option] = $value; |
|
243 | 36 | } |
|
244 | |||
245 | 36 | return $options; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return \IteratorIterator |
||
250 | */ |
||
251 | 35 | protected function ensureIterator() |
|
252 | { |
||
253 | 35 | if ($this->iterator === null) { |
|
254 | 35 | $this->iterator = new \IteratorIterator($this->ensureCursor()); |
|
255 | 33 | } |
|
256 | |||
257 | 33 | return $this->iterator; |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * @throws \MongoCursorException |
||
262 | */ |
||
263 | 15 | protected function errorIfOpened() |
|
264 | { |
||
265 | 15 | if ($this->cursor === null) { |
|
266 | 15 | return; |
|
267 | } |
||
268 | |||
269 | throw new \MongoCursorException('cannot modify cursor after beginning iteration.'); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @return array |
||
274 | */ |
||
275 | 2 | protected function getIterationInfo() |
|
276 | { |
||
277 | $iterationInfo = [ |
||
278 | 2 | 'started_iterating' => $this->cursor !== null, |
|
279 | 2 | ]; |
|
280 | |||
281 | 2 | if ($this->cursor !== null) { |
|
282 | 2 | switch ($this->cursor->getServer()->getType()) { |
|
283 | 2 | case \MongoDB\Driver\Server::TYPE_RS_ARBITER: |
|
284 | $typeString = 'ARBITER'; |
||
285 | break; |
||
286 | 2 | case \MongoDB\Driver\Server::TYPE_MONGOS: |
|
287 | $typeString = 'MONGOS'; |
||
288 | break; |
||
289 | 2 | case \MongoDB\Driver\Server::TYPE_RS_PRIMARY: |
|
290 | $typeString = 'PRIMARY'; |
||
291 | break; |
||
292 | 2 | case \MongoDB\Driver\Server::TYPE_RS_SECONDARY: |
|
293 | $typeString = 'SECONDARY'; |
||
294 | break; |
||
295 | 2 | default: |
|
296 | 2 | $typeString = 'STANDALONE'; |
|
297 | 2 | } |
|
298 | |||
299 | $iterationInfo += [ |
||
300 | 2 | 'id' => (string) $this->cursor->getId(), |
|
301 | 2 | 'at' => null, // @todo Complete info for cursor that is iterating |
|
302 | 2 | 'numReturned' => null, // @todo Complete info for cursor that is iterating |
|
303 | 2 | 'server' => null, // @todo Complete info for cursor that is iterating |
|
304 | 2 | 'host' => $this->cursor->getServer()->getHost(), |
|
305 | 2 | 'port' => $this->cursor->getServer()->getPort(), |
|
306 | 2 | 'connection_type_desc' => $typeString, |
|
307 | ]; |
||
308 | 2 | } |
|
309 | |||
310 | 2 | return $iterationInfo; |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * @throws \Exception |
||
315 | */ |
||
316 | protected function notImplemented() |
||
320 | |||
321 | /** |
||
322 | * Clears the cursor |
||
323 | * |
||
324 | * This is generic but implemented as protected since it's only exposed in MongoCursor |
||
325 | */ |
||
326 | 35 | protected function reset() |
|
327 | { |
||
328 | 35 | $this->cursor = null; |
|
329 | 35 | $this->iterator = null; |
|
331 | } |
||
332 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.