From 30ed212f6634d25f9e3cecdcf4383bf0f0dd4067 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 4 Oct 2016 09:24:53 +0100 Subject: [PATCH] Curl requests being made, introduction of Response class. --- src/CurlHandleMissingException.php | 4 ++ src/CurlMultiException.php | 2 +- src/Http.php | 6 ++- src/Request.php | 14 +++++++ src/RequestResolver.php | 58 ++++++++++++++++++++++------ src/RequestResolverCurlException.php | 4 ++ src/Response.php | 6 +++ 7 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/CurlHandleMissingException.php create mode 100644 src/RequestResolverCurlException.php create mode 100644 src/Response.php diff --git a/src/CurlHandleMissingException.php b/src/CurlHandleMissingException.php new file mode 100644 index 0000000..8865553 --- /dev/null +++ b/src/CurlHandleMissingException.php @@ -0,0 +1,4 @@ +timer = $this->loop->addTimer(0.1, [$this->requestResolver, "tick"]); - $this->loop->run($this->timer); + $this->timer = $this->loop->addPeriodicTimer( + 0.1, [$this->requestResolver, "tick"] + ); + $this->loop->run(); } /** diff --git a/src/Request.php b/src/Request.php index 69b0f5a..4065df3 100644 --- a/src/Request.php +++ b/src/Request.php @@ -127,6 +127,15 @@ public function getCurlHandle() { return $this->curl; } +public function getResponseCode():int { + return (int)$this->curl->getInfo(CURLINFO_HTTP_CODE); +} + +public function getResponse() { + $response = new Response(); + die("NOT YET IMPLEMENTED"); +} + private function curlInit($options = []) { $defaultOptions = []; @@ -154,6 +163,11 @@ private function curlInit($options = []) { } $options = array_merge($defaultOptions, $options); + +// The returntransfer option MUST be set, otherwise the promise resolution +// callbacks will not be able to get the content of the HTTP requests. + $options[CURLOPT_RETURNTRANSFER] = true; + $this->curl->setOptArray($options); } diff --git a/src/RequestResolver.php b/src/RequestResolver.php index 21d91e2..4194a1f 100644 --- a/src/RequestResolver.php +++ b/src/RequestResolver.php @@ -16,7 +16,7 @@ class RequestResolver { private $requestArray = []; private $deferredArray = []; private $index; -private $runningStatus = null; +private $openConnectionCount = null; public function __construct( string $curlMultiClass = "\PHPCurl\CurlWrapper\CurlMulti") { @@ -34,21 +34,46 @@ public function add(Request $request, Deferred $deferred) { * as requests complete. */ public function tick() { - if(is_null($this->runningStatus)) { + if(is_null($this->openConnectionCount)) { $this->start(); } - while(false !== ($message = $this->curlMulti->infoRead($messageCount))) { - var_dump($message); + do { + $info = $this->curlMulti->infoRead($messagesInQueue); + + if($info === false) { + break; + } + + $request = $this->matchRequest($info["handle"]); + if($request->getResponseCode() === 200) { + $requestIndex = array_search($request, $this->requestArray); + $this->deferredArray[$requestIndex]->resolve( + $request->getResponse() + ); + } + + }while($messagesInQueue > 0); + + if($this->openConnectionCount === 0) { +// $this->stopLoopSomehow() no need for comment - function should explain. + die("ALL DONE!"); } - // foreach($this->requestArray as $i => $request) { - // $deferred = $this->deferredArray[$i]; - // } +// Wait for activity on any of the handles. + $this->curlMulti->select(); + +// Execute the multi handle for processing next tick. + $status = $this->curlMulti->exec($this->openConnectionCount); + if($status !== CURLM_OK) { + throw new CurlMultiException($status); + } } +/** + * Adds each request's curl handle to the multi stack. + */ private function start() { -// Add curl handles to the curlMulti stack. foreach($this->requestArray as $i => $request) { $successCode = $this->curlMulti->add($request->getCurlHandle()); @@ -56,12 +81,21 @@ private function start() { throw new CurlMultiException($successCode); } } +} - $this->runningStatus = null; +/** + * Matches and returns the Request object containing the provided curl handle. + * + * @return Request + */ +private function matchRequest($ch) { + foreach($this->requestArray as $request) { + if($request->getCurlHandle()->getHandle() === $ch) { + return $request; + } + } -// Execute all curl handles on the curlMulti stack. - while(CURLM_CALL_MULTI_PERFORM - === $this->curlMulti->exec($this->runningStatus)); + throw new CurlHandleMissingException($ch); } }# \ No newline at end of file diff --git a/src/RequestResolverCurlException.php b/src/RequestResolverCurlException.php new file mode 100644 index 0000000..19f0186 --- /dev/null +++ b/src/RequestResolverCurlException.php @@ -0,0 +1,4 @@ +