Skip to content

Commit 869ba91

Browse files
committed
Update Connector signature to take $context as first argument
1 parent 92fb72d commit 869ba91

File tree

5 files changed

+253
-97
lines changed

5 files changed

+253
-97
lines changed

README.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -944,12 +944,6 @@ also shares all of their features and implementation details.
944944
If you want to typehint in your higher-level protocol implementation, you SHOULD
945945
use the generic [`ConnectorInterface`](#connectorinterface) instead.
946946

947-
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
948-
pass the event loop instance to use for this object. You can use a `null` value
949-
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
950-
This value SHOULD NOT be given unless you're sure you want to explicitly use a
951-
given event loop instance.
952-
953947
As of `v1.4.0`, the `Connector` class defaults to using the
954948
[happy eyeballs algorithm](https://en.wikipedia.org/wiki/Happy_Eyeballs) to
955949
automatically connect over IPv4 or IPv6 when a hostname is given.
@@ -960,7 +954,7 @@ If you want to revert to the old behavior of only doing an IPv4 lookup and
960954
only attempt a single IPv4 connection, you can set up the `Connector` like this:
961955

962956
```php
963-
$connector = new React\Socket\Connector(null, array(
957+
$connector = new React\Socket\Connector(array(
964958
'happy_eyeballs' => false
965959
));
966960
```
@@ -974,7 +968,7 @@ If you explicitly want to use a custom DNS server (such as a local DNS relay or
974968
a company wide DNS server), you can set up the `Connector` like this:
975969

976970
```php
977-
$connector = new React\Socket\Connector(null, array(
971+
$connector = new React\Socket\Connector(array(
978972
'dns' => '127.0.1.1'
979973
));
980974

@@ -988,7 +982,7 @@ If you do not want to use a DNS resolver at all and want to connect to IP
988982
addresses only, you can also set up your `Connector` like this:
989983

990984
```php
991-
$connector = new React\Socket\Connector(null, array(
985+
$connector = new React\Socket\Connector(array(
992986
'dns' => false
993987
));
994988

@@ -1005,7 +999,7 @@ can also set up your `Connector` like this:
1005999
$dnsResolverFactory = new React\Dns\Resolver\Factory();
10061000
$resolver = $dnsResolverFactory->createCached('127.0.1.1');
10071001

1008-
$connector = new React\Socket\Connector(null, array(
1002+
$connector = new React\Socket\Connector(array(
10091003
'dns' => $resolver
10101004
));
10111005

@@ -1020,7 +1014,7 @@ respects your `default_socket_timeout` ini setting (which defaults to 60s).
10201014
If you want a custom timeout value, you can simply pass this like this:
10211015

10221016
```php
1023-
$connector = new React\Socket\Connector(null, array(
1017+
$connector = new React\Socket\Connector(array(
10241018
'timeout' => 10.0
10251019
));
10261020
```
@@ -1029,7 +1023,7 @@ Similarly, if you do not want to apply a timeout at all and let the operating
10291023
system handle this, you can pass a boolean flag like this:
10301024

10311025
```php
1032-
$connector = new React\Socket\Connector(null, array(
1026+
$connector = new React\Socket\Connector(array(
10331027
'timeout' => false
10341028
));
10351029
```
@@ -1040,7 +1034,7 @@ pass boolean flags like this:
10401034

10411035
```php
10421036
// only allow secure TLS connections
1043-
$connector = new React\Socket\Connector(null, array(
1037+
$connector = new React\Socket\Connector(array(
10441038
'tcp' => false,
10451039
'tls' => true,
10461040
'unix' => false,
@@ -1059,7 +1053,7 @@ pass arrays of context options like this:
10591053

10601054
```php
10611055
// allow insecure TLS connections
1062-
$connector = new React\Socket\Connector(null, array(
1056+
$connector = new React\Socket\Connector(array(
10631057
'tcp' => array(
10641058
'bindto' => '192.168.0.1:0'
10651059
),
@@ -1080,7 +1074,7 @@ SSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version you
10801074
want to negotiate with the remote side:
10811075

10821076
```php
1083-
$connector = new React\Socket\Connector(null, array(
1077+
$connector = new React\Socket\Connector(array(
10841078
'tls' => array(
10851079
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
10861080
)
@@ -1106,7 +1100,7 @@ $tls = new React\Socket\SecureConnector($tcp);
11061100

11071101
$unix = new React\Socket\UnixConnector();
11081102

1109-
$connector = new React\Socket\Connector(null, array(
1103+
$connector = new React\Socket\Connector(array(
11101104
'tcp' => $tcp,
11111105
'tls' => $tls,
11121106
'unix' => $unix,
@@ -1133,6 +1127,24 @@ $connector->connect('google.com:80')->then(function (React\Socket\ConnectionInte
11331127
Internally, the `tcp://` and `tls://` connectors will always be wrapped by
11341128
`TimeoutConnector`, unless you disable timeouts like in the above example.
11351129

1130+
This class takes an optional `LoopInterface|null $loop` parameter that can be used to
1131+
pass the event loop instance to use for this object. You can use a `null` value
1132+
here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
1133+
This value SHOULD NOT be given unless you're sure you want to explicitly use a
1134+
given event loop instance.
1135+
1136+
> Changelog v1.9.0: The constructur signature has been updated to take the
1137+
> optional `$context` as the first parameter and the optional `$loop` as a second
1138+
> argument. The previous signature has been deprecated and should not be used anymore.
1139+
>
1140+
> ```php
1141+
> // constructor signature as of v1.9.0
1142+
> $connector = new React\Socket\Connector(array $context = [], ?LoopInterface $loop = null);
1143+
>
1144+
> // legacy constructor signature before v1.9.0
1145+
> $connector = new React\Socket\Connector(?LoopInterface $loop = null, array $context = []);
1146+
> ```
1147+
11361148
### Advanced client usage
11371149
11381150
#### TcpConnector

src/Connector.php

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use React\Dns\Config\Config as DnsConfig;
66
use React\Dns\Resolver\Factory as DnsFactory;
77
use React\Dns\Resolver\ResolverInterface;
8-
use React\EventLoop\Loop;
98
use React\EventLoop\LoopInterface;
109

1110
/**
@@ -16,7 +15,7 @@
1615
* as plaintext TCP/IP, secure TLS or local Unix connection streams.
1716
*
1817
* Under the hood, the `Connector` is implemented as a *higher-level facade*
19-
* or the lower-level connectors implemented in this package. This means it
18+
* for the lower-level connectors implemented in this package. This means it
2019
* also shares all of their features and implementation details.
2120
* If you want to typehint in your higher-level protocol implementation, you SHOULD
2221
* use the generic [`ConnectorInterface`](#connectorinterface) instead.
@@ -27,11 +26,47 @@ final class Connector implements ConnectorInterface
2726
{
2827
private $connectors = array();
2928

30-
public function __construct(LoopInterface $loop = null, array $options = array())
29+
/**
30+
* Instantiate new `Connector`
31+
*
32+
* ```php
33+
* $connector = new React\Socket\Connector();
34+
* ```
35+
*
36+
* This class takes two optional arguments for more advanced usage:
37+
*
38+
* ```php
39+
* // constructor signature as of v1.9.0
40+
* $connector = new React\Socket\Connector(array $context = [], ?LoopInterface $loop = null);
41+
*
42+
* // legacy constructor signature before v1.9.0
43+
* $connector = new React\Socket\Connector(?LoopInterface $loop = null, array $context = []);
44+
* ```
45+
*
46+
* This class takes an optional `LoopInterface|null $loop` parameter that can be used to
47+
* pass the event loop instance to use for this object. You can use a `null` value
48+
* here in order to use the [default loop](https://github.com/reactphp/event-loop#loop).
49+
* This value SHOULD NOT be given unless you're sure you want to explicitly use a
50+
* given event loop instance.
51+
*
52+
* @param array|LoopInterface|null $context
53+
* @param null|LoopInterface|array $loop
54+
* @throws \InvalidArgumentException for invalid arguments
55+
*/
56+
public function __construct($context = array(), $loop = null)
3157
{
32-
$loop = $loop ?: Loop::get();
58+
if (($context instanceof LoopInterface || $context === null) && (\func_num_args() <= 1 || \is_array($loop))) {
59+
$swap = $loop === null ? array(): $loop;
60+
$loop = $context;
61+
$context = $swap;
62+
}
63+
64+
if (($loop !== null && !$loop instanceof LoopInterface) || !\is_array($context)) {
65+
throw new \InvalidArgumentException('Expected "array $context" and "?LoopInterface $loop" arguments');
66+
}
67+
3368
// apply default options if not explicitly given
34-
$options += array(
69+
$context += array(
3570
'tcp' => true,
3671
'tls' => true,
3772
'unix' => true,
@@ -41,25 +76,25 @@ public function __construct(LoopInterface $loop = null, array $options = array()
4176
'happy_eyeballs' => true,
4277
);
4378

44-
if ($options['timeout'] === true) {
45-
$options['timeout'] = (float)\ini_get("default_socket_timeout");
79+
if ($context['timeout'] === true) {
80+
$context['timeout'] = (float)\ini_get("default_socket_timeout");
4681
}
4782

48-
if ($options['tcp'] instanceof ConnectorInterface) {
49-
$tcp = $options['tcp'];
83+
if ($context['tcp'] instanceof ConnectorInterface) {
84+
$tcp = $context['tcp'];
5085
} else {
5186
$tcp = new TcpConnector(
5287
$loop,
53-
\is_array($options['tcp']) ? $options['tcp'] : array()
88+
\is_array($context['tcp']) ? $context['tcp'] : array()
5489
);
5590
}
5691

57-
if ($options['dns'] !== false) {
58-
if ($options['dns'] instanceof ResolverInterface) {
59-
$resolver = $options['dns'];
92+
if ($context['dns'] !== false) {
93+
if ($context['dns'] instanceof ResolverInterface) {
94+
$resolver = $context['dns'];
6095
} else {
61-
if ($options['dns'] !== true) {
62-
$config = $options['dns'];
96+
if ($context['dns'] !== true) {
97+
$config = $context['dns'];
6398
} else {
6499
// try to load nameservers from system config or default to Google's public DNS
65100
$config = DnsConfig::loadSystemConfigBlocking();
@@ -75,52 +110,52 @@ public function __construct(LoopInterface $loop = null, array $options = array()
75110
);
76111
}
77112

78-
if ($options['happy_eyeballs'] === true) {
113+
if ($context['happy_eyeballs'] === true) {
79114
$tcp = new HappyEyeBallsConnector($loop, $tcp, $resolver);
80115
} else {
81116
$tcp = new DnsConnector($tcp, $resolver);
82117
}
83118
}
84119

85-
if ($options['tcp'] !== false) {
86-
$options['tcp'] = $tcp;
120+
if ($context['tcp'] !== false) {
121+
$context['tcp'] = $tcp;
87122

88-
if ($options['timeout'] !== false) {
89-
$options['tcp'] = new TimeoutConnector(
90-
$options['tcp'],
91-
$options['timeout'],
123+
if ($context['timeout'] !== false) {
124+
$context['tcp'] = new TimeoutConnector(
125+
$context['tcp'],
126+
$context['timeout'],
92127
$loop
93128
);
94129
}
95130

96-
$this->connectors['tcp'] = $options['tcp'];
131+
$this->connectors['tcp'] = $context['tcp'];
97132
}
98133

99-
if ($options['tls'] !== false) {
100-
if (!$options['tls'] instanceof ConnectorInterface) {
101-
$options['tls'] = new SecureConnector(
134+
if ($context['tls'] !== false) {
135+
if (!$context['tls'] instanceof ConnectorInterface) {
136+
$context['tls'] = new SecureConnector(
102137
$tcp,
103138
$loop,
104-
\is_array($options['tls']) ? $options['tls'] : array()
139+
\is_array($context['tls']) ? $context['tls'] : array()
105140
);
106141
}
107142

108-
if ($options['timeout'] !== false) {
109-
$options['tls'] = new TimeoutConnector(
110-
$options['tls'],
111-
$options['timeout'],
143+
if ($context['timeout'] !== false) {
144+
$context['tls'] = new TimeoutConnector(
145+
$context['tls'],
146+
$context['timeout'],
112147
$loop
113148
);
114149
}
115150

116-
$this->connectors['tls'] = $options['tls'];
151+
$this->connectors['tls'] = $context['tls'];
117152
}
118153

119-
if ($options['unix'] !== false) {
120-
if (!$options['unix'] instanceof ConnectorInterface) {
121-
$options['unix'] = new UnixConnector($loop);
154+
if ($context['unix'] !== false) {
155+
if (!$context['unix'] instanceof ConnectorInterface) {
156+
$context['unix'] = new UnixConnector($loop);
122157
}
123-
$this->connectors['unix'] = $options['unix'];
158+
$this->connectors['unix'] = $context['unix'];
124159
}
125160
}
126161

@@ -140,4 +175,3 @@ public function connect($uri)
140175
return $this->connectors[$scheme]->connect($uri);
141176
}
142177
}
143-

0 commit comments

Comments
 (0)