@@ -15,120 +15,120 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class MultiRequest |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var [Response] |
|
| 20 | - */ |
|
| 21 | - protected static $requestPool; |
|
| 22 | - protected static $multiHandler; |
|
| 23 | - protected $defaultOptions = array(); |
|
| 24 | - private static $instance; |
|
| 25 | - protected static $loggerHandler; |
|
| 26 | - /** |
|
| 27 | - * MultiRequest constructor. |
|
| 28 | - */ |
|
| 29 | - protected function __construct() |
|
| 30 | - { |
|
| 31 | - } |
|
| 18 | + /** |
|
| 19 | + * @var [Response] |
|
| 20 | + */ |
|
| 21 | + protected static $requestPool; |
|
| 22 | + protected static $multiHandler; |
|
| 23 | + protected $defaultOptions = array(); |
|
| 24 | + private static $instance; |
|
| 25 | + protected static $loggerHandler; |
|
| 26 | + /** |
|
| 27 | + * MultiRequest constructor. |
|
| 28 | + */ |
|
| 29 | + protected function __construct() |
|
| 30 | + { |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @return MultiRequest |
|
| 35 | - */ |
|
| 36 | - public static function create() |
|
| 37 | - { |
|
| 38 | - if (!(self::$instance instanceof self)) { |
|
| 39 | - self::$instance = new self; |
|
| 40 | - } |
|
| 41 | - self::prepare(); |
|
| 42 | - return self::$instance; |
|
| 43 | - } |
|
| 33 | + /** |
|
| 34 | + * @return MultiRequest |
|
| 35 | + */ |
|
| 36 | + public static function create() |
|
| 37 | + { |
|
| 38 | + if (!(self::$instance instanceof self)) { |
|
| 39 | + self::$instance = new self; |
|
| 40 | + } |
|
| 41 | + self::prepare(); |
|
| 42 | + return self::$instance; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param array $options |
|
| 47 | - * @return $this |
|
| 48 | - */ |
|
| 49 | - public function setDefaults(array $options = array()){ |
|
| 50 | - $this->defaultOptions = $options; |
|
| 51 | - return $this; |
|
| 52 | - } |
|
| 53 | - protected static function prepare() |
|
| 54 | - { |
|
| 55 | - self::$multiHandler = curl_multi_init(); |
|
| 56 | - } |
|
| 45 | + /** |
|
| 46 | + * @param array $options |
|
| 47 | + * @return $this |
|
| 48 | + */ |
|
| 49 | + public function setDefaults(array $options = array()){ |
|
| 50 | + $this->defaultOptions = $options; |
|
| 51 | + return $this; |
|
| 52 | + } |
|
| 53 | + protected static function prepare() |
|
| 54 | + { |
|
| 55 | + self::$multiHandler = curl_multi_init(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param $method |
|
| 60 | - * @param $uri |
|
| 61 | - * @param $payload |
|
| 62 | - * @param array $options |
|
| 63 | - * @return $this |
|
| 64 | - */ |
|
| 65 | - public function add($method, $uri, $payload, array $options = array()) |
|
| 66 | - { |
|
| 67 | - $options = array( |
|
| 68 | - 'method' => $method, |
|
| 69 | - 'url' => $uri, |
|
| 70 | - 'data' => $payload, |
|
| 71 | - ) + $options; |
|
| 72 | - $this->addOptions(array($options)); |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 58 | + /** |
|
| 59 | + * @param $method |
|
| 60 | + * @param $uri |
|
| 61 | + * @param $payload |
|
| 62 | + * @param array $options |
|
| 63 | + * @return $this |
|
| 64 | + */ |
|
| 65 | + public function add($method, $uri, $payload, array $options = array()) |
|
| 66 | + { |
|
| 67 | + $options = array( |
|
| 68 | + 'method' => $method, |
|
| 69 | + 'url' => $uri, |
|
| 70 | + 'data' => $payload, |
|
| 71 | + ) + $options; |
|
| 72 | + $this->addOptions(array($options)); |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @param array $URLOptions |
|
| 78 | - * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d')) |
|
| 79 | - * @return $this |
|
| 80 | - */ |
|
| 81 | - public function addOptions(array $URLOptions) |
|
| 82 | - { |
|
| 83 | - foreach ($URLOptions as $options) { |
|
| 84 | - $options = $options + $this->defaultOptions; |
|
| 85 | - $request = Request::create()->addOptions($options)->applyOptions(); |
|
| 86 | - if (isset($options['callback'])) { |
|
| 87 | - $request->onEnd($options['callback']); |
|
| 88 | - } |
|
| 89 | - $this->import($request); |
|
| 90 | - } |
|
| 91 | - return $this; |
|
| 92 | - } |
|
| 76 | + /** |
|
| 77 | + * @param array $URLOptions |
|
| 78 | + * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d')) |
|
| 79 | + * @return $this |
|
| 80 | + */ |
|
| 81 | + public function addOptions(array $URLOptions) |
|
| 82 | + { |
|
| 83 | + foreach ($URLOptions as $options) { |
|
| 84 | + $options = $options + $this->defaultOptions; |
|
| 85 | + $request = Request::create()->addOptions($options)->applyOptions(); |
|
| 86 | + if (isset($options['callback'])) { |
|
| 87 | + $request->onEnd($options['callback']); |
|
| 88 | + } |
|
| 89 | + $this->import($request); |
|
| 90 | + } |
|
| 91 | + return $this; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @param Request $request |
|
| 96 | - * @return $this |
|
| 97 | - */ |
|
| 98 | - public function import(Request $request) |
|
| 99 | - { |
|
| 100 | - if (!is_resource($request->curlHandle)) { |
|
| 101 | - throw new InvalidArgumentException('Request curl handle is not initialized'); |
|
| 102 | - } |
|
| 103 | - curl_multi_add_handle(self::$multiHandler, $request->curlHandle); |
|
| 104 | - self::$requestPool[] = $request; |
|
| 105 | - return $this; |
|
| 106 | - } |
|
| 94 | + /** |
|
| 95 | + * @param Request $request |
|
| 96 | + * @return $this |
|
| 97 | + */ |
|
| 98 | + public function import(Request $request) |
|
| 99 | + { |
|
| 100 | + if (!is_resource($request->curlHandle)) { |
|
| 101 | + throw new InvalidArgumentException('Request curl handle is not initialized'); |
|
| 102 | + } |
|
| 103 | + curl_multi_add_handle(self::$multiHandler, $request->curlHandle); |
|
| 104 | + self::$requestPool[] = $request; |
|
| 105 | + return $this; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * @return array(Response) |
|
| 110 | - */ |
|
| 111 | - public function sendAll() |
|
| 112 | - { |
|
| 113 | - $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
| 114 | - do { |
|
| 115 | - curl_multi_exec(self::$multiHandler, $active); |
|
| 116 | - // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
|
| 117 | - // https://bugs.php.net/bug.php?id=63411 |
|
| 118 | - if (curl_multi_select(self::$multiHandler) == -1) { |
|
| 119 | - usleep($sleepTime); |
|
| 120 | - } |
|
| 121 | - usleep($sleepTime); |
|
| 122 | - } while ($active); |
|
| 123 | - $return = array(); |
|
| 124 | - foreach (self::$requestPool as $request) { |
|
| 125 | - $return[] = $request->send(true); |
|
| 126 | - curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); |
|
| 127 | - curl_close($request->curlHandle); |
|
| 128 | - } |
|
| 129 | - curl_multi_close(self::$multiHandler); |
|
| 130 | - self::$requestPool = null; |
|
| 131 | - return $return; |
|
| 132 | - } |
|
| 108 | + /** |
|
| 109 | + * @return array(Response) |
|
| 110 | + */ |
|
| 111 | + public function sendAll() |
|
| 112 | + { |
|
| 113 | + $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
| 114 | + do { |
|
| 115 | + curl_multi_exec(self::$multiHandler, $active); |
|
| 116 | + // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
|
| 117 | + // https://bugs.php.net/bug.php?id=63411 |
|
| 118 | + if (curl_multi_select(self::$multiHandler) == -1) { |
|
| 119 | + usleep($sleepTime); |
|
| 120 | + } |
|
| 121 | + usleep($sleepTime); |
|
| 122 | + } while ($active); |
|
| 123 | + $return = array(); |
|
| 124 | + foreach (self::$requestPool as $request) { |
|
| 125 | + $return[] = $request->send(true); |
|
| 126 | + curl_multi_remove_handle(self::$multiHandler, $request->curlHandle); |
|
| 127 | + curl_close($request->curlHandle); |
|
| 128 | + } |
|
| 129 | + curl_multi_close(self::$multiHandler); |
|
| 130 | + self::$requestPool = null; |
|
| 131 | + return $return; |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @param array $options |
| 47 | 47 | * @return $this |
| 48 | 48 | */ |
| 49 | - public function setDefaults(array $options = array()){ |
|
| 49 | + public function setDefaults(array $options = array()) { |
|
| 50 | 50 | $this->defaultOptions = $options; |
| 51 | 51 | return $this; |
| 52 | 52 | } |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | 'method' => $method, |
| 69 | 69 | 'url' => $uri, |
| 70 | 70 | 'data' => $payload, |
| 71 | - ) + $options; |
|
| 71 | + )+$options; |
|
| 72 | 72 | $this->addOptions(array($options)); |
| 73 | 73 | return $this; |
| 74 | 74 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | public function addOptions(array $URLOptions) |
| 82 | 82 | { |
| 83 | 83 | foreach ($URLOptions as $options) { |
| 84 | - $options = $options + $this->defaultOptions; |
|
| 84 | + $options = $options+$this->defaultOptions; |
|
| 85 | 85 | $request = Request::create()->addOptions($options)->applyOptions(); |
| 86 | 86 | if (isset($options['callback'])) { |
| 87 | 87 | $request->onEnd($options['callback']); |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | */ |
| 111 | 111 | public function sendAll() |
| 112 | 112 | { |
| 113 | - $sleepTime = 1000;//microsecond, prevent CPU 100% |
|
| 113 | + $sleepTime = 1000; //microsecond, prevent CPU 100% |
|
| 114 | 114 | do { |
| 115 | 115 | curl_multi_exec(self::$multiHandler, $active); |
| 116 | 116 | // bug in PHP 5.3.18+ where curl_multi_select can return -1 |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | usleep($sleepTime); |
| 120 | 120 | } |
| 121 | 121 | usleep($sleepTime); |
| 122 | - } while ($active); |
|
| 122 | + }while ($active); |
|
| 123 | 123 | $return = array(); |
| 124 | 124 | foreach (self::$requestPool as $request) { |
| 125 | 125 | $return[] = $request->send(true); |