/** * Set which HTTP library you want to use for communicating with CouchDB. * * @param string $type The type of adapter you want to use. Should be one of * the Sag::$HTTP_* variables. * @return Sag Returns $this. * * @see Sag::$HTTP_NATIVE_SOCKETS * @see Sag::$HTTP_CURL */ public function setHTTPAdapter($type = null) { if (!$type) { $type = extension_loaded("curl") ? self::$HTTP_CURL : self::$HTTP_NATIVE_SOCKETS; } // nothing to be done if ($type === $this->httpAdapterType) { return true; } // remember what was already set (ie., might have called decode() already) $prevDecode = null; $prevTimeouts = null; if ($this->httpAdapter) { $prevDecode = $this->httpAdapter->decodeResp; $prevTimeouts = $this->httpAdapter->getTimeouts(); } // the glue switch ($type) { case self::$HTTP_NATIVE_SOCKETS: $this->httpAdapter = new SagNativeHTTPAdapter($this->host, $this->port); break; case self::$HTTP_CURL: $this->httpAdapter = new SagCURLHTTPAdapter($this->host, $this->port); break; default: throw SagException("Invalid Sag HTTP adapter specified: {$type}"); } // restore previous decode value, if any if (is_bool($prevDecode)) { $this->httpAdapter->decodeResp = $prevDecode; } // restore previous timeout vlaues, if any if (is_array($prevTimeouts)) { $this->httpAdapter->setTimeoutsFromArray($prevTimeouts); } $this->httpAdapterType = $type; return $this; }
/** * A utility function that sets the different timeout values based on an * associative array. * * @param array $arr An associative array with the keys 'open', 'rwSeconds', * and 'rwMicroseconds'. * * @see getTimeouts() */ public function setTimeoutsFromArray($arr) { /* * Validation is lax in here because this should only ever be used with * getTimeouts() return values. If people are using it by hand then there * might be something wrong with the API. */ if (!is_array($arr)) { throw SagException('Expected an array and got something else.'); } if (is_int($arr['open'])) { $this->setOpenTimeout($arr['open']); } if (is_int($arr['rwSeconds'])) { if (is_int($arr['rwMicroseconds'])) { $this->setRWTimeout($arr['rwSeconds'], $arr['rwMicroseconds']); } else { $this->setRWTimeout($arr['rwSeconds']); } } }