5
5
use React \Dns \Config \Config as DnsConfig ;
6
6
use React \Dns \Resolver \Factory as DnsFactory ;
7
7
use React \Dns \Resolver \ResolverInterface ;
8
- use React \EventLoop \Loop ;
9
8
use React \EventLoop \LoopInterface ;
10
9
11
10
/**
16
15
* as plaintext TCP/IP, secure TLS or local Unix connection streams.
17
16
*
18
17
* 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
20
19
* also shares all of their features and implementation details.
21
20
* If you want to typehint in your higher-level protocol implementation, you SHOULD
22
21
* use the generic [`ConnectorInterface`](#connectorinterface) instead.
@@ -27,11 +26,48 @@ final class Connector implements ConnectorInterface
27
26
{
28
27
private $ connectors = array ();
29
28
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 )
31
57
{
32
- $ loop = $ loop ?: Loop::get ();
58
+ // swap arguments for legacy constructor signature
59
+ if (($ context instanceof LoopInterface || $ context === null ) && (\func_num_args () <= 1 || \is_array ($ loop ))) {
60
+ $ swap = $ loop === null ? array (): $ loop ;
61
+ $ loop = $ context ;
62
+ $ context = $ swap ;
63
+ }
64
+
65
+ if (!\is_array ($ context ) || ($ loop !== null && !$ loop instanceof LoopInterface)) {
66
+ throw new \InvalidArgumentException ('Expected "array $context" and "?LoopInterface $loop" arguments ' );
67
+ }
68
+
33
69
// apply default options if not explicitly given
34
- $ options += array (
70
+ $ context += array (
35
71
'tcp ' => true ,
36
72
'tls ' => true ,
37
73
'unix ' => true ,
@@ -41,25 +77,25 @@ public function __construct(LoopInterface $loop = null, array $options = array()
41
77
'happy_eyeballs ' => true ,
42
78
);
43
79
44
- if ($ options ['timeout ' ] === true ) {
45
- $ options ['timeout ' ] = (float )\ini_get ("default_socket_timeout " );
80
+ if ($ context ['timeout ' ] === true ) {
81
+ $ context ['timeout ' ] = (float )\ini_get ("default_socket_timeout " );
46
82
}
47
83
48
- if ($ options ['tcp ' ] instanceof ConnectorInterface) {
49
- $ tcp = $ options ['tcp ' ];
84
+ if ($ context ['tcp ' ] instanceof ConnectorInterface) {
85
+ $ tcp = $ context ['tcp ' ];
50
86
} else {
51
87
$ tcp = new TcpConnector (
52
88
$ loop ,
53
- \is_array ($ options ['tcp ' ]) ? $ options ['tcp ' ] : array ()
89
+ \is_array ($ context ['tcp ' ]) ? $ context ['tcp ' ] : array ()
54
90
);
55
91
}
56
92
57
- if ($ options ['dns ' ] !== false ) {
58
- if ($ options ['dns ' ] instanceof ResolverInterface) {
59
- $ resolver = $ options ['dns ' ];
93
+ if ($ context ['dns ' ] !== false ) {
94
+ if ($ context ['dns ' ] instanceof ResolverInterface) {
95
+ $ resolver = $ context ['dns ' ];
60
96
} else {
61
- if ($ options ['dns ' ] !== true ) {
62
- $ config = $ options ['dns ' ];
97
+ if ($ context ['dns ' ] !== true ) {
98
+ $ config = $ context ['dns ' ];
63
99
} else {
64
100
// try to load nameservers from system config or default to Google's public DNS
65
101
$ config = DnsConfig::loadSystemConfigBlocking ();
@@ -75,52 +111,52 @@ public function __construct(LoopInterface $loop = null, array $options = array()
75
111
);
76
112
}
77
113
78
- if ($ options ['happy_eyeballs ' ] === true ) {
114
+ if ($ context ['happy_eyeballs ' ] === true ) {
79
115
$ tcp = new HappyEyeBallsConnector ($ loop , $ tcp , $ resolver );
80
116
} else {
81
117
$ tcp = new DnsConnector ($ tcp , $ resolver );
82
118
}
83
119
}
84
120
85
- if ($ options ['tcp ' ] !== false ) {
86
- $ options ['tcp ' ] = $ tcp ;
121
+ if ($ context ['tcp ' ] !== false ) {
122
+ $ context ['tcp ' ] = $ tcp ;
87
123
88
- if ($ options ['timeout ' ] !== false ) {
89
- $ options ['tcp ' ] = new TimeoutConnector (
90
- $ options ['tcp ' ],
91
- $ options ['timeout ' ],
124
+ if ($ context ['timeout ' ] !== false ) {
125
+ $ context ['tcp ' ] = new TimeoutConnector (
126
+ $ context ['tcp ' ],
127
+ $ context ['timeout ' ],
92
128
$ loop
93
129
);
94
130
}
95
131
96
- $ this ->connectors ['tcp ' ] = $ options ['tcp ' ];
132
+ $ this ->connectors ['tcp ' ] = $ context ['tcp ' ];
97
133
}
98
134
99
- if ($ options ['tls ' ] !== false ) {
100
- if (!$ options ['tls ' ] instanceof ConnectorInterface) {
101
- $ options ['tls ' ] = new SecureConnector (
135
+ if ($ context ['tls ' ] !== false ) {
136
+ if (!$ context ['tls ' ] instanceof ConnectorInterface) {
137
+ $ context ['tls ' ] = new SecureConnector (
102
138
$ tcp ,
103
139
$ loop ,
104
- \is_array ($ options ['tls ' ]) ? $ options ['tls ' ] : array ()
140
+ \is_array ($ context ['tls ' ]) ? $ context ['tls ' ] : array ()
105
141
);
106
142
}
107
143
108
- if ($ options ['timeout ' ] !== false ) {
109
- $ options ['tls ' ] = new TimeoutConnector (
110
- $ options ['tls ' ],
111
- $ options ['timeout ' ],
144
+ if ($ context ['timeout ' ] !== false ) {
145
+ $ context ['tls ' ] = new TimeoutConnector (
146
+ $ context ['tls ' ],
147
+ $ context ['timeout ' ],
112
148
$ loop
113
149
);
114
150
}
115
151
116
- $ this ->connectors ['tls ' ] = $ options ['tls ' ];
152
+ $ this ->connectors ['tls ' ] = $ context ['tls ' ];
117
153
}
118
154
119
- if ($ options ['unix ' ] !== false ) {
120
- if (!$ options ['unix ' ] instanceof ConnectorInterface) {
121
- $ options ['unix ' ] = new UnixConnector ($ loop );
155
+ if ($ context ['unix ' ] !== false ) {
156
+ if (!$ context ['unix ' ] instanceof ConnectorInterface) {
157
+ $ context ['unix ' ] = new UnixConnector ($ loop );
122
158
}
123
- $ this ->connectors ['unix ' ] = $ options ['unix ' ];
159
+ $ this ->connectors ['unix ' ] = $ context ['unix ' ];
124
160
}
125
161
}
126
162
@@ -140,4 +176,3 @@ public function connect($uri)
140
176
return $ this ->connectors [$ scheme ]->connect ($ uri );
141
177
}
142
178
}
143
-
0 commit comments