updated depency libaries
This commit is contained in:
@ -5,6 +5,18 @@ use Symfony\Component\EventDispatcher\Event as SymfonyEvent;
|
||||
|
||||
class Event extends SymfonyEvent
|
||||
{
|
||||
/** @var Response $response */
|
||||
public $response = null;
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
public $response;
|
||||
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* @var RequestsQueue
|
||||
*/
|
||||
public $queue;
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ class Request extends EventDispatcher implements RequestInterface
|
||||
public function getOptions()
|
||||
{
|
||||
if (!isset($this->options)) {
|
||||
$this->options = new Options;
|
||||
$this->options = new Options();
|
||||
}
|
||||
return $this->options;
|
||||
}
|
||||
@ -97,6 +97,8 @@ class Request extends EventDispatcher implements RequestInterface
|
||||
* This function should be called after initializing a cURL
|
||||
* session and all the options for the session are set.
|
||||
*
|
||||
* Warning: it doesn't fire 'complete' event.
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function send()
|
||||
@ -113,32 +115,31 @@ class Request extends EventDispatcher implements RequestInterface
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function prepareQueue()
|
||||
{
|
||||
if (!isset($this->queue)) {
|
||||
$request = $this;
|
||||
$this->queue = new RequestsQueue;
|
||||
$this->queue->addListener(
|
||||
'complete',
|
||||
function ($event) use ($request) {
|
||||
$request->dispatch('complete', $event);
|
||||
}
|
||||
);
|
||||
$this->queue->attach($this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates new RequestsQueue with single Request attached to it
|
||||
* and calls RequestsQueue::socketPerform() method.
|
||||
*
|
||||
* @see RequestsQueue::socketPerform()
|
||||
*/
|
||||
public function socketPerform()
|
||||
{
|
||||
$this->prepareQueue();
|
||||
if (!isset($this->queue)) {
|
||||
$this->queue = new RequestsQueue();
|
||||
$this->queue->attach($this);
|
||||
}
|
||||
return $this->queue->socketPerform();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calls socketSelect() on previously created RequestsQueue
|
||||
*
|
||||
* @see RequestsQueue::socketSelect()
|
||||
*/
|
||||
public function socketSelect($timeout = 1)
|
||||
{
|
||||
if (!isset($this->queue)) {
|
||||
throw new Exception('Cannot select without perform before.');
|
||||
throw new Exception('You need to call socketPerform() before.');
|
||||
}
|
||||
return $this->queue->socketSelect($timeout);
|
||||
}
|
||||
|
@ -2,9 +2,8 @@
|
||||
namespace cURL;
|
||||
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Countable;
|
||||
|
||||
class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, Countable
|
||||
class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \Countable
|
||||
{
|
||||
/**
|
||||
* @var Options Default options for new Requests attached to RequestsQueue
|
||||
@ -59,7 +58,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, C
|
||||
public function getDefaultOptions()
|
||||
{
|
||||
if (!isset($this->defaultOptions)) {
|
||||
$this->defaultOptions = new Options;
|
||||
$this->defaultOptions = new Options();
|
||||
}
|
||||
return $this->defaultOptions;
|
||||
}
|
||||
@ -126,7 +125,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, C
|
||||
unset($this->running[$request->getUID()]);
|
||||
$this->detach($request);
|
||||
|
||||
$event = new Event;
|
||||
$event = new Event();
|
||||
$event->request = $request;
|
||||
$event->response = new Response($request, curl_multi_getcontent($request->getHandle()));
|
||||
if ($result !== CURLE_OK) {
|
||||
@ -134,6 +133,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, C
|
||||
}
|
||||
$event->queue = $this;
|
||||
$this->dispatch('complete', $event);
|
||||
$request->dispatch('complete', $event);
|
||||
}
|
||||
|
||||
return $n;
|
||||
@ -168,9 +168,11 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, C
|
||||
*/
|
||||
protected function getRequestsNotRunning()
|
||||
{
|
||||
return array_diff_key($this->queue, $this->running);
|
||||
$map = $this->queue;
|
||||
foreach($this->running as $k => $v) unset($map[$k]);
|
||||
return $map;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Download available data on socket.
|
||||
*
|
||||
@ -200,12 +202,15 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, C
|
||||
$mrc = curl_multi_exec($this->mh, $this->runningCount);
|
||||
} while ($mrc === CURLM_CALL_MULTI_PERFORM);
|
||||
$runningAfter = $this->runningCount;
|
||||
|
||||
$completed = ($runningAfter < $runningBefore) ? $this->read() : 0;
|
||||
|
||||
if ($runningAfter < $runningBefore) {
|
||||
$this->read();
|
||||
}
|
||||
|
||||
$notRunning = $this->getRequestsNotRunning();
|
||||
} while (count($notRunning) > 0);
|
||||
|
||||
// Why the loop? New requests might be added at runtime on 'complete' event.
|
||||
// So we need to attach them to curl_multi handle immediately.
|
||||
|
||||
return $this->count() > 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user