|
6 | 6 | use React\EventLoop\Factory as EventLoopFactory;
|
7 | 7 | use React\Dns\Resolver\Resolver as DnsResolver;
|
8 | 8 | use React\Dns\Resolver\Factory as DnsResolverFactory;
|
9 |
| -use React\HttpClient\Factory as HttpClientFactory; |
10 | 9 | use React\HttpClient\Client as HttpClient;
|
| 10 | +use React\HttpClient\Factory as HttpClientFactory; |
| 11 | +use React\Socket\Connector; |
| 12 | +use React\Socket\ConnectorInterface; |
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | * Factory wrapper for React instances.
|
@@ -43,24 +45,102 @@ public static function buildDnsResolver(
|
43 | 45 | return $factory->createCached($dns, $loop);
|
44 | 46 | }
|
45 | 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 | + |
46 | 63 | /**
|
47 | 64 | * Build a React Http Client.
|
48 | 65 | *
|
49 |
| - * @param LoopInterface $loop |
50 |
| - * @param DnsResolver $dns |
| 66 | + * @param LoopInterface $loop |
| 67 | + * @param ConnectorInterface|DnsResolver|null $connector Only pass this argument if you need to customize DNS |
| 68 | + * behaviour. With react http client v0.5, pass a connector, |
| 69 | + * with v0.4 this must be a DnsResolver. |
51 | 70 | *
|
52 | 71 | * @return HttpClient
|
53 | 72 | */
|
54 | 73 | public static function buildHttpClient(
|
55 | 74 | LoopInterface $loop,
|
56 |
| - DnsResolver $dns = null |
| 75 | + $connector = null |
57 | 76 | ) {
|
| 77 | + if (class_exists(HttpClientFactory::class)) { |
| 78 | + // if HttpClientFactory class exists, use old behavior for backwards compatibility |
| 79 | + return static::buildHttpClient04($loop, $connector); |
| 80 | + } else { |
| 81 | + return static::buildHttpClient05($loop, $connector); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Builds a React Http client v0.4 style. |
| 87 | + * |
| 88 | + * @param LoopInterface $loop |
| 89 | + * @param DnsResolver|null $dns |
| 90 | + * |
| 91 | + * @return HttpClient |
| 92 | + */ |
| 93 | + protected static function buildHttpClient04( |
| 94 | + LoopInterface $loop, |
| 95 | + $dns = null |
| 96 | + ) { |
| 97 | + // create dns resolver if one isn't provided |
58 | 98 | if (null === $dns) {
|
59 |
| - $dns = self::buildDnsResolver($loop); |
| 99 | + $dns = static::buildDnsResolver($loop); |
| 100 | + } |
| 101 | + |
| 102 | + // validate connector instance for proper error reporting |
| 103 | + if (!$dns instanceof DnsResolver) { |
| 104 | + throw new \InvalidArgumentException('For react http client v0.4, $dns must be an instance of DnsResolver'); |
60 | 105 | }
|
61 | 106 |
|
62 | 107 | $factory = new HttpClientFactory();
|
63 | 108 |
|
64 | 109 | return $factory->create($loop, $dns);
|
65 | 110 | }
|
| 111 | + |
| 112 | + /** |
| 113 | + * Builds a React Http client v0.5 style. |
| 114 | + * |
| 115 | + * @param LoopInterface $loop |
| 116 | + * @param DnsResolver|ConnectorInterface|null $connector |
| 117 | + * |
| 118 | + * @return HttpClient |
| 119 | + */ |
| 120 | + protected static function buildHttpClient05( |
| 121 | + LoopInterface $loop, |
| 122 | + $connector = null |
| 123 | + ) { |
| 124 | + // build a connector with given DnsResolver if provided (old deprecated behavior) |
| 125 | + if ($connector instanceof DnsResolver) { |
| 126 | + @trigger_error( |
| 127 | + sprintf( |
| 128 | + 'Passing a %s to buildHttpClient is deprecated since version 2.1.0 and will be removed in 3.0. If you need no specific behaviour, omit the $dns argument, otherwise pass a %s', |
| 129 | + DnsResolver::class, |
| 130 | + ConnectorInterface::class |
| 131 | + ), |
| 132 | + E_USER_DEPRECATED |
| 133 | + ); |
| 134 | + $connector = static::buildConnector($loop, $connector); |
| 135 | + } |
| 136 | + |
| 137 | + // validate connector instance for proper error reporting |
| 138 | + if (null !== $connector && !$connector instanceof ConnectorInterface) { |
| 139 | + throw new \InvalidArgumentException( |
| 140 | + '$connector must be an instance of DnsResolver or ConnectorInterface' |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + return new HttpClient($loop, $connector); |
| 145 | + } |
66 | 146 | }
|
0 commit comments