Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Pool often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pool, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Pool extends Client { |
||
| 18 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
| 19 | |||
| 20 | /* Codes of operations */ |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @TODO DESCR |
||
|
|
|||
| 24 | */ |
||
| 25 | const OP_REPLY = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @TODO DESCR |
||
| 29 | */ |
||
| 30 | const OP_MSG = 1000; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @TODO DESCR |
||
| 34 | */ |
||
| 35 | const OP_UPDATE = 2001; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @TODO DESCR |
||
| 39 | */ |
||
| 40 | const OP_INSERT = 2002; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @TODO DESCR |
||
| 44 | */ |
||
| 45 | const OP_QUERY = 2004; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @TODO DESCR |
||
| 49 | */ |
||
| 50 | const OP_GETMORE = 2005; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @TODO DESCR |
||
| 54 | */ |
||
| 55 | const OP_DELETE = 2006; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @TODO DESCR |
||
| 59 | */ |
||
| 60 | const OP_KILL_CURSORS = 2007; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array Objects of MongoClientAsyncCollection |
||
| 64 | */ |
||
| 65 | public $collections = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string Current database |
||
| 69 | */ |
||
| 70 | public $dbname = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Connection Holds last used MongoClientAsyncConnection object |
||
| 74 | */ |
||
| 75 | public $lastRequestConnection; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var object Object of MemcacheClient |
||
| 79 | */ |
||
| 80 | public $cache; |
||
| 81 | |||
| 82 | protected $safeMode = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Setting default config options |
||
| 86 | * Overriden from AppInstance::getConfigDefaults |
||
| 87 | * @return array|bool |
||
| 88 | */ |
||
| 89 | protected function getConfigDefaults() { |
||
| 90 | return [ |
||
| 91 | /* [string|array] default server list */ |
||
| 92 | 'servers' => 'tcp://127.0.0.1', |
||
| 93 | |||
| 94 | /* [integer] default port */ |
||
| 95 | 'port' => 27017, |
||
| 96 | |||
| 97 | /* [integer] maxconnperserv */ |
||
| 98 | 'maxconnperserv' => 32, |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @TODO |
||
| 104 | * @param array $o |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public static function safeModeEnc(&$o) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Sets default database name |
||
| 120 | * @param string $name Database name |
||
| 121 | * @return boolean Success |
||
| 122 | */ |
||
| 123 | public function selectDB($name) { |
||
| 124 | $this->dbname = $name; |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Generates auth. key |
||
| 131 | * @param string $username Username |
||
| 132 | * @param string $password Password |
||
| 133 | * @param string $nonce Nonce |
||
| 134 | * @return string MD5 hash |
||
| 135 | */ |
||
| 136 | public static function getAuthKey($username, $password, $nonce) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Adds mongo server |
||
| 142 | * @param string $url URL |
||
| 143 | * @param integer $weight Weight |
||
| 144 | * @param mixed $mock @deprecated |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function addServer($url, $weight = NULL, $mock = null) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the key |
||
| 153 | * @param integer $opcode Opcode (see constants above) |
||
| 154 | * @param string $data Data |
||
| 155 | * @param boolean $reply Is an answer expected? |
||
| 156 | * @param Connection $conn Connection. Optional |
||
| 157 | * @param callable $sentcb Sent callback |
||
| 158 | * @callback $sentcb ( ) |
||
| 159 | * @throws ConnectionFinished |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function request($opcode, $data, $reply = false, $conn = null, $sentcb = null) { |
||
| 163 | $cb = $this->requestCbProducer($opcode, $data, $reply, $sentcb); |
||
| 164 | if (is_object($conn) && ($conn instanceof Connection)) { |
||
| 165 | if ($conn->isFinished()) { |
||
| 166 | throw new ConnectionFinished; |
||
| 167 | } |
||
| 168 | $cb($conn); |
||
| 169 | } |
||
| 170 | elseif ($this->finished) { |
||
| 171 | call_user_func($cb, false); |
||
| 172 | } |
||
| 173 | else { |
||
| 174 | $this->getConnectionRR($cb); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @TODO DESCR |
||
| 180 | * @param integer $opcode Opcode (see constants above) |
||
| 181 | * @param string $data Data |
||
| 182 | * @param boolean $reply Is an answer expected? |
||
| 183 | * @param callable $sentcb Sent callback |
||
| 184 | * @callback $sentcb ( ) |
||
| 185 | * @return callable |
||
| 186 | */ |
||
| 187 | protected function requestCbProducer($opcode, $data, $reply = false, $sentcb = null) { |
||
| 188 | return function ($conn) use ($opcode, $data, $reply, $sentcb) { |
||
| 189 | if (!$conn || $conn->isFinished()) { |
||
| 190 | if ($this->finished) { |
||
| 191 | if ($sentcb !== null) { |
||
| 192 | call_user_func($sentcb, false); |
||
| 193 | } |
||
| 194 | } else { |
||
| 195 | $this->getConnectionRR($this->requestCbProducer($opcode, $data, $reply, $sentcb)); |
||
| 196 | } |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | $reqId = ++$conn->lastReqId; |
||
| 200 | $this->lastRequestConnection = $conn; |
||
| 201 | $conn->write(pack('VVVV', strlen($data) + 16, $reqId, 0, $opcode)); |
||
| 202 | $conn->write($data); |
||
| 203 | if ($reply) { |
||
| 204 | $conn->setFree(false); |
||
| 205 | } |
||
| 206 | if ($sentcb !== null) { |
||
| 207 | call_user_func($sentcb, $conn, $reqId); |
||
| 208 | } |
||
| 209 | }; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Finds objects in collection and fires callback when got all objects |
||
| 214 | * @param array $p Hash of properties (offset, limit, opts, tailable, await, where, col, fields, sort, hint, explain, snapshot, orderby, parse_oplog) |
||
| 215 | * @param callable $cb Callback called when response received |
||
| 216 | * @callback $cb ( ) |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function findAll($p, $cb) { |
||
| 220 | $this->find($p, function($cursor) use ($cb) { |
||
| 221 | if (!$cursor->isFinished()) { |
||
| 222 | $cursor->getMore(); |
||
| 223 | } else { |
||
| 224 | call_user_func($cb, $cursor); |
||
| 225 | } |
||
| 226 | }); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Finds objects in collection |
||
| 231 | * @param array $p Hash of properties (offset, limit, opts, tailable, await, where, col, fields, sort, hint, explain, snapshot, orderby, parse_oplog) |
||
| 232 | * @param callable $cb Callback called when response received |
||
| 233 | * @callback $cb ( ) |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | public function find($p, $cb) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Finds one object in collection |
||
| 331 | * @param array $p Hash of properties (offset, opts, where, col, fields, sort, hint, explain, snapshot, orderby, parse_oplog) |
||
| 332 | * @param callable $cb Callback called when response received |
||
| 333 | * @callback $cb ( ) |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | public function findOne($p, $cb) { |
||
| 337 | if (isset($p['cachekey'])) { |
||
| 338 | $db = $this; |
||
| 339 | $this->cache->get($p['cachekey'], function ($r) use ($db, $p, $cb) { |
||
| 340 | if ($r->result !== NULL) { |
||
| 341 | call_user_func($cb, bson_decode($r->result)); |
||
| 342 | } |
||
| 343 | else { |
||
| 344 | unset($p['cachekey']); |
||
| 345 | $db->findOne($p, $cb); |
||
| 346 | } |
||
| 347 | }); |
||
| 348 | |||
| 349 | return; |
||
| 350 | } |
||
| 351 | if (!isset($p['where'])) { |
||
| 352 | $p['where'] = []; |
||
| 353 | } |
||
| 354 | |||
| 355 | $this->_params($p); |
||
| 356 | |||
| 357 | $o = []; |
||
| 358 | $s = false; |
||
| 359 | |||
| 360 | View Code Duplication | foreach ($p as $k => $v) { |
|
| 361 | if ( |
||
| 362 | ($k === 'sort') |
||
| 363 | || ($k === 'hint') |
||
| 364 | || ($k === 'explain') |
||
| 365 | || ($k === 'snapshot') |
||
| 366 | ) { |
||
| 367 | if (!$s) { |
||
| 368 | $s = true; |
||
| 369 | } |
||
| 370 | |||
| 371 | if ($k === 'sort') { |
||
| 372 | $o['orderby'] = $v; |
||
| 373 | } |
||
| 374 | elseif ($k === 'parse_oplog') { |
||
| 375 | } |
||
| 376 | elseif ($k === 'rp') { |
||
| 377 | $o['$readPreference'] = $v; |
||
| 378 | } |
||
| 379 | else { |
||
| 380 | $o[$k] = $v; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 | if (empty($o['orderby'])) { |
||
| 385 | unset($o['orderby']); |
||
| 386 | } |
||
| 387 | |||
| 388 | if ($s) { |
||
| 389 | $o['query'] = $p['where']; |
||
| 390 | } |
||
| 391 | else { |
||
| 392 | $o = $p['where']; |
||
| 393 | } |
||
| 394 | $cb = CallbackWrapper::wrap($cb); |
||
| 395 | if ($this->safeMode) { |
||
| 396 | static::safeModeEnc($o); |
||
| 397 | } |
||
| 398 | try { |
||
| 399 | $this->request(self::OP_QUERY, |
||
| 400 | pack('V', $p['opts']) |
||
| 401 | . $p['col'] . "\x00" |
||
| 402 | . pack('VV', $p['offset'], -1) |
||
| 403 | . bson_encode($o) |
||
| 404 | . (isset($p['fields']) ? bson_encode($p['fields']) : '') |
||
| 405 | View Code Duplication | , true, null, function($conn, $reqId = null) use ($p, $cb) { |
|
| 406 | if (!$conn) { |
||
| 407 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 408 | return; |
||
| 409 | } |
||
| 410 | $conn->requests[$reqId] = [$p['col'], $cb, true]; |
||
| 411 | }); |
||
| 412 | } catch (\MongoException $e) { |
||
| 413 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 414 | View Code Duplication | if ($cb !== null) { |
|
| 415 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $o, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Counts objects in collection |
||
| 422 | * @param array $p Hash of properties (offset, limit, opts, where, col) |
||
| 423 | * @param callable $cb Callback called when response received |
||
| 424 | * @callback $cb ( ) |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | public function findCount($p, $cb) { |
||
| 428 | if (!isset($p['offset'])) { |
||
| 429 | $p['offset'] = 0; |
||
| 430 | } |
||
| 431 | |||
| 432 | if (!isset($p['limit'])) { |
||
| 433 | $p['limit'] = -1; |
||
| 434 | } |
||
| 435 | |||
| 436 | if (!isset($p['opts'])) { |
||
| 437 | $p['opts'] = 0; |
||
| 438 | } |
||
| 439 | |||
| 440 | if (!isset($p['where'])) { |
||
| 441 | $p['where'] = []; |
||
| 442 | } |
||
| 443 | |||
| 444 | View Code Duplication | if (strpos($p['col'], '.') === false) { |
|
| 445 | $p['col'] = $this->dbname . '.' . $p['col']; |
||
| 446 | } |
||
| 447 | |||
| 448 | $e = explode('.', $p['col'], 2); |
||
| 449 | |||
| 450 | $query = [ |
||
| 451 | 'count' => $e[1], |
||
| 452 | 'query' => $p['where'], |
||
| 453 | 'fields' => ['_id' => 1], |
||
| 454 | ]; |
||
| 455 | |||
| 456 | View Code Duplication | if (isset($p[$k = 'rp'])) { |
|
| 457 | $v = $p[$k]; |
||
| 458 | if (is_string($v)) { |
||
| 459 | $v = ['mode' => $v]; |
||
| 460 | } |
||
| 461 | $query['$readPreference'] = $v; |
||
| 462 | } |
||
| 463 | |||
| 464 | View Code Duplication | if (is_string($p['where'])) { |
|
| 465 | $query['where'] = new \MongoCode($p['where']); |
||
| 466 | } |
||
| 467 | elseif ( |
||
| 468 | is_object($p['where']) |
||
| 469 | || sizeof($p['where']) |
||
| 470 | ) { |
||
| 471 | $query['query'] = $p['where']; |
||
| 472 | } |
||
| 473 | $cb = CallbackWrapper::wrap($cb); |
||
| 474 | if ($this->safeMode) { |
||
| 475 | static::safeModeEnc($query); |
||
| 476 | } |
||
| 477 | try { |
||
| 478 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 479 | . $e[0] . '.$cmd' . "\x00" |
||
| 480 | . pack('VV', $p['offset'], $p['limit']) |
||
| 481 | . bson_encode($query) |
||
| 482 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 483 | if (!$conn) { |
||
| 484 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 485 | return; |
||
| 486 | } |
||
| 487 | $conn->requests[$reqId] = [$p['col'], $cb, true]; |
||
| 488 | }); |
||
| 489 | } catch (\MongoException $e) { |
||
| 490 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 491 | View Code Duplication | if ($cb !== null) { |
|
| 492 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 493 | } |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Sends authenciation packet |
||
| 499 | * @param array $p Hash of properties (dbname, user, password, nonce) |
||
| 500 | * @param callable $cb Callback called when response received |
||
| 501 | * @callback $cb ( ) |
||
| 502 | * @return void |
||
| 503 | */ |
||
| 504 | public function auth($p, $cb) { |
||
| 505 | if (!isset($p['opts'])) { |
||
| 506 | $p['opts'] = 0; |
||
| 507 | } |
||
| 508 | |||
| 509 | $query = [ |
||
| 510 | 'authenticate' => 1, |
||
| 511 | 'user' => $p['user'], |
||
| 512 | 'nonce' => $p['nonce'], |
||
| 513 | 'key' => self::getAuthKey($p['user'], $p['password'], $p['nonce']), |
||
| 514 | ]; |
||
| 515 | if ($this->safeMode) { |
||
| 516 | static::safeModeEnc($query); |
||
| 517 | } |
||
| 518 | try { |
||
| 519 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 520 | . $p['dbname'] . '.$cmd' . "\x00" |
||
| 521 | . pack('VV', 0, -1) |
||
| 522 | . bson_encode($query) |
||
| 523 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 524 | if (!$conn) { |
||
| 525 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 526 | return; |
||
| 527 | } |
||
| 528 | $conn->requests[$reqId] = [$p['dbname'], $cb, true]; |
||
| 529 | }); |
||
| 530 | } catch (\MongoException $e) { |
||
| 531 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 532 | View Code Duplication | if ($cb !== null) { |
|
| 533 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Sends request of nonce |
||
| 540 | * @param array $p Hash of properties |
||
| 541 | * @param callable $cb Callback called when response received |
||
| 542 | * @callback $cb ( ) |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | public function getNonce($p, $cb) { |
||
| 546 | if (!isset($p['opts'])) { |
||
| 547 | $p['opts'] = 0; |
||
| 548 | } |
||
| 549 | |||
| 550 | $query = [ |
||
| 551 | 'getnonce' => 1, |
||
| 552 | ]; |
||
| 553 | $cb = CallbackWrapper::wrap($cb); |
||
| 554 | try { |
||
| 555 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 556 | . $p['dbname'] . '.$cmd' . "\x00" |
||
| 557 | . pack('VV', 0, -1) |
||
| 558 | . bson_encode($query) |
||
| 559 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 560 | if (!$conn) { |
||
| 561 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 562 | return; |
||
| 563 | } |
||
| 564 | $conn->requests[$reqId] = [$p['dbname'], $cb, true]; |
||
| 565 | }); |
||
| 566 | } catch (\MongoException $e) { |
||
| 567 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 568 | View Code Duplication | if ($cb !== null) { |
|
| 569 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @TODO DESCR |
||
| 576 | * @param array $keys |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | public function getIndexName($keys) { |
||
| 580 | $name = ''; |
||
| 581 | $first = true; |
||
| 582 | foreach ($keys as $k => $v) { |
||
| 583 | $name .= ($first ? '_' : '') . $k . '_' . $v; |
||
| 584 | $first = false; |
||
| 585 | } |
||
| 586 | return $name; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Ensure index |
||
| 591 | * @param string $ns Collection |
||
| 592 | * @param array $keys Keys |
||
| 593 | * @param array $options Optional. Options |
||
| 594 | * @param callable $cb Optional. Callback called when response received |
||
| 595 | * @callback $cb ( ) |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | public function ensureIndex($ns, $keys, $options = [], $cb = null) { |
||
| 599 | $e = explode('.', $ns, 2); |
||
| 600 | $doc = [ |
||
| 601 | 'ns' => $ns, |
||
| 602 | 'key' => $keys, |
||
| 603 | 'name' => isset($options['name']) ? $options['name'] : $this->getIndexName($keys), |
||
| 604 | ]; |
||
| 605 | if (isset($options['unique'])) { |
||
| 606 | $doc['unique'] = $options['unique']; |
||
| 607 | } |
||
| 608 | if (isset($options['sparse'])) { |
||
| 609 | $doc['sparse'] = $options['sparse']; |
||
| 610 | } |
||
| 611 | if (isset($options['version'])) { |
||
| 612 | $doc['v'] = $options['version']; |
||
| 613 | } |
||
| 614 | if (isset($options['background'])) { |
||
| 615 | $doc['background'] = $options['background']; |
||
| 616 | } |
||
| 617 | if (isset($options['ttl'])) { |
||
| 618 | $doc['expireAfterSeconds'] = $options['ttl']; |
||
| 619 | } |
||
| 620 | $this->getCollection($e[0] . '.system.indexes')->insert($doc, $cb); |
||
| 621 | } |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Gets last error |
||
| 625 | * @param string $db Dbname |
||
| 626 | * @param callable $cb Callback called when response received |
||
| 627 | * @param array $params Parameters. |
||
| 628 | * @param Connection $conn Connection. Optional |
||
| 629 | * @callback $cb ( ) |
||
| 630 | * @return void |
||
| 631 | */ |
||
| 632 | public function lastError($db, $cb, $params = [], $conn = null) { |
||
| 633 | $e = explode('.', $db, 2); |
||
| 634 | $params['getlasterror'] = 1; |
||
| 635 | $cb = CallbackWrapper::wrap($cb); |
||
| 636 | try { |
||
| 637 | $this->request(self::OP_QUERY, |
||
| 638 | pack('V', 0) |
||
| 639 | . $e[0] . '.$cmd' . "\x00" |
||
| 640 | . pack('VV', 0, -1) |
||
| 641 | . bson_encode($params) |
||
| 642 | View Code Duplication | , true, $conn, function ($conn, $reqId = null) use ($db, $cb) { |
|
| 643 | if (!$conn) { |
||
| 644 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 645 | return; |
||
| 646 | } |
||
| 647 | $conn->requests[$reqId] = [$db, $cb, true]; |
||
| 648 | }); |
||
| 649 | } catch (\MongoException $e) { |
||
| 650 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 651 | if ($cb !== null) { |
||
| 652 | call_user_func($cb, ['$err' => $e->getMessage()]); |
||
| 653 | } |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Find objects in collection using min/max specifiers |
||
| 659 | * @param array $p Hash of properties (offset, limit, opts, where, col, min, max) |
||
| 660 | * @param callable $cb Callback called when response received |
||
| 661 | * @callback $cb ( ) |
||
| 662 | * @return void |
||
| 663 | */ |
||
| 664 | public function range($p, $cb) { |
||
| 665 | if (!isset($p['offset'])) { |
||
| 666 | $p['offset'] = 0; |
||
| 667 | } |
||
| 668 | |||
| 669 | if (!isset($p['limit'])) { |
||
| 670 | $p['limit'] = -1; |
||
| 671 | } |
||
| 672 | |||
| 673 | if (!isset($p['opts'])) { |
||
| 674 | $p['opts'] = 0; |
||
| 675 | } |
||
| 676 | |||
| 677 | if (!isset($p['where'])) { |
||
| 678 | $p['where'] = []; |
||
| 679 | } |
||
| 680 | |||
| 681 | if (!isset($p['min'])) { |
||
| 682 | $p['min'] = []; |
||
| 683 | } |
||
| 684 | |||
| 685 | if (!isset($p['max'])) { |
||
| 686 | $p['max'] = []; |
||
| 687 | } |
||
| 688 | |||
| 689 | View Code Duplication | if (strpos($p['col'], '.') === false) { |
|
| 690 | $p['col'] = $this->dbname . '.' . $p['col']; |
||
| 691 | } |
||
| 692 | |||
| 693 | $e = explode('.', $p['col'], 2); |
||
| 694 | |||
| 695 | $query = [ |
||
| 696 | '$query' => $p['where'], |
||
| 697 | ]; |
||
| 698 | |||
| 699 | if (sizeof($p['min'])) { |
||
| 700 | $query['$min'] = $p['min']; |
||
| 701 | } |
||
| 702 | |||
| 703 | if (sizeof($p['max'])) { |
||
| 704 | $query['$max'] = $p['max']; |
||
| 705 | } |
||
| 706 | |||
| 707 | View Code Duplication | if (is_string($p['where'])) { |
|
| 708 | $query['where'] = new \MongoCode($p['where']); |
||
| 709 | } |
||
| 710 | elseif ( |
||
| 711 | is_object($p['where']) |
||
| 712 | || sizeof($p['where']) |
||
| 713 | ) { |
||
| 714 | $query['query'] = $p['where']; |
||
| 715 | } |
||
| 716 | |||
| 717 | $cb = CallbackWrapper::wrap($cb); |
||
| 718 | if ($this->safeMode) { |
||
| 719 | static::safeModeEnc($query); |
||
| 720 | } |
||
| 721 | try { |
||
| 722 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 723 | . $e[0] . '.$cmd' . "\x00" |
||
| 724 | . pack('VV', $p['offset'], $p['limit']) |
||
| 725 | . bson_encode($query) |
||
| 726 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 727 | if (!$conn) { |
||
| 728 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 729 | return; |
||
| 730 | } |
||
| 731 | $conn->requests[$reqId] = [$p['col'], $cb, true]; |
||
| 732 | }); |
||
| 733 | } catch (\MongoException $e) { |
||
| 734 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 735 | View Code Duplication | if ($cb !== null) { |
|
| 736 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 737 | } |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Evaluates a code on the server side |
||
| 743 | * @param string $code Code |
||
| 744 | * @param callable $cb Callback called when response received |
||
| 745 | * @callback $cb ( ) |
||
| 746 | * @return void |
||
| 747 | */ |
||
| 748 | public function evaluate($code, $cb) { |
||
| 749 | $p = []; |
||
| 750 | |||
| 751 | if (!isset($p['offset'])) { |
||
| 752 | $p['offset'] = 0; |
||
| 753 | } |
||
| 754 | |||
| 755 | if (!isset($p['limit'])) { |
||
| 756 | $p['limit'] = -1; |
||
| 757 | } |
||
| 758 | |||
| 759 | if (!isset($p['opts'])) { |
||
| 760 | $p['opts'] = 0; |
||
| 761 | } |
||
| 762 | |||
| 763 | if (!isset($p['db'])) { |
||
| 764 | $p['db'] = $this->dbname; |
||
| 765 | } |
||
| 766 | |||
| 767 | $cb = CallbackWrapper::wrap($cb); |
||
| 768 | try { |
||
| 769 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 770 | . $p['db'] . '.$cmd' . "\x00" |
||
| 771 | . pack('VV', $p['offset'], $p['limit']) |
||
| 772 | . bson_encode(['$eval' => new \MongoCode($code)]) |
||
| 773 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 774 | if (!$conn) { |
||
| 775 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 776 | return; |
||
| 777 | } |
||
| 778 | $conn->requests[$reqId] = [$p['db'], $cb, true]; |
||
| 779 | }); |
||
| 780 | } catch (\MongoException $e) { |
||
| 781 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 782 | View Code Duplication | if ($cb !== null) { |
|
| 783 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Returns distinct values of the property |
||
| 790 | * @param array $p Hash of properties (offset, limit, opts, key, col, where) |
||
| 791 | * @param callable $cb Callback called when response received |
||
| 792 | * @callback $cb ( ) |
||
| 793 | * @return void |
||
| 794 | */ |
||
| 795 | public function distinct($p, $cb) { |
||
| 796 | $this->_params($p); |
||
| 797 | $e = explode('.', $p['col'], 2); |
||
| 798 | |||
| 799 | $query = [ |
||
| 800 | 'distinct' => $e[1], |
||
| 801 | 'key' => $p['key'], |
||
| 802 | ]; |
||
| 803 | |||
| 804 | View Code Duplication | if (isset($p[$k = 'rp'])) { |
|
| 805 | $v = $p[$k]; |
||
| 806 | if (is_string($v)) { |
||
| 807 | $v = ['mode' => $v]; |
||
| 808 | } |
||
| 809 | $query['$readPreference'] = $v; |
||
| 810 | } |
||
| 811 | |||
| 812 | if (isset($p['where'])) { |
||
| 813 | $query['query'] = $p['where']; |
||
| 814 | } |
||
| 815 | $cb = CallbackWrapper::wrap($cb); |
||
| 816 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 817 | . $e[0] . '.$cmd' . "\x00" |
||
| 818 | . pack('VV', $p['offset'], $p['limit']) |
||
| 819 | . bson_encode($query) |
||
| 820 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 821 | if (!$conn) { |
||
| 822 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 823 | return; |
||
| 824 | } |
||
| 825 | $conn->requests[$reqId] = [$p['col'], $cb, true]; |
||
| 826 | }); |
||
| 827 | } |
||
| 828 | |||
| 829 | /** |
||
| 830 | * [_paramFields description] |
||
| 831 | * @param mixed $f |
||
| 832 | * @return array |
||
| 833 | */ |
||
| 834 | protected function _paramFields($f) { |
||
| 835 | if (is_string($f)) { |
||
| 836 | $f = array_map('trim', explode(',', $f)); |
||
| 837 | } |
||
| 838 | if (!is_array($f) || sizeof($f) === 0) { |
||
| 839 | return null; |
||
| 840 | } |
||
| 841 | if (!isset($f[0])) { |
||
| 842 | return $f; |
||
| 843 | } |
||
| 844 | $p = []; |
||
| 845 | foreach ($f as $k) { |
||
| 846 | $p[$k] = 1; |
||
| 847 | } |
||
| 848 | return $p; |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * [_params description] |
||
| 853 | * @param array &$p |
||
| 854 | * @return void |
||
| 855 | */ |
||
| 856 | protected function _params(&$p) { |
||
| 857 | foreach ($p as $k => &$v) { |
||
| 858 | if ($k === 'fields' || $k === 'sort') { |
||
| 859 | $v = $this->_paramFields($v); |
||
| 860 | } elseif ($k === 'where') { |
||
| 861 | if (is_string($v)) { |
||
| 862 | $v = new \MongoCode($v); |
||
| 863 | } |
||
| 864 | } |
||
| 865 | elseif ($k === 'reduce') { |
||
| 866 | if (is_string($v)) { |
||
| 867 | $v = new \MongoCode($v); |
||
| 868 | } |
||
| 869 | } |
||
| 870 | elseif ($k === 'rp') { |
||
| 871 | if (is_string($v)) { |
||
| 872 | $v = ['mode' => $v]; |
||
| 873 | } |
||
| 874 | } |
||
| 875 | } |
||
| 876 | |||
| 877 | if (!isset($p['offset'])) { |
||
| 878 | $p['offset'] = 0; |
||
| 879 | } |
||
| 880 | |||
| 881 | if (!isset($p['limit'])) { |
||
| 882 | $p['limit'] = -1; |
||
| 883 | } |
||
| 884 | |||
| 885 | if (!isset($p['opts'])) { |
||
| 886 | $p['opts'] = 0; |
||
| 887 | } |
||
| 888 | |||
| 889 | if (!isset($p['key'])) { |
||
| 890 | $p['key'] = ''; |
||
| 891 | } |
||
| 892 | |||
| 893 | View Code Duplication | if (strpos($p['col'], '.') === false) { |
|
| 894 | $p['col'] = $this->dbname . '.' . $p['col']; |
||
| 895 | } |
||
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Find and modify |
||
| 900 | * @param array $p Hash of properties |
||
| 901 | * @param callable $cb Callback called when response received |
||
| 902 | * @callback $cb ( ) |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | public function findAndModify($p, $cb) { |
||
| 906 | $this->_params($p); |
||
| 907 | $e = explode('.', $p['col'], 2); |
||
| 908 | $query = [ |
||
| 909 | 'findAndModify' => $e[1], |
||
| 910 | ]; |
||
| 911 | |||
| 912 | View Code Duplication | if (isset($p[$k = 'rp'])) { |
|
| 913 | $v = $p[$k]; |
||
| 914 | if (is_string($v)) { |
||
| 915 | $v = ['mode' => $v]; |
||
| 916 | } |
||
| 917 | $query['$readPreference'] = $v; |
||
| 918 | } |
||
| 919 | |||
| 920 | if (isset($p['sort'])) { |
||
| 921 | $query['sort'] = $p['sort']; |
||
| 922 | } |
||
| 923 | if (isset($p['update'])) { |
||
| 924 | $query['update'] = $p['update']; |
||
| 925 | } |
||
| 926 | if (isset($p['new'])) { |
||
| 927 | $query['new'] = (boolean) $p['new']; |
||
| 928 | } |
||
| 929 | if (isset($p['remove'])) { |
||
| 930 | $query['remove'] = (boolean) $p['remove']; |
||
| 931 | } |
||
| 932 | if (isset($p['upsert'])) { |
||
| 933 | $query['upsert'] = (boolean) $p['upsert']; |
||
| 934 | } |
||
| 935 | if (isset($p['where'])) { |
||
| 936 | $query['query'] = $p['where']; |
||
| 937 | } |
||
| 938 | elseif (isset($p['query'])) { |
||
| 939 | $query['query'] = $p['query']; |
||
| 940 | } |
||
| 941 | if ($this->safeMode) { |
||
| 942 | static::safeModeEnc($query); |
||
| 943 | } |
||
| 944 | $cb = CallbackWrapper::wrap($cb); |
||
| 945 | try { |
||
| 946 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 947 | . $e[0] . '.$cmd' . "\x00" |
||
| 948 | . pack('VV', $p['offset'], $p['limit']) |
||
| 949 | . bson_encode($query) |
||
| 950 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 951 | if (!$conn) { |
||
| 952 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 953 | return; |
||
| 954 | } |
||
| 955 | $conn->requests[$reqId] = [$p['col'], $cb, true]; |
||
| 956 | }); |
||
| 957 | } catch (\MongoException $e) { |
||
| 958 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 959 | View Code Duplication | if ($cb !== null) { |
|
| 960 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 961 | } |
||
| 962 | } |
||
| 963 | } |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Groupping function |
||
| 967 | * @param array $p Hash of properties (offset, limit, opts, key, col, reduce, initial) |
||
| 968 | * @param callable $cb Callback called when response received |
||
| 969 | * @callback $cb ( ) |
||
| 970 | * @return void |
||
| 971 | */ |
||
| 972 | public function group($p, $cb) { |
||
| 973 | if (!isset($p['reduce'])) { |
||
| 974 | $p['reduce'] = ''; |
||
| 975 | } |
||
| 976 | $this->_params($p); |
||
| 977 | |||
| 978 | $e = explode('.', $p['col'], 2); |
||
| 979 | |||
| 980 | $query = [ |
||
| 981 | 'group' => [ |
||
| 982 | 'ns' => $e[1], |
||
| 983 | 'key' => $p['key'], |
||
| 984 | '$reduce' => $p['reduce'], |
||
| 985 | 'initial' => $p['initial'], |
||
| 986 | ] |
||
| 987 | ]; |
||
| 988 | |||
| 989 | if (isset($p[$k = 'cond'])) { |
||
| 990 | $query['group'][$k] = $p[$k]; |
||
| 991 | } |
||
| 992 | |||
| 993 | if (isset($p['rp'])) { |
||
| 994 | $query['$readPreference'] = $p['rp']; |
||
| 995 | } |
||
| 996 | |||
| 997 | if (isset($p[$k = 'finalize'])) { |
||
| 998 | if (is_string($p[$k])) { |
||
| 999 | $p[$k] = new \MongoCode($p[$k]); |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | $query['group'][$k] = $p[$k]; |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | if (isset($p[$k = 'keyf'])) { |
||
| 1006 | $query[$k] = $p[$k]; |
||
| 1007 | } |
||
| 1008 | if ($this->safeMode) { |
||
| 1009 | static::safeModeEnc($query); |
||
| 1010 | } |
||
| 1011 | $cb = CallbackWrapper::wrap($cb); |
||
| 1012 | try { |
||
| 1013 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 1014 | . $e[0] . '.$cmd' . "\x00" |
||
| 1015 | . pack('VV', $p['offset'], $p['limit']) |
||
| 1016 | . bson_encode($query) |
||
| 1017 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 1018 | if (!$conn) { |
||
| 1019 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 1020 | return; |
||
| 1021 | } |
||
| 1022 | $conn->requests[$reqId] = [$p['col'], $cb, false]; |
||
| 1023 | }); |
||
| 1024 | } catch (\MongoException $e) { |
||
| 1025 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 1026 | View Code Duplication | if ($cb !== null) { |
|
| 1027 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 1028 | } |
||
| 1029 | } |
||
| 1030 | } |
||
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Aggregate function |
||
| 1034 | * @param array $p Hash of properties (offset, limit, opts, key, col) |
||
| 1035 | * @param callable $cb Callback called when response received |
||
| 1036 | * @callback $cb ( ) |
||
| 1037 | * @return void |
||
| 1038 | */ |
||
| 1039 | public function aggregate($p, $cb) { |
||
| 1040 | $this->_params($p); |
||
| 1041 | |||
| 1042 | $e = explode('.', $p['col'], 2); |
||
| 1043 | $query = [ |
||
| 1044 | 'aggregate' => $e[1] |
||
| 1045 | ]; |
||
| 1046 | |||
| 1047 | if (isset($p['rp'])) { |
||
| 1048 | $query['$readPreference'] = $p['rp']; |
||
| 1049 | unset($p['rp']); |
||
| 1050 | } |
||
| 1051 | foreach ($p as $k => $v) { |
||
| 1052 | if (substr($k, 0, 1) === '$' || $k === 'pipeline') { |
||
| 1053 | $query[$k] = $v; |
||
| 1054 | } |
||
| 1055 | } |
||
| 1056 | $cb = CallbackWrapper::wrap($cb); |
||
| 1057 | try { |
||
| 1058 | $this->request(self::OP_QUERY, pack('V', $p['opts']) |
||
| 1059 | . $e[0] . '.$cmd' . "\x00" |
||
| 1060 | . pack('VV', $p['offset'], $p['limit']) |
||
| 1061 | . bson_encode($query) |
||
| 1062 | View Code Duplication | . (isset($p['fields']) ? bson_encode($p['fields']) : ''), true, null, function ($conn, $reqId = null) use ($p, $cb) { |
|
| 1063 | if (!$conn) { |
||
| 1064 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 1065 | return; |
||
| 1066 | } |
||
| 1067 | $conn->requests[$reqId] = [$p['col'], $cb, false]; |
||
| 1068 | }); |
||
| 1069 | } catch (\MongoException $e) { |
||
| 1070 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 1071 | View Code Duplication | if ($cb !== null) { |
|
| 1072 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $query, '$fields' => isset($p['fields']) ? $p['fields'] : null]); |
||
| 1073 | } |
||
| 1074 | } |
||
| 1075 | } |
||
| 1076 | |||
| 1077 | /** |
||
| 1078 | * Updates one object in collection |
||
| 1079 | * @param string $col Collection's name |
||
| 1080 | * @param array $cond Conditions |
||
| 1081 | * @param array $data Data |
||
| 1082 | * @param integer $flags Optional. Flags |
||
| 1083 | * @param callable $cb Optional. Callback |
||
| 1084 | * @param array $params Optional. Parameters |
||
| 1085 | * @callback $cb ( ) |
||
| 1086 | * @return void |
||
| 1087 | */ |
||
| 1088 | public function update($col, $cond, $data, $flags = 0, $cb = NULL, $params = []) { |
||
| 1089 | View Code Duplication | if (strpos($col, '.') === false) { |
|
| 1090 | $col = $this->dbname . '.' . $col; |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | if (is_string($cond)) { |
||
| 1094 | $cond = new \MongoCode($cond); |
||
| 1095 | } |
||
| 1096 | |||
| 1097 | if ($flags) { |
||
| 1098 | //if (!isset($data['_id'])) {$data['_id'] = new MongoId();} |
||
| 1099 | } |
||
| 1100 | if ($this->safeMode) { |
||
| 1101 | static::safeModeEnc($cond); |
||
| 1102 | static::safeModeEnc($data); |
||
| 1103 | } |
||
| 1104 | $this->request(self::OP_UPDATE, |
||
| 1105 | "\x00\x00\x00\x00" |
||
| 1106 | . $col . "\x00" |
||
| 1107 | . pack('V', $flags) |
||
| 1108 | . bson_encode($cond) |
||
| 1109 | . bson_encode($data) |
||
| 1110 | View Code Duplication | , false, null, function ($conn, $reqId = null) use ($cb, $col, $params) { |
|
| 1111 | if (!$conn) { |
||
| 1112 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 1113 | return; |
||
| 1114 | } |
||
| 1115 | if ($cb !== NULL) { |
||
| 1116 | $this->lastError($col, $cb, $params, $conn); |
||
| 1117 | } |
||
| 1118 | }); |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | /** |
||
| 1122 | * Updates one object in collection |
||
| 1123 | * @param string $col Collection's name |
||
| 1124 | * @param array $cond Conditions |
||
| 1125 | * @param array $data Data |
||
| 1126 | * @param callable $cb Optional. Callback |
||
| 1127 | * @param array $params Optional. Parameters |
||
| 1128 | * @callback $cb ( ) |
||
| 1129 | * @return void |
||
| 1130 | */ |
||
| 1131 | public function updateOne($col, $cond, $data, $cb = NULL, $params = []) { |
||
| 1134 | |||
| 1135 | /** |
||
| 1136 | * Updates several objects in collection |
||
| 1137 | * @param string $col Collection's name |
||
| 1138 | * @param array $cond Conditions |
||
| 1139 | * @param array $data Data |
||
| 1140 | * @param callable $cb Optional. Callback |
||
| 1141 | * @param array $params Optional. Parameters |
||
| 1142 | * @callback $cb ( ) |
||
| 1143 | * @return void |
||
| 1144 | */ |
||
| 1145 | public function updateMulti($col, $cond, $data, $cb = NULL, $params = []) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Upserts an object (updates if exists, insert if not exists) |
||
| 1151 | * @param string $col Collection's name |
||
| 1152 | * @param array $cond Conditions |
||
| 1153 | * @param array $data Data |
||
| 1154 | * @param boolean $multi Optional. Multi |
||
| 1155 | * @param callable $cb Optional. Callback |
||
| 1156 | * @param array $params Optional. Parameters |
||
| 1157 | * @callback $cb ( ) |
||
| 1158 | * @return void |
||
| 1159 | */ |
||
| 1160 | public function upsert($col, $cond, $data, $multi = false, $cb = NULL, $params = []) { |
||
| 1163 | |||
| 1164 | /** |
||
| 1165 | * Upserts an object (updates if exists, insert if not exists) |
||
| 1166 | * @param string $col Collection's name |
||
| 1167 | * @param array $cond Conditions |
||
| 1168 | * @param array $data Data |
||
| 1169 | * @param callable $cb Optional. Callback |
||
| 1170 | * @param array $params Optional. Parameters |
||
| 1171 | * @callback $cb ( ) |
||
| 1172 | * @return void |
||
| 1173 | */ |
||
| 1174 | public function upsertOne($col, $cond, $data, $cb = NULL, $params = []) { |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Upserts an object (updates if exists, insert if not exists) |
||
| 1180 | * @param string $col Collection's name |
||
| 1181 | * @param array $cond Conditions |
||
| 1182 | * @param array $data Data |
||
| 1183 | * @param callable $cb Optional. Callback |
||
| 1184 | * @param array $params Optional. Parameters |
||
| 1185 | * @callback $cb ( ) |
||
| 1186 | * @return void |
||
| 1187 | */ |
||
| 1188 | public function upsertMulti($col, $cond, $data, $cb = NULL, $params = []) { |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Inserts an object |
||
| 1194 | * @param string $col Collection's name |
||
| 1195 | * @param array $doc Document |
||
| 1196 | * @param callable $cb Optional. Callback |
||
| 1197 | * @param array $params Optional. Parameters |
||
| 1198 | * @callback $cb ( ) |
||
| 1199 | * @return MongoId |
||
| 1200 | */ |
||
| 1201 | public function insert($col, $doc = [], $cb = NULL, $params = []) { |
||
| 1202 | View Code Duplication | if (strpos($col, '.') === false) { |
|
| 1203 | $col = $this->dbname . '.' . $col; |
||
| 1204 | } |
||
| 1205 | |||
| 1206 | if (!isset($doc['_id'])) { |
||
| 1207 | $doc['_id'] = new \MongoId; |
||
| 1208 | } |
||
| 1209 | if ($this->safeMode) { |
||
| 1210 | static::safeModeEnc($doc); |
||
| 1211 | } |
||
| 1212 | try { |
||
| 1213 | $this->request(self::OP_INSERT, |
||
| 1214 | "\x00\x00\x00\x00" |
||
| 1215 | . $col . "\x00" |
||
| 1216 | . bson_encode($doc) |
||
| 1217 | View Code Duplication | , false, null, function ($conn, $reqId = null) use ($cb, $col, $params) { |
|
| 1218 | if ($cb !== NULL) { |
||
| 1219 | $this->lastError($col, $cb, $params, $conn); |
||
| 1220 | } |
||
| 1221 | }); |
||
| 1222 | |||
| 1223 | return $doc['_id']; |
||
| 1224 | } catch (\MongoException $e) { |
||
| 1225 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 1226 | View Code Duplication | if ($cb !== null) { |
|
| 1227 | if ($cb !== null) { |
||
| 1228 | call_user_func($cb, ['$err' => $e->getMessage(), '$doc' => $doc]); |
||
| 1229 | } |
||
| 1230 | } |
||
| 1231 | } |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | /** |
||
| 1235 | * Sends a request to kill certain cursors on the server side |
||
| 1236 | * @param array $cursors Array of cursors |
||
| 1237 | * @param Connection $conn Connection |
||
| 1238 | * @return void |
||
| 1239 | */ |
||
| 1240 | public function killCursors($cursors = [], $conn) { |
||
| 1241 | $this->request(self::OP_KILL_CURSORS, |
||
| 1242 | "\x00\x00\x00\x00" |
||
| 1243 | . pack('V', sizeof($cursors)) |
||
| 1244 | . implode('', $cursors) |
||
| 1245 | , false, $conn); |
||
| 1246 | } |
||
| 1247 | |||
| 1248 | /** |
||
| 1249 | * Inserts several documents |
||
| 1250 | * @param string $col Collection's name |
||
| 1251 | * @param array $docs Array of docs |
||
| 1252 | * @param callable $cb Optional. Callback |
||
| 1253 | * @param array $params Optional. Parameters |
||
| 1254 | * @callback $cb ( ) |
||
| 1255 | * @return array IDs |
||
| 1256 | */ |
||
| 1257 | public function insertMulti($col, $docs = [], $cb = NULL, $params = []) { |
||
| 1258 | View Code Duplication | if (strpos($col, '.') === false) { |
|
| 1259 | $col = $this->dbname . '.' . $col; |
||
| 1260 | } |
||
| 1261 | |||
| 1262 | $ids = []; |
||
| 1263 | $bson = ''; |
||
| 1264 | |||
| 1265 | foreach ($docs as &$doc) { |
||
| 1266 | if (!isset($doc['_id'])) { |
||
| 1267 | $doc['_id'] = new MongoId(); |
||
| 1268 | } |
||
| 1269 | try { |
||
| 1270 | $bson .= bson_encode($doc); |
||
| 1271 | } catch (\MongoException $e) { |
||
| 1272 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 1273 | View Code Duplication | if ($cb !== null) { |
|
| 1274 | call_user_func($cb, ['$err' => $e->getMessage(), '$doc' => $doc]); |
||
| 1275 | } |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | $ids[] = $doc['_id']; |
||
| 1279 | } |
||
| 1280 | |||
| 1281 | $this->request(self::OP_INSERT, |
||
| 1282 | "\x00\x00\x00\x00" |
||
| 1283 | . $col . "\x00" |
||
| 1284 | . $bson |
||
| 1285 | View Code Duplication | , false, null, function ($conn, $reqId = null) use ($cb, $col, $params) { |
|
| 1286 | if ($cb !== NULL) { |
||
| 1287 | $this->lastError($col, $cb, $params, $conn); |
||
| 1288 | } |
||
| 1289 | }); |
||
| 1290 | return $ids; |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | /** |
||
| 1294 | * Remove objects from collection |
||
| 1295 | * @param string $col Collection's name |
||
| 1296 | * @param array $cond Conditions |
||
| 1297 | * @param callable $cb Optional. Callback called when response received |
||
| 1298 | * @param array $params Optional. Parameters |
||
| 1299 | * @callback $cb ( ) |
||
| 1300 | * @return void |
||
| 1301 | */ |
||
| 1302 | public function remove($col, $cond = [], $cb = NULL, $params = []) { |
||
| 1303 | View Code Duplication | if (strpos($col, '.') === false) { |
|
| 1304 | $col = $this->dbname . '.' . $col; |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | if (is_string($cond)) { |
||
| 1308 | $cond = new \MongoCode($cond); |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | if ($this->safeMode && is_array($cond)) { |
||
| 1312 | static::safeModeEnc($cond); |
||
| 1313 | } |
||
| 1314 | try { |
||
| 1315 | $this->request(self::OP_DELETE, |
||
| 1316 | "\x00\x00\x00\x00" |
||
| 1317 | . $col . "\x00" |
||
| 1318 | . "\x00\x00\x00\x00" |
||
| 1319 | . bson_encode($cond) |
||
| 1320 | View Code Duplication | , false, null, function ($conn, $reqId = null) use ($col, $cb, $params) { |
|
| 1321 | if (!$conn) { |
||
| 1322 | !$cb || call_user_func($cb, ['$err' => 'Connection error.']); |
||
| 1323 | return; |
||
| 1324 | } |
||
| 1325 | if ($cb !== NULL) { |
||
| 1326 | $this->lastError($col, $cb, $params, $conn); |
||
| 1327 | } |
||
| 1328 | }); |
||
| 1329 | } catch (\MongoException $e) { |
||
| 1330 | Daemon::log('MongoClient exception: '.$e->getMessage().': '.$e->getTraceAsString()); |
||
| 1331 | View Code Duplication | if ($cb !== null) { |
|
| 1332 | call_user_func($cb, ['$err' => $e->getMessage(), '$query' => $cond]); |
||
| 1333 | } |
||
| 1334 | } |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | /** |
||
| 1338 | * Asks for more objects |
||
| 1339 | * @param string $col Collection's name |
||
| 1340 | * @param string $id Cursor's ID |
||
| 1341 | * @param integer $number Number of objects |
||
| 1342 | * @param Connection $conn Connection |
||
| 1343 | * @return void |
||
| 1344 | */ |
||
| 1345 | public function getMore($col, $id, $number, $conn) { |
||
| 1363 | |||
| 1364 | /** |
||
| 1365 | * Returns an object of collection |
||
| 1366 | * @param string $col Collection's name |
||
| 1367 | * @return Collection |
||
| 1368 | */ |
||
| 1369 | public function getCollection($col) { |
||
| 1370 | if (strpos($col, '.') === false) { |
||
| 1371 | $col = $this->dbname . '.' . $col; |
||
| 1372 | } |
||
| 1373 | else { |
||
| 1374 | $collName = explode('.', $col, 2); |
||
| 1375 | } |
||
| 1376 | |||
| 1377 | if (isset($this->collections[$col])) { |
||
| 1378 | return $this->collections[$col]; |
||
| 1379 | } |
||
| 1380 | |||
| 1381 | return $this->collections[$col] = new Collection($col, $this); |
||
| 1382 | } |
||
| 1383 | |||
| 1384 | /** |
||
| 1385 | * Magic getter-method. Proxy for getCollection |
||
| 1386 | * @param string $name Collection's name |
||
| 1387 | * @return Collection |
||
| 1388 | */ |
||
| 1389 | public function __get($name) { |
||
| 1392 | |||
| 1393 | public function sasl_scrum_sha1_auth($p, $cb) |
||
| 1394 | { |
||
| 1395 | $session = [ |
||
| 1396 | 'cb' => $cb, |
||
| 1397 | 'step' => 0, |
||
| 1398 | 'dbname' => $p['dbname'], |
||
| 1399 | 'user' => $p['user'], |
||
| 1400 | 'password' => $p['password'], |
||
| 1401 | 'auth_message' => '', |
||
| 1402 | 'conn' => array_key_exists('conn', $p) ? $p['conn'] : null, |
||
| 1403 | ]; |
||
| 1404 | $this->sasl_scrum_sha1_step($session); |
||
| 1405 | } |
||
| 1406 | |||
| 1407 | public function sasl_scrum_sha1_step($session, $input = null) |
||
| 1408 | { |
||
| 1409 | $session['step']++; |
||
| 1410 | $query = []; |
||
| 1411 | |||
| 1412 | if (!is_null($input) && (!empty($input['$err']) || !empty($input['errmsg']))) { |
||
| 1413 | call_user_func($session['cb'], $input); |
||
| 1414 | return; |
||
| 1415 | } |
||
| 1416 | |||
| 1417 | if ($session['step'] == 1) { |
||
| 1418 | |||
| 1419 | $session['nonce'] = base64_encode(openssl_random_pseudo_bytes(24)); |
||
| 1484 | |||
| 1485 | public function sasl_scrum_sha1_conversation($dbname, $query, $cb, $conn = null) |
||
| 1509 | |||
| 1510 | public function sasl_scrum_sha1_extract_payload($payload) |
||
| 1521 | } |
||
| 1522 |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.