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