@@ -19,7 +19,7 @@ |
||
| 19 | 19 | $ch = curl_init(); |
| 20 | 20 | curl_setopt_array($ch, array( |
| 21 | 21 | CURLOPT_URL => "http://localhost:8080$path?" . http_build_query($q, '', '&'), |
| 22 | - CURLOPT_WRITEFUNCTION => function ($ch, $buf) use ($callback) { |
|
| 22 | + CURLOPT_WRITEFUNCTION => function($ch, $buf) use ($callback) { |
|
| 23 | 23 | $callback($buf); |
| 24 | 24 | return strlen($buf); |
| 25 | 25 | }, |
@@ -68,21 +68,21 @@ |
||
| 68 | 68 | continue; |
| 69 | 69 | } |
| 70 | 70 | $endpoints = array( |
| 71 | - '/rest' => function ($con, $q) { |
|
| 71 | + '/rest' => function($con, $q) { |
|
| 72 | 72 | $sleep = isset($q['sleep']) ? (int)$q['sleep'] : 0; |
| 73 | 73 | sleep($sleep); |
| 74 | 74 | $id = isset($q['id']) ? (int)$q['id'] : '?'; |
| 75 | 75 | respond_rest($con, 200, 'OK', "Rest response #$id (sleep: $sleep sec)\n"); |
| 76 | 76 | }, |
| 77 | - '/streaming' => function ($con, $q) { |
|
| 77 | + '/streaming' => function($con, $q) { |
|
| 78 | 78 | $tick = isset($q['tick']) ? (int)$q['tick'] : 1; |
| 79 | 79 | $id = isset($q['id']) ? (int)$q['id'] : '?'; |
| 80 | 80 | $times = isset($q['times']) ? (int)$q['times'] : 10; |
| 81 | - respond_streaming($con, function ($i) use ($id, $times) { |
|
| 81 | + respond_streaming($con, function($i) use ($id, $times) { |
|
| 82 | 82 | return "Rest response #$id ($i / $times, tick: $tick sec)\n"; |
| 83 | 83 | }, $tick, $times); |
| 84 | 84 | }, |
| 85 | - '' => function ($con, $q) { |
|
| 85 | + '' => function($con, $q) { |
|
| 86 | 86 | respond_rest($con, 404, 'Not Found', "Undefined path\n"); |
| 87 | 87 | }, |
| 88 | 88 | ); |
@@ -55,8 +55,9 @@ discard block |
||
| 55 | 55 | $con = @stream_socket_accept($socket, -1); |
| 56 | 56 | if (!$con) { |
| 57 | 57 | $err = error_get_last(); // どーなんだこれ strerror(EINTR) |
| 58 | - if (strpos($err["message"], "Interrupted system call") !== false) |
|
| 59 | - continue; |
|
| 58 | + if (strpos($err["message"], "Interrupted system call") !== false) { |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 60 | 61 | fwrite(STDERR, "[Server] " . $err["message"] . "\n"); |
| 61 | 62 | exit(1); |
| 62 | 63 | } |
@@ -91,7 +92,9 @@ discard block |
||
| 91 | 92 | parse_str(isset($parsed['query']) ? $parsed['query'] : '', $q); |
| 92 | 93 | foreach ($endpoints as $endpoint => $action) { |
| 93 | 94 | if ($parsed['path'] === $endpoint || $endpoint === '') { |
| 94 | - if ($endpoint === '') $endpoint = '[Undefined]'; |
|
| 95 | + if ($endpoint === '') { |
|
| 96 | + $endpoint = '[Undefined]'; |
|
| 97 | + } |
|
| 95 | 98 | fwrite(STDERR, "Request: $parts[1]\n"); |
| 96 | 99 | $action($con, $q); |
| 97 | 100 | exit(0); |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | // Wait 7 sec |
| 9 | 9 | print_time(); |
| 10 | -$result = Co::wait([curl('/rest', ['id' => 1, 'sleep' => 7]), function () { |
|
| 10 | +$result = Co::wait([curl('/rest', ['id' => 1, 'sleep' => 7]), function() { |
|
| 11 | 11 | // Wait 4 sec |
| 12 | 12 | print_r(yield [ |
| 13 | 13 | curl('/rest', ['id' => 2, 'sleep' => 3]), |
@@ -16,13 +16,13 @@ discard block |
||
| 16 | 16 | print_time(); |
| 17 | 17 | // Wait 2 sec |
| 18 | 18 | print_r(yield [ |
| 19 | - function () { |
|
| 19 | + function() { |
|
| 20 | 20 | // Wait 1 sec |
| 21 | 21 | echo yield curl('/rest', ['id' => 4, 'sleep' => 1]), "\n"; |
| 22 | 22 | print_time(); |
| 23 | 23 | return curl('/rest', ['id' => 5, 'sleep' => 1]); |
| 24 | 24 | }, |
| 25 | - function () { |
|
| 25 | + function() { |
|
| 26 | 26 | // Wait 0 sec |
| 27 | 27 | echo (yield CO::SAFE => curl_init('invaild'))->getMessage(), "\n"; |
| 28 | 28 | print_time(); |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | echo $e->getMessage(), "\n"; |
| 34 | 34 | print_time(); |
| 35 | 35 | } |
| 36 | - return ['x' => ['y' => function () { |
|
| 36 | + return ['x' => ['y' => function() { |
|
| 37 | 37 | return curl('/rest', ['id' => 6, 'sleep' => 2]); |
| 38 | 38 | }]]; |
| 39 | 39 | } |
@@ -18,10 +18,10 @@ |
||
| 18 | 18 | |
| 19 | 19 | var_dump(Co::wait([ |
| 20 | 20 | "google.com HTML" => curl_init_with("https://google.com"), |
| 21 | - "Content-Length of github.com" => function () { |
|
| 21 | + "Content-Length of github.com" => function() { |
|
| 22 | 22 | return strlen(yield curl_init_with("https://github.com")); |
| 23 | 23 | }, |
| 24 | - "Save mpyw's Gravatar Image URL to local" => function () { |
|
| 24 | + "Save mpyw's Gravatar Image URL to local" => function() { |
|
| 25 | 25 | yield curl_init_with( |
| 26 | 26 | (yield get_xpath_async('https://github.com/mpyw')) |
| 27 | 27 | ->evaluate('string(//img[contains(@class,"avatar")]/@src)'), |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | define('FAVORITE_REGEX', '/' . trim(fgets(STDIN)) . '/'); |
| 16 | 16 | |
| 17 | 17 | // Listen Twitter UserStreaming on 2 different accounts |
| 18 | -Co::wait(array_map(function ($to) { |
|
| 18 | +Co::wait(array_map(function($to) { |
|
| 19 | 19 | // You have to install (Go)mpyw/twhelp-go or (PHP)mpyw/twhelp. |
| 20 | 20 | // - Go: https://github.com/mpyw/twhelp-go |
| 21 | 21 | // - PHP: https://github.com/mpyw/twhelp |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | exit(1); |
| 25 | 25 | } |
| 26 | 26 | eval(implode($r)); |
| 27 | - return $to->curlStreaming('user', function ($status) use ($to) { |
|
| 27 | + return $to->curlStreaming('user', function($status) use ($to) { |
|
| 28 | 28 | if (isset($status->text)) { |
| 29 | 29 | if (preg_match(FAVORITE_REGEX, htmlspecialchars_decode($status->text, ENT_NOQUOTES))) { |
| 30 | 30 | Co::async($to->curlPost('favorites/create', array( |
@@ -323,8 +323,10 @@ |
||
| 323 | 323 | // NOTE: DO NOT call curl_multi_remove_handle |
| 324 | 324 | // or curl_multi_add_handle while looping curl_multi_info_read! |
| 325 | 325 | $entries = array(); |
| 326 | - do if ($entry = curl_multi_info_read($this->mh, $remains)) { |
|
| 326 | + do { |
|
| 327 | + if ($entry = curl_multi_info_read($this->mh, $remains)) { |
|
| 327 | 328 | $entries[] = $entry; |
| 329 | + } |
|
| 328 | 330 | } while ($remains); |
| 329 | 331 | // Remove done and consume queue. |
| 330 | 332 | foreach ($entries as $entry) { |
@@ -54,14 +54,14 @@ |
||
| 54 | 54 | * - "async" (Co::async calls) |
| 55 | 55 | */ |
| 56 | 56 | private $options = array(); |
| 57 | - private $mh; // curl_multi_init() |
|
| 58 | - private $count = 0; // count(curl_multi_add_handle called) |
|
| 59 | - private $queue = array(); // cURL resources over concurrency limits are temporalily stored here |
|
| 60 | - private $tree = array(); // array<*Stack ID*, mixed> |
|
| 61 | - private $values = array(); // array<*Stack ID*|*cURL ID*, Generator|resource<cURL>> |
|
| 62 | - private $value_to_parent = array(); // array<*Stack ID*|*cURL ID*, *Stack ID*> |
|
| 57 | + private $mh; // curl_multi_init() |
|
| 58 | + private $count = 0; // count(curl_multi_add_handle called) |
|
| 59 | + private $queue = array(); // cURL resources over concurrency limits are temporalily stored here |
|
| 60 | + private $tree = array(); // array<*Stack ID*, mixed> |
|
| 61 | + private $values = array(); // array<*Stack ID*|*cURL ID*, Generator|resource<cURL>> |
|
| 62 | + private $value_to_parent = array(); // array<*Stack ID*|*cURL ID*, *Stack ID*> |
|
| 63 | 63 | private $value_to_children = array(); // array<*Stack ID*, array<*Stack ID*|*cURL ID*, true>> |
| 64 | - private $value_to_keylist = array(); // array<*Stack ID*|*cURL ID*, array<mixed>> |
|
| 64 | + private $value_to_keylist = array(); // array<*Stack ID*|*cURL ID*, array<mixed>> |
|
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * Override or get default settings. |
@@ -18,10 +18,10 @@ |
||
| 18 | 18 | |
| 19 | 19 | var_dump(Co::wait([ |
| 20 | 20 | "google.com HTML" => curl_init_with("https://google.com"), |
| 21 | - "Content-Length of github.com" => function () { |
|
| 21 | + "Content-Length of github.com" => function() { |
|
| 22 | 22 | return strlen(yield curl_init_with("https://github.com")); |
| 23 | 23 | }, |
| 24 | - "Save mpyw's Gravatar Image URL to local" => function () { |
|
| 24 | + "Save mpyw's Gravatar Image URL to local" => function() { |
|
| 25 | 25 | yield curl_init_with( |
| 26 | 26 | (yield get_xpath_async('https://github.com/mpyw')) |
| 27 | 27 | ->evaluate('string(//img[contains(@class,"avatar")]/@src)'), |