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: 12:
13: class AsyncPlugin implements EventSubscriberInterface
14: {
15: 16: 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: 30: 31: 32:
33: public function onBeforeSend(Event $event)
34: {
35:
36: $event['request']->getCurlOptions()->set('progress', true);
37: }
38:
39: 40: 41: 42: 43: 44: 45:
46: public function onCurlProgress(Event $event)
47: {
48: if ($event['handle'] &&
49: ($event['downloaded'] || ($event['uploaded'] && $event['upload_size'] === $event['uploaded']))
50: ) {
51:
52: curl_setopt($event['handle']->getHandle(), CURLOPT_TIMEOUT_MS, 1);
53:
54: curl_setopt($event['handle']->getHandle(), CURLOPT_NOBODY, true);
55: }
56: }
57:
58: 59: 60: 61: 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: 74: 75: 76: 77:
78: public function onRequestSent(Event $event)
79: {
80:
81: $event['request']->getResponse()->setHeader('X-Guzzle-Async', 'Did not wait for the response');
82: }
83: }
84: