Skip to content

Commit

Permalink
Curl requests being made, introduction of Response class.
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Oct 4, 2016
1 parent 185e79c commit 30ed212
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/CurlHandleMissingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace phpgt\fetch;

class CurlHandleMissingException extends \Exception {}#
2 changes: 1 addition & 1 deletion src/CurlMultiException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
namespace phpgt\fetch;

class CurlMultiHandleException extends \Exception {}#
class CurlMultiException extends \Exception {}#
6 changes: 4 additions & 2 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ public function request($input, array $init = []) {
* completed.
*/
public function wait() {
$this->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();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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);
}

Expand Down
58 changes: 46 additions & 12 deletions src/RequestResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -34,34 +34,68 @@ 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());

if($successCode !== 0) {
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);
}

}#
4 changes: 4 additions & 0 deletions src/RequestResolverCurlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace phpgt\fetch;

class RequestResolverCurlException extends \Exception {}#
6 changes: 6 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace phpgt\fetch;

class Response {

}#

0 comments on commit 30ed212

Please sign in to comment.