Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • AsyncPlugin
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
 1: <?php
 2: 
 3: namespace Guzzle\Plugin\Async;
 4: 
 5: use Guzzle\Common\Event;
 6: use Guzzle\Http\Message\Response;
 7: use Guzzle\Http\Exception\CurlException;
 8: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 9: 
10: /**
11:  * Sends requests but does not wait for the response
12:  */
13: class AsyncPlugin implements EventSubscriberInterface
14: {
15:     /**
16:      * {@inheritdoc}
17:      */
18:     public static function getSubscribedEvents()
19:     {
20:         return array(
21:             'request.before_send'    => 'onBeforeSend',
22:             'request.exception'      => 'onRequestTimeout',
23:             'request.sent'           => 'onRequestSent',
24:             'curl.callback.progress' => 'onCurlProgress'
25:         );
26:     }
27: 
28:     /**
29:      * Event used to ensure that progress callback are emitted from the curl handle's request mediator.
30:      *
31:      * @param Event $event
32:      */
33:     public function onBeforeSend(Event $event)
34:     {
35:         // Ensure that progress callbacks are dispatched
36:         $event['request']->getCurlOptions()->set('progress', true);
37:     }
38: 
39:     /**
40:      * Event emitted when a curl progress function is called. When the amount of data uploaded == the amount of data to
41:      * upload OR any bytes have been downloaded, then time the request out after 1ms because we're done with
42:      * transmitting the request, and tell curl not download a body.
43:      *
44:      * @param Event $event
45:      */
46:     public function onCurlProgress(Event $event)
47:     {
48:         if ($event['handle'] &&
49:             ($event['downloaded'] || ($event['uploaded'] && $event['upload_size'] === $event['uploaded']))
50:         ) {
51:             // Timeout after 1ms
52:             curl_setopt($event['handle']->getHandle(), CURLOPT_TIMEOUT_MS, 1);
53:             // Even if the response is quick, tell curl not to download the body
54:             curl_setopt($event['handle']->getHandle(), CURLOPT_NOBODY, true);
55:         }
56:     }
57: 
58:     /**
59:      * Event emitted when a curl exception occurs. Ignore the exception and set a mock response.
60:      *
61:      * @param Event $event
62:      */
63:     public function onRequestTimeout(Event $event)
64:     {
65:         if ($event['exception'] instanceof CurlException) {
66:             $event['request']->setResponse(new Response(200, array(
67:                 'X-Guzzle-Async' => 'Did not wait for the response'
68:             )));
69:         }
70:     }
71: 
72:     /**
73:      * Event emitted when a request completes because it took less than 1ms. Add an X-Guzzle-Async header to notify the
74:      * caller that there is no body in the message.
75:      *
76:      * @param Event $event
77:      */
78:     public function onRequestSent(Event $event)
79:     {
80:         // Let the caller know this was meant to be async
81:         $event['request']->getResponse()->setHeader('X-Guzzle-Async', 'Did not wait for the response');
82:     }
83: }
84: 
php-coveralls API documentation generated by ApiGen 2.8.0