Skip to content

Commit 94bc741

Browse files
committed
Add factory to build EventLoop, Client, DNSResolver
This allow to simplify the process of react instance creation
1 parent 55e1193 commit 94bc741

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/ReactFactory.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Http\Adapter;
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\Factory as HttpClientFactory;
10+
use React\HttpClient\Client as HttpClient;
11+
12+
/**
13+
* Factory wrapper for React instances
14+
* @author Stéphane Hulard <[email protected]>
15+
*/
16+
class ReactFactory
17+
{
18+
/**
19+
* Build a react Event Loop
20+
* @return LoopInterface
21+
*/
22+
public static function buildEventLoop()
23+
{
24+
return EventLoopFactory::create();
25+
}
26+
27+
/**
28+
* Build a React Dns Resolver
29+
* @param LoopInterface $loop
30+
* @param string $dns
31+
* @return DnsResolver
32+
*/
33+
public static function buildDnsResolver(
34+
LoopInterface $loop,
35+
$dns = '8.8.8.8'
36+
) {
37+
$factory = new DnsResolverFactory();
38+
return $factory->createCached($dns, $loop);
39+
}
40+
41+
/**
42+
* Build a React Http Client
43+
* @param LoopInterface $loop
44+
* @param Resolver $dns
45+
* @return HttpClient
46+
*/
47+
public static function buildHttpClient(
48+
LoopInterface $loop,
49+
DnsResolver $dns = null
50+
) {
51+
if( null === $dns ) {
52+
$dns = self::buildDnsResolver($loop);
53+
}
54+
55+
$factory = new HttpClientFactory();
56+
return $factory->create($loop, $dns);
57+
}
58+
}

0 commit comments

Comments
 (0)