@@ -68,9 +68,10 @@ |
||
| 68 | 68 | $this->scheme = isset($matches['scheme']) ? $matches['scheme'] : "tcp://"; |
| 69 | 69 | $this->host = isset($matches['host']) ? $matches['host'] : self::DEFAULT_HOST; |
| 70 | 70 | $this->port = isset($matches['port']) ? substr($matches['port'], 1) : self::DEFAULT_PORT; |
| 71 | - } |
|
| 72 | - else // Match attempt failed. |
|
| 71 | + } else { |
|
| 72 | + // Match attempt failed. |
|
| 73 | 73 | throw new \InvalidArgumentException(sprintf("'%s' is not a valid URI.", $server)); |
| 74 | + } |
|
| 74 | 75 | |
| 75 | 76 | $this->userName = (string)$userName; |
| 76 | 77 | $this->password = (string)$password; |
@@ -52,8 +52,9 @@ discard block |
||
| 52 | 52 | * @copydoc AbstractAdapter::initialize() |
| 53 | 53 | */ |
| 54 | 54 | public function initialize() { |
| 55 | - if (!extension_loaded("curl")) |
|
| 56 | - throw new \RuntimeException("The cURL extension is not loaded."); |
|
| 55 | + if (!extension_loaded("curl")) { |
|
| 56 | + throw new \RuntimeException("The cURL extension is not loaded."); |
|
| 57 | + } |
|
| 57 | 58 | } |
| 58 | 59 | |
| 59 | 60 | |
@@ -64,8 +65,9 @@ discard block |
||
| 64 | 65 | $opts = []; |
| 65 | 66 | |
| 66 | 67 | // Resets all the cURL options. The curl_reset() function is available only since PHP 5.5. |
| 67 | - if (function_exists('curl_reset')) |
|
| 68 | - curl_reset($this->handle); |
|
| 68 | + if (function_exists('curl_reset')) { |
|
| 69 | + curl_reset($this->handle); |
|
| 70 | + } |
|
| 69 | 71 | |
| 70 | 72 | // Sets the methods and its related options. |
| 71 | 73 | switch ($request->getMethod()) { |
@@ -100,9 +102,9 @@ discard block |
||
| 100 | 102 | |
| 101 | 103 | $opts[CURLOPT_INFILE] = $fd; |
| 102 | 104 | $opts[CURLOPT_INFILESIZE] = $request->getBodyLength(); |
| 105 | + } else { |
|
| 106 | + throw new \RuntimeException("Cannot create the stream."); |
|
| 103 | 107 | } |
| 104 | - else |
|
| 105 | - throw new \RuntimeException("Cannot create the stream."); |
|
| 106 | 108 | } |
| 107 | 109 | |
| 108 | 110 | break; |
@@ -143,8 +145,9 @@ discard block |
||
| 143 | 145 | // This fix a known cURL bug: see http://the-stickman.com/web-development/php-and-curl-disabling-100-continue-header/ |
| 144 | 146 | // cURL sets the Expect header field automatically, ignoring the fact that a client may not need it for the specific |
| 145 | 147 | // request. |
| 146 | - if (!$request->hasHeaderField(Request::EXPECT_HF)) |
|
| 147 | - curl_setopt($this->handle, CURLOPT_HTTPHEADER, array("Expect:")); |
|
| 148 | + if (!$request->hasHeaderField(Request::EXPECT_HF)) { |
|
| 149 | + curl_setopt($this->handle, CURLOPT_HTTPHEADER, array("Expect:")); |
|
| 150 | + } |
|
| 148 | 151 | |
| 149 | 152 | // Sets the request header. |
| 150 | 153 | // Due to a stupid bug, using curl_setopt_array(), cURL doesn't override the Content-Type header field. So we must |
@@ -176,8 +179,7 @@ discard block |
||
| 176 | 179 | $response = new Response($header); |
| 177 | 180 | $response->setBody($result); |
| 178 | 181 | return $response; |
| 179 | - } |
|
| 180 | - else { |
|
| 182 | + } else { |
|
| 181 | 183 | $error = curl_error($this->handle); |
| 182 | 184 | throw new \RuntimeException($error); |
| 183 | 185 | } |
@@ -47,7 +47,7 @@ |
||
| 47 | 47 | /** |
| 48 | 48 | * @copydoc AbstractAdapter::__construct() |
| 49 | 49 | * @param[in] bool $persistent (optional) When `true` the client uses a persistent connection. |
| 50 | - */ |
|
| 50 | + */ |
|
| 51 | 51 | public function __construct($server = parent::DEFAULT_SERVER, $userName = "", $password = "", $persistent = TRUE) { |
| 52 | 52 | $this->initialize(); |
| 53 | 53 | |
@@ -56,13 +56,15 @@ discard block |
||
| 56 | 56 | $this->timeout = static::$defaultSocketTimeout; |
| 57 | 57 | |
| 58 | 58 | // Establishes a connection within the server. |
| 59 | - if ($persistent) |
|
| 60 | - $this->handle = @pfsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout); |
|
| 61 | - else |
|
| 62 | - $this->handle = @fsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout); |
|
| 59 | + if ($persistent) { |
|
| 60 | + $this->handle = @pfsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout); |
|
| 61 | + } else { |
|
| 62 | + $this->handle = @fsockopen($this->scheme.$this->host, $this->port, $errno, $errstr, $this->timeout); |
|
| 63 | + } |
|
| 63 | 64 | |
| 64 | - if (!is_resource($this->handle)) |
|
| 65 | - throw new \ErrorException($errstr, $errno); |
|
| 65 | + if (!is_resource($this->handle)) { |
|
| 66 | + throw new \ErrorException($errstr, $errno); |
|
| 67 | + } |
|
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | |
@@ -117,8 +119,9 @@ discard block |
||
| 117 | 119 | $statusCodeAndHeader .= $buffer; |
| 118 | 120 | |
| 119 | 121 | // The header is separated from the body by a newline, so we break when we read it. |
| 120 | - if ($buffer == Message::CRLF) |
|
| 121 | - break; |
|
| 122 | + if ($buffer == Message::CRLF) { |
|
| 123 | + break; |
|
| 124 | + } |
|
| 122 | 125 | } |
| 123 | 126 | |
| 124 | 127 | return $statusCodeAndHeader; |
@@ -135,18 +138,21 @@ discard block |
||
| 135 | 138 | |
| 136 | 139 | // If it's only a newline, this normally means it's read the total amount of data requested minus the newline |
| 137 | 140 | // continue to next loop to make sure we're done. |
| 138 | - if ($line == Message::CRLF) |
|
| 139 | - continue; |
|
| 141 | + if ($line == Message::CRLF) { |
|
| 142 | + continue; |
|
| 143 | + } |
|
| 140 | 144 | |
| 141 | 145 | // The length of the block is expressed in hexadecimal. |
| 142 | 146 | $length = hexdec($line); |
| 143 | 147 | |
| 144 | - if (!is_int($length)) |
|
| 145 | - throw new \RuntimeException("The response doesn't seem chunk encoded."); |
|
| 148 | + if (!is_int($length)) { |
|
| 149 | + throw new \RuntimeException("The response doesn't seem chunk encoded."); |
|
| 150 | + } |
|
| 146 | 151 | |
| 147 | 152 | // Zero is sent when at the end of the chunks or the end of the stream. |
| 148 | - if ($length < 1) |
|
| 149 | - break; |
|
| 153 | + if ($length < 1) { |
|
| 154 | + break; |
|
| 155 | + } |
|
| 150 | 156 | |
| 151 | 157 | // Reads the chunk. |
| 152 | 158 | // When reading from network streams or pipes, such as those returned when reading remote files or from popen() |
@@ -159,18 +165,21 @@ discard block |
||
| 159 | 165 | $size = min(self::BUFFER_LENGTH, $length); |
| 160 | 166 | $data = fread($this->handle, $size); |
| 161 | 167 | |
| 162 | - if (strlen($data) == 0) |
|
| 163 | - break; // EOF |
|
| 168 | + if (strlen($data) == 0) { |
|
| 169 | + break; |
|
| 170 | + } |
|
| 171 | + // EOF |
|
| 164 | 172 | |
| 165 | 173 | $buffer .= $data; |
| 166 | 174 | $length -= strlen($data); |
| 167 | 175 | } |
| 168 | 176 | |
| 169 | 177 | // If a function has been hooked, calls it, else just adds the buffer to the body. |
| 170 | - if (is_null($chunkHook)) |
|
| 171 | - $body .= $buffer; |
|
| 172 | - else |
|
| 173 | - $chunkHook->process($buffer); |
|
| 178 | + if (is_null($chunkHook)) { |
|
| 179 | + $body .= $buffer; |
|
| 180 | + } else { |
|
| 181 | + $chunkHook->process($buffer); |
|
| 182 | + } |
|
| 174 | 183 | } |
| 175 | 184 | |
| 176 | 185 | // A chunk response might have some footer, but CouchDB doesn't use them, so we simply ignore them. |
@@ -179,8 +188,9 @@ discard block |
||
| 179 | 188 | $buffer = fgets($this->handle, self::BUFFER_LENGTH); |
| 180 | 189 | |
| 181 | 190 | // The chunk response ends with a newline, so we break when we read it. |
| 182 | - if ($buffer == Message::CRLF) |
|
| 183 | - break; |
|
| 191 | + if ($buffer == Message::CRLF) { |
|
| 192 | + break; |
|
| 193 | + } |
|
| 184 | 194 | } |
| 185 | 195 | |
| 186 | 196 | return $body; |
@@ -203,8 +213,9 @@ discard block |
||
| 203 | 213 | $body .= $buffer; |
| 204 | 214 | $bytes += strlen($buffer); |
| 205 | 215 | |
| 206 | - if ($bytes >= $length) |
|
| 207 | - break; |
|
| 216 | + if ($bytes >= $length) { |
|
| 217 | + break; |
|
| 218 | + } |
|
| 208 | 219 | } |
| 209 | 220 | } |
| 210 | 221 | |
@@ -214,10 +225,11 @@ discard block |
||
| 214 | 225 | |
| 215 | 226 | // Reads the entity-body. |
| 216 | 227 | protected function readResponseBody($response, $chunkHook) { |
| 217 | - if ($response->getHeaderFieldValue(Response::TRANSFER_ENCODING_HF) == "chunked") |
|
| 218 | - return $this->readChunkedResponseBody($chunkHook); |
|
| 219 | - else |
|
| 220 | - return $this->readStandardResponseBody($response); |
|
| 228 | + if ($response->getHeaderFieldValue(Response::TRANSFER_ENCODING_HF) == "chunked") { |
|
| 229 | + return $this->readChunkedResponseBody($chunkHook); |
|
| 230 | + } else { |
|
| 231 | + return $this->readStandardResponseBody($response); |
|
| 232 | + } |
|
| 221 | 233 | } |
| 222 | 234 | |
| 223 | 235 | |
@@ -227,12 +239,14 @@ discard block |
||
| 227 | 239 | public function send(Request $request, Hook\IChunkHook $chunkHook = NULL) { |
| 228 | 240 | $request->setHeaderField(Request::HOST_HF, $this->host.":".$this->port); |
| 229 | 241 | |
| 230 | - if (!empty($this->userName)) |
|
| 231 | - $request->setBasicAuth($this->userName, $this->password); |
|
| 242 | + if (!empty($this->userName)) { |
|
| 243 | + $request->setBasicAuth($this->userName, $this->password); |
|
| 244 | + } |
|
| 232 | 245 | |
| 233 | 246 | // Sets the Content-Length header only when the given request has a message body. |
| 234 | - if ($request->hasBody()) |
|
| 235 | - $request->setHeaderField(Message::CONTENT_LENGTH_HF, $request->getBodyLength()); |
|
| 247 | + if ($request->hasBody()) { |
|
| 248 | + $request->setHeaderField(Message::CONTENT_LENGTH_HF, $request->getBodyLength()); |
|
| 249 | + } |
|
| 236 | 250 | |
| 237 | 251 | // Writes the request over the socket. |
| 238 | 252 | $this->writeRequest($request); |
@@ -144,8 +144,9 @@ discard block |
||
| 144 | 144 | public static function checkFn($fnImpl, $fnDef, $fnRegex) { |
| 145 | 145 | Lint::checkSourceCode($fnImpl); |
| 146 | 146 | |
| 147 | - if (!preg_match($fnRegex, $fnImpl)) |
|
| 148 | - throw new \Exception("The \$closure must be defined like: $fnDef"); |
|
| 147 | + if (!preg_match($fnRegex, $fnImpl)) { |
|
| 148 | + throw new \Exception("The \$closure must be defined like: $fnDef"); |
|
| 149 | + } |
|
| 149 | 150 | } |
| 150 | 151 | |
| 151 | 152 | |
@@ -153,14 +154,17 @@ discard block |
||
| 153 | 154 | $view = []; |
| 154 | 155 | $view['map'] = $this->mapFn; |
| 155 | 156 | |
| 156 | - if (!empty($this->language)) |
|
| 157 | - $view['language'] = $this->language; |
|
| 157 | + if (!empty($this->language)) { |
|
| 158 | + $view['language'] = $this->language; |
|
| 159 | + } |
|
| 158 | 160 | |
| 159 | - if (!empty($this->reduceFn)) |
|
| 160 | - $view['reduce'] = $this->reduceFn; |
|
| 161 | + if (!empty($this->reduceFn)) { |
|
| 162 | + $view['reduce'] = $this->reduceFn; |
|
| 163 | + } |
|
| 161 | 164 | |
| 162 | - if (!empty($this->options)) |
|
| 163 | - $view['options'] = $this->options; |
|
| 165 | + if (!empty($this->options)) { |
|
| 166 | + $view['options'] = $this->options; |
|
| 167 | + } |
|
| 164 | 168 | |
| 165 | 169 | return $view; |
| 166 | 170 | } |
@@ -225,8 +229,9 @@ discard block |
||
| 225 | 229 | public function setMapFn($value) { |
| 226 | 230 | $fn = stripslashes((string)$value); |
| 227 | 231 | |
| 228 | - if ($this->language == "php") |
|
| 229 | - self::checkFn($fn, self::MAP_DEFINITION, self::MAP_REGEX); |
|
| 232 | + if ($this->language == "php") { |
|
| 233 | + self::checkFn($fn, self::MAP_DEFINITION, self::MAP_REGEX); |
|
| 234 | + } |
|
| 230 | 235 | |
| 231 | 236 | $this->mapFn = $fn; |
| 232 | 237 | } |
@@ -240,8 +245,9 @@ discard block |
||
| 240 | 245 | public function setReduceFn($value) { |
| 241 | 246 | $fn = stripslashes((string)$value); |
| 242 | 247 | |
| 243 | - if ($this->language == "php") |
|
| 244 | - self::checkFn($fn, self::REDUCE_DEFINITION, self::REDUCE_REGEX); |
|
| 248 | + if ($this->language == "php") { |
|
| 249 | + self::checkFn($fn, self::REDUCE_DEFINITION, self::REDUCE_REGEX); |
|
| 250 | + } |
|
| 245 | 251 | |
| 246 | 252 | $this->reduceFn = $fn; |
| 247 | 253 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | * arrays. |
| 55 | 55 | * @retval array An array of rows. |
| 56 | 56 | */ |
| 57 | - public function asArray() { |
|
| 57 | + public function asArray() { |
|
| 58 | 58 | return $this->result['rows']; |
| 59 | 59 | } |
| 60 | 60 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * @param[in] integer $offset The offset to retrieve. |
| 106 | 106 | * @retval mixed Can return all value types. |
| 107 | 107 | */ |
| 108 | - public function offsetGet($offset) { |
|
| 108 | + public function offsetGet($offset) { |
|
| 109 | 109 | return $this->result['rows'][$offset]; |
| 110 | 110 | } |
| 111 | 111 | |