Skip to content

Commit b15947f

Browse files
committed
Simplify usage by making Browser optional
1 parent 86af3ac commit b15947f

File tree

5 files changed

+55
-62
lines changed

5 files changed

+55
-62
lines changed

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,23 @@ The `Client` class is responsible for communication with the remote SOAP
9494
WebService server. It requires the WSDL file contents and an optional
9595
array of SOAP options:
9696

97-
It requires a [`Browser`](https://github.com/reactphp/http#browser) object
98-
bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
99-
in order to handle async requests, the WSDL file contents and an optional
100-
array of SOAP options:
101-
10297
```php
103-
$browser = new React\Http\Browser();
104-
10598
$wsdl = '<?xml ';
10699
$options = array();
107100
108-
$client = new Clue\React\Soap\Client($browser, $wsdl, $options);
101+
$client = new Clue\React\Soap\Client(null, $wsdl, $options);
109102
```
110103
104+
This class takes an optional `Browser|null $browser` parameter that can be used to
105+
pass the browser instance to use for this object.
111106
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
112107
proxy servers etc.), you can explicitly pass a custom instance of the
113108
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
114-
to the [`Browser`](https://github.com/reactphp/http#browser) instance:
109+
to the [`Browser`](https://github.com/reactphp/http#browser) instance
110+
and pass it as an additional argument to the `Client` like this:
115111
116112
```php
117-
$connector = new React\Socket\Connector(null, array(
113+
$connector = new React\Socket\Connector(array(
118114
'dns' => '127.0.0.1',
119115
'tcp' => array(
120116
'bindto' => '192.168.10.1:0'
@@ -125,7 +121,7 @@ $connector = new React\Socket\Connector(null, array(
125121
)
126122
));
127123
128-
$browser = new React\Http\Browser(null, $connector);
124+
$browser = new React\Http\Browser($connector);
129125
$client = new Clue\React\Soap\Client($browser, $wsdl);
130126
```
131127
@@ -156,7 +152,7 @@ parsed, this will throw a `SoapFault`:
156152

157153
```php
158154
try {
159-
$client = new Clue\React\Soap\Client($browser, $wsdl);
155+
$client = new Clue\React\Soap\Client(null, $wsdl);
160156
} catch (SoapFault $e) {
161157
echo 'Error: ' . $e->getMessage() . PHP_EOL;
162158
}
@@ -180,7 +176,7 @@ the URL of the SOAP server to send the request to, and `uri` is the target
180176
namespace of the SOAP service:
181177

182178
```php
183-
$client = new Clue\React\Soap\Client($browser, null, array(
179+
$client = new Clue\React\Soap\Client(null, null, array(
184180
'location' => 'http://example.com',
185181
'uri' => 'http://ping.example.com',
186182
));
@@ -190,7 +186,7 @@ Similarly, if working in WSDL mode, the `location` option can be used to
190186
explicitly overwrite the URL of the SOAP server to send the request to:
191187

192188
```php
193-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
189+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
194190
'location' => 'http://example.com'
195191
));
196192
```
@@ -199,7 +195,7 @@ You can use the `soap_version` option to change from the default SOAP 1.1 to
199195
use SOAP 1.2 instead:
200196

201197
```php
202-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
198+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
203199
'soap_version' => SOAP_1_2
204200
));
205201
```
@@ -208,7 +204,7 @@ You can use the `classmap` option to map certain WSDL types to PHP classes
208204
like this:
209205

210206
```php
211-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
207+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
212208
'classmap' => array(
213209
'getBankResponseType' => BankResponse::class
214210
)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"require": {
2020
"php": ">=7.1",
21-
"react/http": "^1.0",
21+
"react/http": "^1.5",
2222
"react/promise": "^2.1 || ^1.2",
2323
"ext-soap": "*"
2424
},

examples/02-client-blz-non-wsdl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
$browser = new React\Http\Browser();
6-
75
$blz = isset($argv[1]) ? $argv[1] : '12070000';
86

9-
$client = new Clue\React\Soap\Client($browser, null, array(
7+
$client = new Clue\React\Soap\Client(null, null, array(
108
'location' => 'http://www.thomas-bayer.com/axis2/services/BLZService',
119
'uri' => 'http://thomas-bayer.com/blz/',
1210
'use' => SOAP_LITERAL

src/Client.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,26 @@
1111

1212
/**
1313
* The `Client` class is responsible for communication with the remote SOAP
14-
* WebService server.
15-
*
16-
* It requires a [`Browser`](https://github.com/reactphp/http#browser) object
17-
* bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
18-
* in order to handle async requests, the WSDL file contents and an optional
14+
* WebService server. It requires the WSDL file contents and an optional
1915
* array of SOAP options:
2016
*
2117
* ```php
22-
* $browser = new React\Http\Browser();
23-
*
2418
* $wsdl = '<?xml …';
2519
* $options = array();
2620
*
27-
* $client = new Clue\React\Soap\Client($browser, $wsdl, $options);
21+
* $client = new Clue\React\Soap\Client(null, $wsdl, $options);
2822
* ```
2923
*
24+
* This class takes an optional `Browser|null $browser` parameter that can be used to
25+
* pass the browser instance to use for this object.
3026
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
3127
* proxy servers etc.), you can explicitly pass a custom instance of the
3228
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
33-
* to the [`Browser`](https://github.com/clue/reactphp/http#browser) instance:
29+
* to the [`Browser`](https://github.com/reactphp/http#browser) instance
30+
* and pass it as an additional argument to the `Client` like this:
3431
*
3532
* ```php
36-
* $connector = new React\Socket\Connector(null, array(
33+
* $connector = new React\Socket\Connector(array(
3734
* 'dns' => '127.0.0.1',
3835
* 'tcp' => array(
3936
* 'bindto' => '192.168.10.1:0'
@@ -44,7 +41,7 @@
4441
* )
4542
* ));
4643
*
47-
* $browser = new React\Http\Browser(null, $connector);
44+
* $browser = new React\Http\Browser($connector);
4845
* $client = new Clue\React\Soap\Client($browser, $wsdl);
4946
* ```
5047
*
@@ -75,7 +72,7 @@
7572
*
7673
* ```php
7774
* try {
78-
* $client = new Clue\React\Soap\Client($browser, $wsdl);
75+
* $client = new Clue\React\Soap\Client(null, $wsdl);
7976
* } catch (SoapFault $e) {
8077
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
8178
* }
@@ -99,7 +96,7 @@
9996
* namespace of the SOAP service:
10097
*
10198
* ```php
102-
* $client = new Clue\React\Soap\Client($browser, null, array(
99+
* $client = new Clue\React\Soap\Client(null, null, array(
103100
* 'location' => 'http://example.com',
104101
* 'uri' => 'http://ping.example.com',
105102
* ));
@@ -109,7 +106,7 @@
109106
* explicitly overwrite the URL of the SOAP server to send the request to:
110107
*
111108
* ```php
112-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
109+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
113110
* 'location' => 'http://example.com'
114111
* ));
115112
* ```
@@ -118,7 +115,7 @@
118115
* use SOAP 1.2 instead:
119116
*
120117
* ```php
121-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
118+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
122119
* 'soap_version' => SOAP_1_2
123120
* ));
124121
* ```
@@ -127,7 +124,7 @@
127124
* like this:
128125
*
129126
* ```php
130-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
127+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
131128
* 'classmap' => array(
132129
* 'getBankResponseType' => BankResponse::class
133130
* )
@@ -145,29 +142,32 @@
145142
*/
146143
class Client
147144
{
145+
/** @var Browser */
148146
private $browser;
147+
149148
private $encoder;
150149
private $decoder;
151150

152151
/**
153152
* Instantiate a new SOAP client for the given WSDL contents.
154153
*
155-
* @param Browser $browser
156-
* @param string|null $wsdlContents
157-
* @param array $options
154+
* @param ?Browser $browser
155+
* @param ?string $wsdlContents
156+
* @param ?array $options
158157
*/
159-
public function __construct(Browser $browser, ?string $wsdlContents, array $options = array())
158+
public function __construct(?Browser $browser, ?string $wsdlContents, array $options = array())
160159
{
161160
$wsdl = $wsdlContents !== null ? 'data://text/plain;base64,' . base64_encode($wsdlContents) : null;
162161

162+
$this->browser = $browser ?? new Browser();
163+
163164
// Accept HTTP responses with error status codes as valid responses.
164165
// This is done in order to process these error responses through the normal SOAP decoder.
165166
// Additionally, we explicitly limit number of redirects to zero because following redirects makes little sense
166167
// because it transforms the POST request to a GET one and hence loses the SOAP request body.
167-
$browser = $browser->withRejectErrorResponse(false);
168-
$browser = $browser->withFollowRedirects(0);
168+
$this->browser = $this->browser->withRejectErrorResponse(false);
169+
$this->browser = $this->browser->withFollowRedirects(0);
169170

170-
$this->browser = $browser;
171171
$this->encoder = new ClientEncoder($wsdl, $options);
172172
$this->decoder = new ClientDecoder($wsdl, $options);
173173
}

0 commit comments

Comments
 (0)