Open Monograph Press  3.3.0
AsyncPlugin.php
1 <?php
2 
4 
9 
14 {
15  public static function getSubscribedEvents()
16  {
17  return array(
18  'request.before_send' => 'onBeforeSend',
19  'request.exception' => 'onRequestTimeout',
20  'request.sent' => 'onRequestSent',
21  'curl.callback.progress' => 'onCurlProgress'
22  );
23  }
24 
30  public function onBeforeSend(Event $event)
31  {
32  // Ensure that progress callbacks are dispatched
33  $event['request']->getCurlOptions()->set('progress', true);
34  }
35 
43  public function onCurlProgress(Event $event)
44  {
45  if ($event['handle'] &&
46  ($event['downloaded'] || (isset($event['uploaded']) && $event['upload_size'] === $event['uploaded']))
47  ) {
48  // Timeout after 1ms
49  curl_setopt($event['handle'], CURLOPT_TIMEOUT_MS, 1);
50  // Even if the response is quick, tell curl not to download the body.
51  // - Note that we can only perform this shortcut if the request transmitted a body so as to ensure that the
52  // request method is not converted to a HEAD request before the request was sent via curl.
53  if ($event['uploaded']) {
54  curl_setopt($event['handle'], CURLOPT_NOBODY, true);
55  }
56  }
57  }
58 
64  public function onRequestTimeout(Event $event)
65  {
66  if ($event['exception'] instanceof CurlException) {
67  $event['request']->setResponse(new Response(200, array(
68  'X-Guzzle-Async' => 'Did not wait for the response'
69  )));
70  }
71  }
72 
79  public function onRequestSent(Event $event)
80  {
81  // Let the caller know this was meant to be async
82  $event['request']->getResponse()->setHeader('X-Guzzle-Async', 'Did not wait for the response');
83  }
84 }
Guzzle\Http\Exception\CurlException
Definition: CurlException.php:10
Guzzle\Plugin\Async\AsyncPlugin\onBeforeSend
onBeforeSend(Event $event)
Definition: AsyncPlugin.php:30
Symfony\Component\EventDispatcher\EventSubscriberInterface
Definition: lib/vendor/symfony/event-dispatcher/EventSubscriberInterface.php:25
Guzzle\Http\Message\Response
Definition: paymethod/paypal/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:17
Guzzle\Plugin\Async
Definition: AsyncPlugin.php:3
Guzzle\Plugin\Async\AsyncPlugin\onRequestTimeout
onRequestTimeout(Event $event)
Definition: AsyncPlugin.php:64
Guzzle\Plugin\Async\AsyncPlugin
Definition: AsyncPlugin.php:13
Guzzle\Common\Event
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Event.php:10
Guzzle\Plugin\Async\AsyncPlugin\getSubscribedEvents
static getSubscribedEvents()
Definition: AsyncPlugin.php:15
Guzzle\Plugin\Async\AsyncPlugin\onCurlProgress
onCurlProgress(Event $event)
Definition: AsyncPlugin.php:43
Guzzle\Plugin\Async\AsyncPlugin\onRequestSent
onRequestSent(Event $event)
Definition: AsyncPlugin.php:79