55use React \Dns \Config \Config as DnsConfig ;
66use React \Dns \Resolver \Factory as DnsFactory ;
77use React \Dns \Resolver \ResolverInterface ;
8- use React \EventLoop \Loop ;
98use React \EventLoop \LoopInterface ;
109
1110/**
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,48 @@ 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+ // 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+
3369 // apply default options if not explicitly given
34- $ options += array (
70+ $ context += array (
3571 'tcp ' => true ,
3672 'tls ' => true ,
3773 'unix ' => true ,
@@ -41,25 +77,25 @@ public function __construct(LoopInterface $loop = null, array $options = array()
4177 'happy_eyeballs ' => true ,
4278 );
4379
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 " );
4682 }
4783
48- if ($ options ['tcp ' ] instanceof ConnectorInterface) {
49- $ tcp = $ options ['tcp ' ];
84+ if ($ context ['tcp ' ] instanceof ConnectorInterface) {
85+ $ tcp = $ context ['tcp ' ];
5086 } else {
5187 $ tcp = new TcpConnector (
5288 $ loop ,
53- \is_array ($ options ['tcp ' ]) ? $ options ['tcp ' ] : array ()
89+ \is_array ($ context ['tcp ' ]) ? $ context ['tcp ' ] : array ()
5490 );
5591 }
5692
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 ' ];
6096 } else {
61- if ($ options ['dns ' ] !== true ) {
62- $ config = $ options ['dns ' ];
97+ if ($ context ['dns ' ] !== true ) {
98+ $ config = $ context ['dns ' ];
6399 } else {
64100 // try to load nameservers from system config or default to Google's public DNS
65101 $ config = DnsConfig::loadSystemConfigBlocking ();
@@ -75,52 +111,52 @@ public function __construct(LoopInterface $loop = null, array $options = array()
75111 );
76112 }
77113
78- if ($ options ['happy_eyeballs ' ] === true ) {
114+ if ($ context ['happy_eyeballs ' ] === true ) {
79115 $ tcp = new HappyEyeBallsConnector ($ loop , $ tcp , $ resolver );
80116 } else {
81117 $ tcp = new DnsConnector ($ tcp , $ resolver );
82118 }
83119 }
84120
85- if ($ options ['tcp ' ] !== false ) {
86- $ options ['tcp ' ] = $ tcp ;
121+ if ($ context ['tcp ' ] !== false ) {
122+ $ context ['tcp ' ] = $ tcp ;
87123
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 ' ],
92128 $ loop
93129 );
94130 }
95131
96- $ this ->connectors ['tcp ' ] = $ options ['tcp ' ];
132+ $ this ->connectors ['tcp ' ] = $ context ['tcp ' ];
97133 }
98134
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 (
102138 $ tcp ,
103139 $ loop ,
104- \is_array ($ options ['tls ' ]) ? $ options ['tls ' ] : array ()
140+ \is_array ($ context ['tls ' ]) ? $ context ['tls ' ] : array ()
105141 );
106142 }
107143
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 ' ],
112148 $ loop
113149 );
114150 }
115151
116- $ this ->connectors ['tls ' ] = $ options ['tls ' ];
152+ $ this ->connectors ['tls ' ] = $ context ['tls ' ];
117153 }
118154
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 );
122158 }
123- $ this ->connectors ['unix ' ] = $ options ['unix ' ];
159+ $ this ->connectors ['unix ' ] = $ context ['unix ' ];
124160 }
125161 }
126162
@@ -140,4 +176,3 @@ public function connect($uri)
140176 return $ this ->connectors [$ scheme ]->connect ($ uri );
141177 }
142178}
143-
0 commit comments