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,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