Esempio n. 1
0
 /**
  * Executes a closure with a temporary read preference on a database or
  * collection.
  *
  * @param Database|Collection $object
  * @param \Closure            $closure
  * @return mixed
  */
 private function withReadPreference($object, \Closure $closure)
 {
     if (!isset($this->query['readPreference'])) {
         return $closure();
     }
     $prevReadPref = $object->getReadPreference();
     $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
     try {
         $result = $closure();
     } catch (\Exception $e) {
     }
     $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
     $object->setReadPreference($prevReadPref['type'], $prevTags);
     if (isset($e)) {
         throw $e;
     }
     return $result;
 }