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