Skip to content

Commit

Permalink
Curl multi handle execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 1, 2016
1 parent e38d79f commit 185e79c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/CurlMultiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace phpgt\fetch;

class CurlMultiHandleException extends \Exception {}#
8 changes: 6 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function __construct(string $uri, array $init = [], array $curlOpt = [],
$this->curlInit($curlOpt);
}

public function getCurlHandle() {
return $this->curl;
}

private function curlInit($options = []) {
$defaultOptions = [];

Expand All @@ -135,8 +139,8 @@ private function curlInit($options = []) {
if(isset($this->body)) {
$defaultOptions[CURLOPT_POSTFIELDS] = $this->body;
}
// TODO: Set up cookie jar for $this->credentials
// as described https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch
// TODO: Set up cookie jar for $this->credentials
// as described https://developer.mozilla.org/en-US/docs/Web/API/GlobalFetch/fetch

if($this->redirect === self::REDIRECT_FOLLOW) {
$defaultOptions[CURLOPT_FOLLOWLOCATION] = true;
Expand Down
34 changes: 30 additions & 4 deletions src/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ class RequestResolver {
/**
* @var PHPCurl\CurlWrapper\CurlMulti
*/
private $curlMultiHandle;
private $curlMulti;

private $requestArray = [];
private $deferredArray = [];
private $index;
private $runningStatus = null;

public function __construct(
string $curlMultiClass = "\PHPCurl\CurlWrapper\CurlMulti") {
$this->curlMultiHandle = new $curlMultiClass();
$this->curlMulti = new $curlMultiClass();
}

public function add(Request $request, Deferred $deferred) {
Expand All @@ -33,9 +34,34 @@ public function add(Request $request, Deferred $deferred) {
* as requests complete.
*/
public function tick() {
for($i = 0, $length = count($this->requestArray); $i < $length; $i++) {
$this->deferredArray[$i]->resolve(true);
if(is_null($this->runningStatus)) {
$this->start();
}

while(false !== ($message = $this->curlMulti->infoRead($messageCount))) {
var_dump($message);
}

// foreach($this->requestArray as $i => $request) {
// $deferred = $this->deferredArray[$i];
// }
}

private function start() {
// Add curl handles to the curlMulti stack.
foreach($this->requestArray as $i => $request) {
$successCode = $this->curlMulti->add($request->getCurlHandle());

if($successCode !== 0) {
throw new CurlMultiException($successCode);
}
}

$this->runningStatus = null;

// Execute all curl handles on the curlMulti stack.
while(CURLM_CALL_MULTI_PERFORM
=== $this->curlMulti->exec($this->runningStatus));
}

}#

0 comments on commit 185e79c

Please sign in to comment.