@@ -190,7 +190,7 @@ |
||
190 | 190 | /** |
191 | 191 | * Write some info to the CLI |
192 | 192 | * |
193 | - * @param $str |
|
193 | + * @param string $str |
|
194 | 194 | */ |
195 | 195 | protected function logToCLI($str){ |
196 | 196 | echo '['.date('c', time()).']'.sprintf('[%10ss] ', sprintf('%01.4f', microtime(true) - $this->starttime)).$str.PHP_EOL; |
@@ -14,7 +14,6 @@ |
||
14 | 14 | use chillerlan\Database\Drivers\MySQLi\MySQLiDriver; |
15 | 15 | use chillerlan\Database\Traits\DatabaseTrait; |
16 | 16 | use chillerlan\Database\DBOptions; |
17 | -use chillerlan\Database\Drivers\PDO\PDOMySQLDriver; |
|
18 | 17 | use chillerlan\TinyCurl\MultiRequest; |
19 | 18 | use chillerlan\TinyCurl\MultiRequestOptions; |
20 | 19 | use chillerlan\TinyCurl\Request; |
@@ -206,11 +206,11 @@ |
||
206 | 206 | }, self::API_LANGUAGES); |
207 | 207 | |
208 | 208 | $sql = 'CREATE TEMPORARY TABLE IF NOT EXISTS `'.self::TEMP_TABLE.'` (' |
209 | - .'`id` int(10) unsigned NOT NULL,' |
|
210 | - .substr(implode(' ', $sql_lang), 0, -1) |
|
211 | - .' `updated` tinyint(1) unsigned NOT NULL DEFAULT 0,' |
|
212 | - .'`response_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,' |
|
213 | - .'PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin'; |
|
209 | + .'`id` int(10) unsigned NOT NULL,' |
|
210 | + .substr(implode(' ', $sql_lang), 0, -1) |
|
211 | + .' `updated` tinyint(1) unsigned NOT NULL DEFAULT 0,' |
|
212 | + .'`response_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,' |
|
213 | + .'PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin'; |
|
214 | 214 | $this->DBDriverInterface->raw('DROP TEMPORARY TABLE IF EXISTS `'.self::TEMP_TABLE.'`'); |
215 | 215 | $this->DBDriverInterface->raw($sql); |
216 | 216 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * Class ItemMultiResponseHandler |
29 | 29 | */ |
30 | -class ItemMultiResponseHandler implements MultiResponseHandlerInterface{ |
|
30 | +class ItemMultiResponseHandler implements MultiResponseHandlerInterface { |
|
31 | 31 | use DatabaseTrait; |
32 | 32 | |
33 | 33 | /** |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | * |
74 | 74 | * @param \chillerlan\TinyCurl\MultiRequest $multiRequest |
75 | 75 | */ |
76 | - public function __construct(MultiRequest $multiRequest = null){ |
|
76 | + public function __construct(MultiRequest $multiRequest = null) { |
|
77 | 77 | $this->multiRequest = $multiRequest; |
78 | 78 | |
79 | 79 | (new Dotenv(self::CONFIGDIR))->load(); |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | /** |
93 | 93 | * start the mayhem |
94 | 94 | */ |
95 | - public function init(){ |
|
95 | + public function init() { |
|
96 | 96 | $this->createTempTable(); |
97 | 97 | $this->getURLs(); |
98 | 98 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * @return bool|\chillerlan\TinyCurl\URL |
134 | 134 | * @internal |
135 | 135 | */ |
136 | - public function handleResponse(ResponseInterface $response){ |
|
136 | + public function handleResponse(ResponseInterface $response) { |
|
137 | 137 | $info = $response->info; |
138 | 138 | $this->callback++; |
139 | 139 | |
@@ -141,11 +141,11 @@ discard block |
||
141 | 141 | parse_str(parse_url($info->url, PHP_URL_QUERY), $params); |
142 | 142 | |
143 | 143 | // there be dragons. |
144 | - if(in_array($info->http_code, [200, 206], true)){ |
|
144 | + if (in_array($info->http_code, [200, 206], true)) { |
|
145 | 145 | $lang = $response->headers->{'content-language'} ?: $params['lang']; |
146 | 146 | |
147 | 147 | // discard the response when it's impossible to determine the language |
148 | - if(!in_array($lang, self::API_LANGUAGES)){ |
|
148 | + if (!in_array($lang, self::API_LANGUAGES)) { |
|
149 | 149 | $this->logToCLI('URL discarded. ('.$info->url.')'); |
150 | 150 | return false; |
151 | 151 | } |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | $sql = 'UPDATE '.self::TEMP_TABLE.' SET `'.$lang.'` = ? WHERE `id` = ?'; |
154 | 154 | $values = []; |
155 | 155 | |
156 | - foreach($response->json as $item){ |
|
156 | + foreach ($response->json as $item) { |
|
157 | 157 | # $this->logToCLI(str_pad($item->id, 5).' - '.$item->name); |
158 | 158 | // just dumping the raw JSON for each item here because i'm lazy (or to process the itemdata later) |
159 | 159 | $values[] = [json_encode($item), $item->id]; |
@@ -161,10 +161,10 @@ discard block |
||
161 | 161 | |
162 | 162 | // insert the data as soon as we receive it |
163 | 163 | // this will result in a couple more database writes but won't block the responses much |
164 | - if($this->DBDriverInterface->multi($sql, $values)){ |
|
165 | - $this->logToCLI('['.str_pad($this->callback, 6, ' ',STR_PAD_RIGHT).']['.$lang.'] '.md5($response->info->url).' updated'); |
|
164 | + if ($this->DBDriverInterface->multi($sql, $values)) { |
|
165 | + $this->logToCLI('['.str_pad($this->callback, 6, ' ', STR_PAD_RIGHT).']['.$lang.'] '.md5($response->info->url).' updated'); |
|
166 | 166 | } |
167 | - else{ |
|
167 | + else { |
|
168 | 168 | // retry if the insert failed for whatever reason |
169 | 169 | $this->logToCLI('SQL insert failed, retrying URL. ('.$info->url.')'); |
170 | 170 | return new URL($info->url); |
@@ -175,12 +175,12 @@ discard block |
||
175 | 175 | } |
176 | 176 | // instant retry on a 502 |
177 | 177 | // https://gitter.im/arenanet/api-cdi?at=56c3ba6ba5bdce025f69bcc8 |
178 | - else if($info->http_code === 502){ |
|
178 | + else if ($info->http_code === 502) { |
|
179 | 179 | $this->logToCLI('URL readded due to a 502. ('.$info->url.')'); |
180 | 180 | return new URL($info->url); |
181 | 181 | } |
182 | 182 | // examine and add the failed response to retry later @todo |
183 | - else{ |
|
183 | + else { |
|
184 | 184 | $this->logToCLI('('.$info->url.')'); |
185 | 185 | return false; |
186 | 186 | } |
@@ -192,16 +192,16 @@ discard block |
||
192 | 192 | * |
193 | 193 | * @param $str |
194 | 194 | */ |
195 | - protected function logToCLI($str){ |
|
195 | + protected function logToCLI($str) { |
|
196 | 196 | echo '['.date('c', time()).']'.sprintf('[%10ss] ', sprintf('%01.4f', microtime(true) - $this->starttime)).$str.PHP_EOL; |
197 | 197 | } |
198 | 198 | |
199 | 199 | /** |
200 | 200 | * Creates a temporary table to receive the item responses on the fly |
201 | 201 | */ |
202 | - protected function createTempTable(){ |
|
202 | + protected function createTempTable() { |
|
203 | 203 | |
204 | - $sql_lang = array_map(function($lang){ |
|
204 | + $sql_lang = array_map(function($lang) { |
|
205 | 205 | return '`'.$lang.'` text COLLATE utf8mb4_bin NOT NULL, '; |
206 | 206 | }, self::API_LANGUAGES); |
207 | 207 | |
@@ -218,17 +218,17 @@ discard block |
||
218 | 218 | /** |
219 | 219 | * @throws \chillerlan\TinyCurl\RequestException |
220 | 220 | */ |
221 | - protected function getURLs(){ |
|
221 | + protected function getURLs() { |
|
222 | 222 | $this->starttime = microtime(true); |
223 | 223 | $this->logToCLI('self::getURLs() fetch'); |
224 | 224 | |
225 | 225 | $response = (new Request)->fetch(new URL('https://api.guildwars2.com/v2/items')); |
226 | 226 | |
227 | - if($response->info->http_code !== 200){ |
|
227 | + if ($response->info->http_code !== 200) { |
|
228 | 228 | throw new Exception('failed to get /v2/items'); |
229 | 229 | } |
230 | 230 | |
231 | - $values = array_map(function($item){ |
|
231 | + $values = array_map(function($item) { |
|
232 | 232 | return [$item]; |
233 | 233 | }, $response->json); |
234 | 234 | |
@@ -238,8 +238,8 @@ discard block |
||
238 | 238 | |
239 | 239 | $chunks = array_chunk($response->json, self::CHUNK_SIZE); |
240 | 240 | |
241 | - array_map(function($chunk){ |
|
242 | - foreach(self::API_LANGUAGES as $lang){ |
|
241 | + array_map(function($chunk) { |
|
242 | + foreach (self::API_LANGUAGES as $lang) { |
|
243 | 243 | $this->urls[] = new URL(self::API_BASE.'?lang='.$lang.'&ids='.implode(',', $chunk)); |
244 | 244 | } |
245 | 245 | }, $chunks); |
@@ -163,8 +163,7 @@ |
||
163 | 163 | // this will result in a couple more database writes but won't block the responses much |
164 | 164 | if($this->DBDriverInterface->multi($sql, $values)){ |
165 | 165 | $this->logToCLI('['.str_pad($this->callback, 6, ' ',STR_PAD_RIGHT).']['.$lang.'] '.md5($response->info->url).' updated'); |
166 | - } |
|
167 | - else{ |
|
166 | + } else{ |
|
168 | 167 | // retry if the insert failed for whatever reason |
169 | 168 | $this->logToCLI('SQL insert failed, retrying URL. ('.$info->url.')'); |
170 | 169 | return new URL($info->url); |
@@ -15,15 +15,15 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @return array |
17 | 17 | */ |
18 | -if(!function_exists('array_sort_recursive')){ |
|
18 | +if (!function_exists('array_sort_recursive')) { |
|
19 | 19 | |
20 | - function array_sort_recursive(array $array){ |
|
20 | + function array_sort_recursive(array $array) { |
|
21 | 21 | array_multisort($array); |
22 | 22 | ksort($array); |
23 | 23 | |
24 | - foreach($array as $key => $value){ |
|
24 | + foreach ($array as $key => $value) { |
|
25 | 25 | |
26 | - if(is_array($value)){ |
|
26 | + if (is_array($value)) { |
|
27 | 27 | $array[$key] = array_sort_recursive($value); |
28 | 28 | } |
29 | 29 | |
@@ -41,27 +41,27 @@ discard block |
||
41 | 41 | * @return array |
42 | 42 | * @link http://php.net/manual/function.array-diff-assoc.php#111675 |
43 | 43 | */ |
44 | -if(!function_exists('array_diff_assoc_recursive')){ |
|
44 | +if (!function_exists('array_diff_assoc_recursive')) { |
|
45 | 45 | |
46 | - function array_diff_assoc_recursive(array $arr1, array $arr2, $identical = false){ |
|
46 | + function array_diff_assoc_recursive(array $arr1, array $arr2, $identical = false) { |
|
47 | 47 | $diff = $identical ? array_diff_key($arr2, $arr1) : []; |
48 | 48 | |
49 | - foreach($arr1 as $key => $value){ |
|
49 | + foreach ($arr1 as $key => $value) { |
|
50 | 50 | |
51 | - if(is_array($value)){ |
|
51 | + if (is_array($value)) { |
|
52 | 52 | |
53 | - if(!isset($arr2[$key]) || !is_array($arr2[$key])){ |
|
53 | + if (!isset($arr2[$key]) || !is_array($arr2[$key])) { |
|
54 | 54 | $diff[$key] = $value; |
55 | 55 | } |
56 | - else{ |
|
56 | + else { |
|
57 | 57 | $new_diff = array_diff_assoc_recursive($value, $arr2[$key], $identical); |
58 | - if(!empty($new_diff)){ |
|
58 | + if (!empty($new_diff)) { |
|
59 | 59 | $diff[$key] = $new_diff; |
60 | 60 | } |
61 | 61 | } |
62 | 62 | |
63 | 63 | } |
64 | - else if(!array_key_exists($key, $arr2) || $arr2[$key] !== $value){ |
|
64 | + else if (!array_key_exists($key, $arr2) || $arr2[$key] !== $value) { |
|
65 | 65 | $diff[$key] = $value; |
66 | 66 | } |
67 | 67 | |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | /** |
76 | 76 | * Checks wether the script is running in CLI mode. |
77 | 77 | */ |
78 | -if(!function_exists('is_cli')){ |
|
79 | - function is_cli(){ |
|
78 | +if (!function_exists('is_cli')) { |
|
79 | + function is_cli() { |
|
80 | 80 | return !isset($_SERVER['SERVER_SOFTWARE']) && (PHP_SAPI === 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)); |
81 | 81 | } |
82 | 82 | } |
@@ -52,16 +52,14 @@ |
||
52 | 52 | |
53 | 53 | if(!isset($arr2[$key]) || !is_array($arr2[$key])){ |
54 | 54 | $diff[$key] = $value; |
55 | - } |
|
56 | - else{ |
|
55 | + } else{ |
|
57 | 56 | $new_diff = array_diff_assoc_recursive($value, $arr2[$key], $identical); |
58 | 57 | if(!empty($new_diff)){ |
59 | 58 | $diff[$key] = $new_diff; |
60 | 59 | } |
61 | 60 | } |
62 | 61 | |
63 | - } |
|
64 | - else if(!array_key_exists($key, $arr2) || $arr2[$key] !== $value){ |
|
62 | + } else if(!array_key_exists($key, $arr2) || $arr2[$key] !== $value){ |
|
65 | 63 | $diff[$key] = $value; |
66 | 64 | } |
67 | 65 |
@@ -1,12 +1,12 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * |
|
4 | - * @filesource gw2api.php |
|
5 | - * @created 17.02.2016 |
|
6 | - * @author Smiley <[email protected]> |
|
7 | - * @copyright 2016 Smiley |
|
8 | - * @license MIT |
|
9 | - */ |
|
3 | + * |
|
4 | + * @filesource gw2api.php |
|
5 | + * @created 17.02.2016 |
|
6 | + * @author Smiley <[email protected]> |
|
7 | + * @copyright 2016 Smiley |
|
8 | + * @license MIT |
|
9 | + */ |
|
10 | 10 | |
11 | 11 | namespace Example; |
12 | 12 |
@@ -15,7 +15,7 @@ |
||
15 | 15 | require_once '../vendor/autoload.php'; |
16 | 16 | require_once 'functions.php'; |
17 | 17 | |
18 | -if(!is_cli()){ |
|
18 | +if (!is_cli()) { |
|
19 | 19 | throw new \Exception('no way, buddy.'); |
20 | 20 | } |
21 | 21 |
@@ -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 |
@@ -188,8 +188,7 @@ |
||
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 | - } |
|
192 | - else{ |
|
191 | + } else{ |
|
193 | 192 | // retry on next if we don't get what we expect |
194 | 193 | $this->createHandle(); |
195 | 194 | } |
@@ -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 | } |