From b3d25c8b3caa0b81da9afc129c7c9068c930bdb5 Mon Sep 17 00:00:00 2001 From: Julian Esperat Date: Wed, 19 Aug 2015 10:54:37 +0200 Subject: [PATCH] Allow adding Multicurl handles again after start() --- src/Curl/MultiCurl.php | 45 +++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/src/Curl/MultiCurl.php b/src/Curl/MultiCurl.php index 806a88f..7c37608 100644 --- a/src/Curl/MultiCurl.php +++ b/src/Curl/MultiCurl.php @@ -8,6 +8,8 @@ class MultiCurl public $multiCurl; public $curls = array(); private $curlFileHandles = array(); + private $nextCurlId = 1; + private $isStarted = false; private $beforeSendFunction = null; private $successFunction = null; @@ -458,29 +460,23 @@ class MultiCurl public function start() { foreach ($this->curls as $ch) { - foreach ($this->options as $option => $value) { - $ch->setOpt($option, $value); - } - foreach ($this->headers as $key => $value) { - $ch->setHeader($key, $value); - } - $ch->setJsonDecoder($this->jsonDecoder); - $ch->call($ch->beforeSendFunction); + $this->initHandle($ch); } - $curl_handles = $this->curls; + $this->isStarted = true; + do { curl_multi_select($this->multiCurl); curl_multi_exec($this->multiCurl, $active); while (!($info_array = curl_multi_info_read($this->multiCurl)) === false) { if ($info_array['msg'] === CURLMSG_DONE) { - foreach ($curl_handles as $key => $ch) { + foreach ($this->curls as $key => $ch) { if ($ch->curl === $info_array['handle']) { $ch->curlErrorCode = $info_array['result']; $ch->exec($ch->curl); curl_multi_remove_handle($this->multiCurl, $ch->curl); - unset($curl_handles[$key]); + unset($this->curls[$key]); // Close open file handles and reset the curl instance. if (isset($this->curlFileHandles[$ch->id])) { @@ -493,6 +489,8 @@ class MultiCurl } } } while ($active > 0); + + $this->isStarted = false; } /** @@ -557,6 +555,29 @@ class MultiCurl $curl->error($this->errorFunction); $curl->complete($this->completeFunction); $this->curls[] = $curl; - $curl->id = count($this->curls); + $curl->id = $this->nextCurlId++; + + if ($this->isStarted) { + $this->initHandle($curl); + } + } + + /** + * Init Handle + * + * @access private + * @param $curl + */ + private function initHandle($curl) + { + foreach ($this->options as $option => $value) { + $curl->setOpt($option, $value); + } + foreach ($this->headers as $key => $value) { + $curl->setHeader($key, $value); + } + $curl->setJsonDecoder($this->jsonDecoder); + $curl->call($curl->beforeSendFunction); + } }