1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\React; |
4
|
|
|
|
5
|
|
|
use React\EventLoop\LoopInterface; |
6
|
|
|
use React\EventLoop\Factory as EventLoopFactory; |
7
|
|
|
use React\Dns\Resolver\Resolver as DnsResolver; |
8
|
|
|
use React\Dns\Resolver\Factory as DnsResolverFactory; |
9
|
|
|
use React\HttpClient\Client as HttpClient; |
10
|
|
|
use React\HttpClient\Factory as HttpClientFactory; |
11
|
|
|
use React\Socket\Connector; |
12
|
|
|
use React\Socket\ConnectorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Factory wrapper for React instances. |
16
|
|
|
* |
17
|
|
|
* @author Stéphane Hulard <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ReactFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Build a react Event Loop. |
23
|
|
|
* |
24
|
|
|
* @return LoopInterface |
25
|
|
|
*/ |
26
|
109 |
|
public static function buildEventLoop() |
27
|
|
|
{ |
28
|
109 |
|
return EventLoopFactory::create(); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Build a React Dns Resolver. |
33
|
|
|
* |
34
|
|
|
* @param LoopInterface $loop |
35
|
|
|
* @param string $dns |
36
|
|
|
* |
37
|
|
|
* @return DnsResolver |
38
|
|
|
*/ |
39
|
109 |
|
public static function buildDnsResolver( |
40
|
|
|
LoopInterface $loop, |
41
|
|
|
$dns = '8.8.8.8' |
42
|
|
|
) { |
43
|
109 |
|
$factory = new DnsResolverFactory(); |
44
|
|
|
|
45
|
109 |
|
return $factory->createCached($dns, $loop); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param LoopInterface $loop |
50
|
|
|
* @param DnsResolver|null $dns |
51
|
|
|
* |
52
|
|
|
* @return ConnectorInterface |
53
|
|
|
*/ |
54
|
|
|
public static function buildConnector( |
55
|
|
|
LoopInterface $loop, |
56
|
|
|
DnsResolver $dns = null |
57
|
|
|
) { |
58
|
|
|
return null !== $dns |
59
|
|
|
? new Connector($loop, ['dns' => $dns]) |
60
|
|
|
: new Connector($loop); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build a React Http Client. |
65
|
|
|
* |
66
|
|
|
* @param LoopInterface $loop |
67
|
|
|
* @param ConnectorInterface|DnsResolver|null $connector Passing a DnsResolver instance is deprecated. Should pass a |
68
|
|
|
* ConnectorInterface instance. |
69
|
|
|
* |
70
|
|
|
* @return HttpClient |
71
|
|
|
*/ |
72
|
111 |
|
public static function buildHttpClient( |
73
|
|
|
LoopInterface $loop, |
74
|
|
|
$connector = null |
75
|
|
|
) { |
76
|
111 |
|
if (class_exists(HttpClientFactory::class)) { |
77
|
|
|
// if HttpClientFactory class exists, use old behavior for backwards compatibility |
78
|
111 |
|
return static::buildHttpClient04($loop, $connector); |
|
|
|
|
79
|
|
|
} else { |
80
|
|
|
return static::buildHttpClient05($loop, $connector); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Builds a React Http client v0.4 style. |
86
|
|
|
* |
87
|
|
|
* @param LoopInterface $loop |
88
|
|
|
* @param DnsResolver|null $dns |
89
|
|
|
* |
90
|
|
|
* @return HttpClient |
91
|
|
|
*/ |
92
|
111 |
View Code Duplication |
private static function buildHttpClient04( |
|
|
|
|
93
|
|
|
LoopInterface $loop, |
94
|
|
|
$dns = null |
95
|
|
|
) { |
96
|
|
|
// create dns resolver if one isn't provided |
97
|
111 |
|
if (null === $dns) { |
98
|
109 |
|
$dns = static::buildDnsResolver($loop); |
99
|
109 |
|
} |
100
|
|
|
|
101
|
|
|
// validate connector instance for proper error reporting |
102
|
111 |
|
if (!$dns instanceof DnsResolver) { |
103
|
1 |
|
throw new \InvalidArgumentException('$connector must be an instance of DnsResolver'); |
104
|
|
|
} |
105
|
|
|
|
106
|
110 |
|
$factory = new HttpClientFactory(); |
107
|
|
|
|
108
|
110 |
|
return $factory->create($loop, $dns); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Builds a React Http client v0.5 style. |
113
|
|
|
* |
114
|
|
|
* @param LoopInterface $loop |
115
|
|
|
* @param DnsResolver|ConnectorInterface|null $connector |
116
|
|
|
* |
117
|
|
|
* @return HttpClient |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
private static function buildHttpClient05( |
|
|
|
|
120
|
|
|
LoopInterface $loop, |
121
|
|
|
$connector = null |
122
|
|
|
) { |
123
|
|
|
// build a connector with given DnsResolver if provided (old deprecated behavior) |
124
|
|
|
if ($connector instanceof DnsResolver) { |
125
|
|
|
$connector = static::buildConnector($loop, $connector); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// validate connector instance for proper error reporting |
129
|
|
|
if (null !== $connector && !$connector instanceof ConnectorInterface) { |
130
|
|
|
throw new \InvalidArgumentException( |
131
|
|
|
'$connector must be an instance of DnsResolver or ConnectorInterface' |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return new HttpClient($loop, $connector); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: