1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Guzzle; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use GuzzleHttp\Handler\CurlMultiHandler; |
8
|
|
|
use GuzzleHttp\HandlerStack; |
9
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
10
|
|
|
use function GuzzleHttp\Promise\queue; |
11
|
|
|
use Nopolabs\Yabot\Helpers\ConfigTrait; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use React\EventLoop\LoopInterface; |
14
|
|
|
use React\EventLoop\Timer\TimerInterface; |
15
|
|
|
|
16
|
|
|
class Guzzle |
17
|
|
|
{ |
18
|
|
|
use ConfigTrait; |
19
|
|
|
|
20
|
|
|
/** @var Client */ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** @var LoopInterface */ |
24
|
|
|
private $eventloop; |
25
|
|
|
|
26
|
|
|
/** @var CurlMultiHandler */ |
27
|
|
|
private $handler; |
28
|
|
|
|
29
|
|
|
/** @var TimerInterface */ |
30
|
|
|
public $timer; |
31
|
|
|
|
32
|
|
|
public function __construct(LoopInterface $eventLoop, array $config = []) |
33
|
|
|
{ |
34
|
|
|
$this->handler = new CurlMultiHandler(); |
35
|
|
|
$config['handler'] = HandlerStack::create($this->handler); |
36
|
|
|
$this->client = new Client($config); |
37
|
|
|
$this->eventloop = $eventLoop; |
38
|
|
|
$this->setConfig($config); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getAsync(string $uri, array $options = []) : PromiseInterface |
42
|
|
|
{ |
43
|
|
|
$request = $this->client->getAsync($uri, $options); |
44
|
|
|
|
45
|
|
|
$this->scheduleProcessing(); |
46
|
|
|
|
47
|
|
|
return $request; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function post(string $uri, array $options = []) : ResponseInterface |
51
|
|
|
{ |
52
|
|
|
return $this->client->post($uri, $options); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @see http://stephencoakley.com/2015/06/11/integrating-guzzle-6-asynchronous-requests-with-reactphp |
57
|
|
|
* |
58
|
|
|
* Jiggerery with Closure::bind to get access to CurlMultiHandler::handles private member. |
59
|
|
|
*/ |
60
|
|
|
private function scheduleProcessing() |
61
|
|
|
{ |
62
|
|
|
if ($this->timer === null) { |
63
|
|
|
$self =& $this; |
64
|
|
|
$this->timer = $this->eventloop->addPeriodicTimer(0, \Closure::bind(function () use (&$self) { |
65
|
|
|
|
66
|
|
|
$this->tick(); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// Stop the timer when there are no more requests |
69
|
|
|
if (empty($this->handles) && queue()->isEmpty()) { |
|
|
|
|
70
|
|
|
$self->timer->cancel(); |
71
|
|
|
$self->timer = null; |
72
|
|
|
} |
73
|
|
|
}, $this->handler, $this->handler)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.