@@ -3,7 +3,6 @@ |
||
3 | 3 | require __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | use mpyw\Co\Co; |
6 | -use mpyw\Co\CURLException; |
|
7 | 6 | |
8 | 7 | function curl_init_with($url, array $options = [CURLOPT_RETURNTRANSFER => true]) { |
9 | 8 | $ch = curl_init($url); |
@@ -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 | yield Co::RETURN_WITH => 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 | $xpath = (yield get_xpath_async('https://github.com/mpyw')); |
26 | 26 | yield curl_init_with( |
27 | 27 | $xpath->evaluate('string(//img[contains(@class,"avatar")]/@src)'), |
@@ -3,7 +3,6 @@ |
||
3 | 3 | require __DIR__ . '/../vendor/autoload.php'; |
4 | 4 | |
5 | 5 | use mpyw\Co\Co; |
6 | -use mpyw\Co\CURLException; |
|
7 | 6 | |
8 | 7 | function curl_init_with($url, array $options = [CURLOPT_RETURNTRANSFER => true]) { |
9 | 8 | $ch = curl_init($url); |
@@ -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)'), |
@@ -12,6 +12,10 @@ discard block |
||
12 | 12 | throw new \RuntimeException($error['message']); |
13 | 13 | } |
14 | 14 | } |
15 | +/** |
|
16 | + * @param integer $status |
|
17 | + * @param string $message |
|
18 | + */ |
|
15 | 19 | function respond_rest($con, $status, $message, $content) { |
16 | 20 | try { |
17 | 21 | $length = strlen($content); |
@@ -24,6 +28,11 @@ discard block |
||
24 | 28 | } |
25 | 29 | fclose($con); |
26 | 30 | } |
31 | +/** |
|
32 | + * @param Closure $tick_function |
|
33 | + * @param integer $tick |
|
34 | + * @param integer $times |
|
35 | + */ |
|
27 | 36 | function respond_streaming($con, $tick_function, $tick, $times) { |
28 | 37 | try { |
29 | 38 | fsend($con, "HTTP/1.1 200 OK\r\n"); |
@@ -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); |
@@ -62,7 +62,6 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @access public |
64 | 64 | * @static |
65 | - * @param int $concurrency |
|
66 | 65 | * @param bool $throw |
67 | 66 | */ |
68 | 67 | public static function setDefaultThrow($throw) |
@@ -451,7 +450,7 @@ discard block |
||
451 | 450 | * |
452 | 451 | * @access private |
453 | 452 | * @param Generator $value |
454 | - * @return bool |
|
453 | + * @return boolean|null |
|
455 | 454 | */ |
456 | 455 | private function canThrow(\Generator $value) |
457 | 456 | { |
@@ -514,7 +513,7 @@ discard block |
||
514 | 513 | * @access private |
515 | 514 | * @static |
516 | 515 | * @param int|float|string $interval |
517 | - * @return int |
|
516 | + * @return double |
|
518 | 517 | */ |
519 | 518 | private static function validateInterval($interval) |
520 | 519 | { |
@@ -45,17 +45,17 @@ |
||
45 | 45 | * - "wait" (Co::wait calls) |
46 | 46 | * - "async" (Co::async calls) |
47 | 47 | */ |
48 | - private $mh; // curl_multi_init() |
|
49 | - private $count = 0; // count(curl_multi_add_handle called) |
|
50 | - private $concurrency = 6; // Limit of TCP connections |
|
51 | - private $interval = 1.0; // curl_multi_select() timeout |
|
52 | - private $throw = true; // Throw CURLExceptions? |
|
53 | - private $queue = array(); // cURL resources over concurrency limits are temporalily stored here |
|
54 | - private $tree = array(); // array<*Stack ID*, mixed> |
|
55 | - private $values = array(); // array<*Stack ID*|*cURL ID*, Generator|resource<cURL>> |
|
56 | - private $value_to_parent = array(); // array<*Stack ID*|*cURL ID*, *Stack ID*> |
|
48 | + private $mh; // curl_multi_init() |
|
49 | + private $count = 0; // count(curl_multi_add_handle called) |
|
50 | + private $concurrency = 6; // Limit of TCP connections |
|
51 | + private $interval = 1.0; // curl_multi_select() timeout |
|
52 | + private $throw = true; // Throw CURLExceptions? |
|
53 | + private $queue = array(); // cURL resources over concurrency limits are temporalily stored here |
|
54 | + private $tree = array(); // array<*Stack ID*, mixed> |
|
55 | + private $values = array(); // array<*Stack ID*|*cURL ID*, Generator|resource<cURL>> |
|
56 | + private $value_to_parent = array(); // array<*Stack ID*|*cURL ID*, *Stack ID*> |
|
57 | 57 | private $value_to_children = array(); // array<*Stack ID*, array<*Stack ID*|*cURL ID*, true>> |
58 | - private $value_to_keylist = array(); // array<*Stack ID*|*cURL ID*, array<mixed>> |
|
58 | + private $value_to_keylist = array(); // array<*Stack ID*|*cURL ID*, array<mixed>> |
|
59 | 59 | |
60 | 60 | /** |
61 | 61 | * Override or get default settings. |
@@ -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) { |
@@ -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 | }, |
@@ -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 | } |
@@ -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( |