1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Foundation\Transport; |
5
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Events\CommandLocatorEvent; |
7
|
|
|
use Clue\React\Buzz\Browser; |
8
|
|
|
use Clue\React\Buzz\Io\Sender; |
9
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
10
|
|
|
use GuzzleHttp\HandlerStack; |
11
|
|
|
use League\Container\ContainerInterface; |
12
|
|
|
use League\Event\EmitterInterface; |
13
|
|
|
use React\Dns\Resolver\Factory as ResolverFactory; |
14
|
|
|
use React\Dns\Resolver\Resolver; |
15
|
|
|
use React\EventLoop\Factory as LoopFactory; |
16
|
|
|
use React\EventLoop\LoopInterface; |
17
|
|
|
use React\HttpClient\Client as HttpClient; |
18
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
19
|
|
|
use WyriHaximus\React\GuzzlePsr7\HttpClientAdapter; |
20
|
|
|
|
21
|
|
|
class Factory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param ContainerInterface $container |
25
|
|
|
* @param LoopInterface|null $loop |
26
|
|
|
* @param array $options |
27
|
|
|
* @return Client |
28
|
|
|
*/ |
29
|
|
|
public static function create( |
30
|
|
|
ContainerInterface $container, |
31
|
|
|
LoopInterface $loop = null, |
32
|
|
|
array $options = [] |
33
|
|
|
): Client { |
34
|
|
|
$container->get(EmitterInterface::class)-> |
35
|
|
|
addListener(CommandLocatorEvent::NAME, function (CommandLocatorEvent $event) { |
36
|
|
|
$event->add( |
37
|
|
|
__DIR__ . DIRECTORY_SEPARATOR . 'CommandBus' . DIRECTORY_SEPARATOR, |
38
|
|
|
__NAMESPACE__ . '\CommandBus' |
39
|
|
|
); |
40
|
|
|
}) |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
if (!($loop instanceof LoopInterface)) { |
44
|
|
|
$loop = LoopFactory::create(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$container->share(LoopInterface::class, $loop); |
48
|
|
|
|
49
|
|
|
if (!isset($options[Options::DNS])) { |
50
|
|
|
$options[Options::DNS] = '8.8.8.8'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$resolver = (new ResolverFactory())->createCached($options[Options::DNS], $loop); |
54
|
|
|
$httpClient = (new HttpClientFactory())->create($loop, $resolver); |
55
|
|
|
|
56
|
|
|
return self::createFromReactHttpClient( |
57
|
|
|
$container, |
58
|
|
|
$httpClient, |
59
|
|
|
$resolver, |
60
|
|
|
$loop, |
61
|
|
|
$options |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ContainerInterface $container |
67
|
|
|
* @param HttpClient $httpClient |
68
|
|
|
* @param Resolver $resolver |
69
|
|
|
* @param LoopInterface|null $loop |
70
|
|
|
* @param array $options |
71
|
|
|
* @return Client |
72
|
|
|
*/ |
73
|
|
|
public static function createFromReactHttpClient( |
74
|
|
|
ContainerInterface $container, |
75
|
|
|
HttpClient $httpClient, |
|
|
|
|
76
|
|
|
Resolver $resolver, |
77
|
|
|
LoopInterface $loop = null, |
78
|
|
|
array $options = [] |
79
|
|
|
): Client { |
80
|
|
|
return self::createFromBuzz( |
81
|
|
|
$container, |
82
|
|
|
$loop, |
|
|
|
|
83
|
|
|
(new Browser($loop, Sender::createFromLoopDns($loop, $resolver)))->withOptions([ |
|
|
|
|
84
|
|
|
'streaming' => true, |
85
|
|
|
]), |
86
|
|
|
$options |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ContainerInterface $container |
92
|
|
|
* @param LoopInterface $loop |
93
|
|
|
* @param Browser $buzz |
94
|
|
|
* @param array $options |
95
|
|
|
* @return Client |
96
|
|
|
*/ |
97
|
|
|
public static function createFromBuzz( |
98
|
|
|
ContainerInterface $container, |
99
|
|
|
LoopInterface $loop, |
100
|
|
|
Browser $buzz, |
101
|
|
|
array $options = [] |
102
|
|
|
): Client { |
103
|
|
|
return new Client( |
104
|
|
|
$loop, |
105
|
|
|
$container, |
106
|
|
|
$buzz, |
107
|
|
|
$options |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.