@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | * |
| 20 | 20 | * @link http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/ |
| 21 | 21 | */ |
| 22 | -class MultiRequest{ |
|
| 22 | +class MultiRequest { |
|
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * the curl_multi master handle |
@@ -64,15 +64,15 @@ discard block |
||
| 64 | 64 | * |
| 65 | 65 | * @param \chillerlan\TinyCurl\MultiRequestOptions $options |
| 66 | 66 | */ |
| 67 | - public function __construct(MultiRequestOptions $options = null){ |
|
| 67 | + public function __construct(MultiRequestOptions $options = null) { |
|
| 68 | 68 | |
| 69 | - if(!$options){ |
|
| 69 | + if (!$options) { |
|
| 70 | 70 | $options = new MultiRequestOptions; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $this->options = $options; |
| 74 | 74 | |
| 75 | - if($this->options->handler){ |
|
| 75 | + if ($this->options->handler) { |
|
| 76 | 76 | $this->setHandler(); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -92,8 +92,8 @@ discard block |
||
| 92 | 92 | * |
| 93 | 93 | * @codeCoverageIgnore |
| 94 | 94 | */ |
| 95 | - public function __destruct(){ |
|
| 96 | - if($this->curl_multi){ |
|
| 95 | + public function __destruct() { |
|
| 96 | + if ($this->curl_multi) { |
|
| 97 | 97 | curl_multi_close($this->curl_multi); |
| 98 | 98 | } |
| 99 | 99 | } |
@@ -104,17 +104,17 @@ discard block |
||
| 104 | 104 | * @return $this |
| 105 | 105 | * @throws \chillerlan\TinyCurl\RequestException |
| 106 | 106 | */ |
| 107 | - public function setHandler(MultiResponseHandlerInterface $handler = null){ |
|
| 107 | + public function setHandler(MultiResponseHandlerInterface $handler = null) { |
|
| 108 | 108 | |
| 109 | - if(!$handler){ |
|
| 109 | + if (!$handler) { |
|
| 110 | 110 | |
| 111 | - if(!class_exists($this->options->handler)){ |
|
| 111 | + if (!class_exists($this->options->handler)) { |
|
| 112 | 112 | throw new RequestException('!$this->options->handler'); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | $handler = new $this->options->handler($this); |
| 116 | 116 | |
| 117 | - if(!is_a($handler, MultiResponseHandlerInterface::class)){ |
|
| 117 | + if (!is_a($handler, MultiResponseHandlerInterface::class)) { |
|
| 118 | 118 | throw new RequestException('!is_a($handler)'); |
| 119 | 119 | } |
| 120 | 120 | |
@@ -131,9 +131,9 @@ discard block |
||
| 131 | 131 | * @return $this |
| 132 | 132 | * @throws \chillerlan\TinyCurl\RequestException |
| 133 | 133 | */ |
| 134 | - public function fetch(array $urls){ |
|
| 134 | + public function fetch(array $urls) { |
|
| 135 | 135 | |
| 136 | - if(empty($urls)){ |
|
| 136 | + if (empty($urls)) { |
|
| 137 | 137 | throw new RequestException('empty($urls)'); |
| 138 | 138 | } |
| 139 | 139 | |
@@ -142,12 +142,12 @@ discard block |
||
| 142 | 142 | |
| 143 | 143 | $url_count = count($this->stack); |
| 144 | 144 | |
| 145 | - if($this->options->window_size > $url_count){ |
|
| 145 | + if ($this->options->window_size > $url_count) { |
|
| 146 | 146 | $this->options->window_size = $url_count; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | // shoot out the first batch of requests |
| 150 | - array_map(function(){ |
|
| 150 | + array_map(function() { |
|
| 151 | 151 | $this->createHandle(); |
| 152 | 152 | }, range(1, $this->options->window_size)); |
| 153 | 153 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | * @see \chillerlan\TinyCurl\Response\MultiResponseHandlerInterface |
| 164 | 164 | * @return $this |
| 165 | 165 | */ |
| 166 | - public function addResponse($response){ |
|
| 166 | + public function addResponse($response) { |
|
| 167 | 167 | $this->responses[] = $response; |
| 168 | 168 | |
| 169 | 169 | return $this; |
@@ -172,24 +172,24 @@ discard block |
||
| 172 | 172 | /** |
| 173 | 173 | * @return array |
| 174 | 174 | */ |
| 175 | - public function getResponseData(){ |
|
| 175 | + public function getResponseData() { |
|
| 176 | 176 | return $this->responses; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | /** |
| 180 | 180 | * creates a new handle for $request[$index] |
| 181 | 181 | */ |
| 182 | - protected function createHandle(){ |
|
| 182 | + protected function createHandle() { |
|
| 183 | 183 | |
| 184 | - if(!empty($this->stack)){ |
|
| 184 | + if (!empty($this->stack)) { |
|
| 185 | 185 | $url = array_shift($this->stack); |
| 186 | 186 | |
| 187 | - if($url instanceof URL){ |
|
| 187 | + if ($url instanceof URL) { |
|
| 188 | 188 | $curl = curl_init($url); |
| 189 | 189 | curl_setopt_array($curl, $this->curl_options); |
| 190 | 190 | curl_multi_add_handle($this->curl_multi, $curl); |
| 191 | 191 | } |
| 192 | - else{ |
|
| 192 | + else { |
|
| 193 | 193 | // retry on next if we don't get what we expect |
| 194 | 194 | $this->createHandle(); |
| 195 | 195 | } |
@@ -201,19 +201,19 @@ discard block |
||
| 201 | 201 | /** |
| 202 | 202 | * processes the requests |
| 203 | 203 | */ |
| 204 | - protected function processStack(){ |
|
| 204 | + protected function processStack() { |
|
| 205 | 205 | |
| 206 | - do{ |
|
| 206 | + do { |
|
| 207 | 207 | |
| 208 | - if(curl_multi_exec($this->curl_multi, $active) !== CURLM_OK){ |
|
| 208 | + if (curl_multi_exec($this->curl_multi, $active) !== CURLM_OK) { |
|
| 209 | 209 | break; // @codeCoverageIgnore |
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | // welcome to callback hell. |
| 213 | - while($state = curl_multi_info_read($this->curl_multi)){ |
|
| 213 | + while ($state = curl_multi_info_read($this->curl_multi)) { |
|
| 214 | 214 | $url = $this->multiResponseHandler->handleResponse(new MultiResponse($state['handle'])); |
| 215 | 215 | |
| 216 | - if($url instanceof URL){ |
|
| 216 | + if ($url instanceof URL) { |
|
| 217 | 217 | $this->stack[] = $url; |
| 218 | 218 | } |
| 219 | 219 | |
@@ -221,12 +221,12 @@ discard block |
||
| 221 | 221 | curl_multi_remove_handle($this->curl_multi, $state['handle']); |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - if($active){ |
|
| 224 | + if ($active) { |
|
| 225 | 225 | curl_multi_select($this->curl_multi, $this->options->timeout); |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | } |
| 229 | - while($active); |
|
| 229 | + while ($active); |
|
| 230 | 230 | |
| 231 | 231 | } |
| 232 | 232 | |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | /** |
| 15 | 15 | * Class MultiRequestOptions |
| 16 | 16 | */ |
| 17 | -class MultiRequestOptions extends RequestOptions{ |
|
| 17 | +class MultiRequestOptions extends RequestOptions { |
|
| 18 | 18 | |
| 19 | 19 | public $handler = null; |
| 20 | 20 | |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | /** |
| 19 | 19 | * |
| 20 | 20 | */ |
| 21 | -class Request{ |
|
| 21 | +class Request { |
|
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * The cURL connection |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @param \chillerlan\TinyCurl\RequestOptions $options |
| 39 | 39 | */ |
| 40 | - public function __construct(RequestOptions $options = null){ |
|
| 40 | + public function __construct(RequestOptions $options = null) { |
|
| 41 | 41 | |
| 42 | - if(!$options){ |
|
| 42 | + if (!$options) { |
|
| 43 | 43 | $options = new RequestOptions; |
| 44 | 44 | } |
| 45 | 45 | |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @return ResponseInterface |
| 53 | 53 | */ |
| 54 | - protected function getResponse($url){ |
|
| 54 | + protected function getResponse($url) { |
|
| 55 | 55 | $ca_info = is_file($this->options->ca_info) ? $this->options->ca_info : null; |
| 56 | 56 | |
| 57 | 57 | curl_setopt_array($this->curl, $this->options->curl_options + [ |
@@ -71,10 +71,10 @@ discard block |
||
| 71 | 71 | * @return \chillerlan\TinyCurl\Response\ResponseInterface |
| 72 | 72 | * @throws \chillerlan\TinyCurl\RequestException |
| 73 | 73 | */ |
| 74 | - public function fetch(URL $url){ |
|
| 74 | + public function fetch(URL $url) { |
|
| 75 | 75 | $this->curl = curl_init(); |
| 76 | 76 | |
| 77 | - if(!$url->host || !in_array($url->scheme, ['http', 'https', 'ftp'], true)){ |
|
| 77 | + if (!$url->host || !in_array($url->scheme, ['http', 'https', 'ftp'], true)) { |
|
| 78 | 78 | throw new RequestException('$url'); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -86,10 +86,10 @@ discard block |
||
| 86 | 86 | * |
| 87 | 87 | * @return array |
| 88 | 88 | */ |
| 89 | - public function extractShortUrl($url){ |
|
| 89 | + public function extractShortUrl($url) { |
|
| 90 | 90 | $urls = [$url]; |
| 91 | 91 | |
| 92 | - while($url = $this->extract($url)){ |
|
| 92 | + while ($url = $this->extract($url)) { |
|
| 93 | 93 | $urls[] = $url; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @return string|bool |
| 103 | 103 | * @link http://www.internoetics.com/2012/11/12/resolve-short-urls-to-their-destination-url-php-api/ |
| 104 | 104 | */ |
| 105 | - protected function extract($url){ |
|
| 105 | + protected function extract($url) { |
|
| 106 | 106 | $this->curl = curl_init(); |
| 107 | 107 | |
| 108 | 108 | curl_setopt_array($this->curl, [ |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | $info = $response->info; |
| 115 | 115 | $headers = $response->headers; |
| 116 | 116 | |
| 117 | - switch(true){ |
|
| 117 | + switch (true) { |
|
| 118 | 118 | // check curl_info() |
| 119 | 119 | case in_array($info->http_code, range(300, 308), true) && isset($info->redirect_url) && !empty($info->redirect_url): |
| 120 | 120 | return $info->redirect_url; |
@@ -16,6 +16,6 @@ |
||
| 16 | 16 | /** |
| 17 | 17 | * Class RequestException |
| 18 | 18 | */ |
| 19 | -class RequestException extends Exception{ |
|
| 19 | +class RequestException extends Exception { |
|
| 20 | 20 | |
| 21 | 21 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | /** |
| 15 | 15 | * Class RequestOptions |
| 16 | 16 | */ |
| 17 | -class RequestOptions{ |
|
| 17 | +class RequestOptions { |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * options for each curl instance |
@@ -15,19 +15,19 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * |
| 17 | 17 | */ |
| 18 | -class MultiResponse extends Response implements ResponseInterface{ |
|
| 18 | +class MultiResponse extends Response implements ResponseInterface { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * Fills self::$response_body and calls self::getInfo() |
| 22 | 22 | */ |
| 23 | - protected function exec(){ |
|
| 23 | + protected function exec() { |
|
| 24 | 24 | $response = explode("\r\n\r\n", curl_multi_getcontent($this->curl), 2); |
| 25 | 25 | $headers = isset($response[0]) ? explode("\r\n", $response[0]) : null; |
| 26 | 26 | $this->response_body = isset($response[1]) ? $response[1] : null; |
| 27 | 27 | $this->getInfo(); |
| 28 | 28 | |
| 29 | - if(is_array($headers)){ |
|
| 30 | - foreach($headers as $line){ |
|
| 29 | + if (is_array($headers)) { |
|
| 30 | + foreach ($headers as $line) { |
|
| 31 | 31 | $this->headerLine(null, $line); |
| 32 | 32 | } |
| 33 | 33 | } |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * |
| 17 | 17 | */ |
| 18 | -interface MultiResponseHandlerInterface{ |
|
| 18 | +interface MultiResponseHandlerInterface { |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * The response handler. |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | /** |
| 18 | 18 | * |
| 19 | 19 | */ |
| 20 | -class Response implements ResponseInterface{ |
|
| 20 | +class Response implements ResponseInterface { |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * @var resource |
@@ -51,14 +51,14 @@ discard block |
||
| 51 | 51 | * |
| 52 | 52 | * @throws \chillerlan\TinyCurl\Response\ResponseException |
| 53 | 53 | */ |
| 54 | - public function __construct($curl){ |
|
| 54 | + public function __construct($curl) { |
|
| 55 | 55 | |
| 56 | - if(!$curl){ |
|
| 56 | + if (!$curl) { |
|
| 57 | 57 | throw new ResponseException('!$curl'); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | $this->curl = $curl; |
| 61 | - $this->curl_info = new stdClass; |
|
| 61 | + $this->curl_info = new stdClass; |
|
| 62 | 62 | $this->response_error = new stdClass; |
| 63 | 63 | $this->response_headers = new stdClass; |
| 64 | 64 | |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | /** |
| 69 | 69 | * Farewell. |
| 70 | 70 | */ |
| 71 | - public function __destruct(){ |
|
| 72 | - if($this->curl){ |
|
| 71 | + public function __destruct() { |
|
| 72 | + if ($this->curl) { |
|
| 73 | 73 | curl_close($this->curl); |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -80,9 +80,9 @@ discard block |
||
| 80 | 80 | * @return mixed |
| 81 | 81 | * @throws \chillerlan\TinyCurl\Response\ResponseException |
| 82 | 82 | */ |
| 83 | - public function __get($property){ |
|
| 83 | + public function __get($property) { |
|
| 84 | 84 | |
| 85 | - switch($property){ |
|
| 85 | + switch ($property) { |
|
| 86 | 86 | case 'body' : return $this->getBody(); |
| 87 | 87 | case 'info' : return $this->curl_info; |
| 88 | 88 | case 'json' : return json_decode($this->response_body); |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | /** |
| 97 | 97 | * Fills self::$response_body and calls self::getInfo() |
| 98 | 98 | */ |
| 99 | - protected function exec(){ |
|
| 99 | + protected function exec() { |
|
| 100 | 100 | curl_setopt($this->curl, CURLOPT_HEADERFUNCTION, [$this, 'headerLine']); |
| 101 | 101 | $this->response_body = curl_exec($this->curl); |
| 102 | 102 | $this->getInfo(); |
@@ -110,9 +110,9 @@ discard block |
||
| 110 | 110 | * |
| 111 | 111 | * @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
| 112 | 112 | */ |
| 113 | - protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
|
| 113 | + protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line) { |
|
| 114 | 114 | |
| 115 | - if(substr($header_line, 0, 4) === 'HTTP'){ |
|
| 115 | + if (substr($header_line, 0, 4) === 'HTTP') { |
|
| 116 | 116 | $status = explode(' ', $header_line, 3); |
| 117 | 117 | |
| 118 | 118 | $this->response_headers->httpversion = explode('/', $status[0], 2)[1]; |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | $h = explode(':', $header_line, 2); |
| 124 | - if(count($h) === 2){ |
|
| 124 | + if (count($h) === 2) { |
|
| 125 | 125 | $this->response_headers->{trim(strtolower($h[0]))} = trim($h[1]); |
| 126 | 126 | } |
| 127 | 127 | |
@@ -131,17 +131,17 @@ discard block |
||
| 131 | 131 | /** |
| 132 | 132 | * @return \stdClass |
| 133 | 133 | */ |
| 134 | - protected function getBody(){ |
|
| 134 | + protected function getBody() { |
|
| 135 | 135 | $body = new stdClass; |
| 136 | 136 | |
| 137 | 137 | $body->content = $this->response_body; |
| 138 | 138 | $body->length = strlen($this->response_body); |
| 139 | 139 | |
| 140 | - if(isset($this->curl_info->content_type) && !empty($this->curl_info->content_type)){ |
|
| 140 | + if (isset($this->curl_info->content_type) && !empty($this->curl_info->content_type)) { |
|
| 141 | 141 | $body->content_type = $this->curl_info->content_type; |
| 142 | 142 | } |
| 143 | 143 | // @codeCoverageIgnoreStart |
| 144 | - elseif(isset($this->response_headers->content_type) && !empty($this->response_headers->content_type)){ |
|
| 144 | + elseif (isset($this->response_headers->content_type) && !empty($this->response_headers->content_type)) { |
|
| 145 | 145 | $body->content_type = $this->response_headers->content_type; |
| 146 | 146 | } |
| 147 | 147 | // @codeCoverageIgnoreEnd |
@@ -152,10 +152,10 @@ discard block |
||
| 152 | 152 | /** |
| 153 | 153 | * |
| 154 | 154 | */ |
| 155 | - protected function getInfo(){ |
|
| 155 | + protected function getInfo() { |
|
| 156 | 156 | $curl_info = curl_getinfo($this->curl); |
| 157 | - if(is_array($curl_info)){ |
|
| 158 | - foreach($curl_info as $key => $value){ |
|
| 157 | + if (is_array($curl_info)) { |
|
| 158 | + foreach ($curl_info as $key => $value) { |
|
| 159 | 159 | $this->curl_info->{$key} = $value; |
| 160 | 160 | } |
| 161 | 161 | } |
@@ -16,6 +16,6 @@ |
||
| 16 | 16 | /** |
| 17 | 17 | * Class ResponseException |
| 18 | 18 | */ |
| 19 | -class ResponseException extends Exception{ |
|
| 19 | +class ResponseException extends Exception { |
|
| 20 | 20 | |
| 21 | 21 | } |