Skip to content

Commit e802f94

Browse files
authored
Merge pull request #56 from SimonFrings/updates
Simplify usage by updating to new default loop and making Browser optional
2 parents 602149d + c1064cb commit e802f94

7 files changed

Lines changed: 68 additions & 83 deletions

File tree

README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ Once [installed](#install), you can use the following code to query an example
6767
web service via SOAP:
6868

6969
```php
70-
$loop = React\EventLoop\Factory::create();
71-
$browser = new React\Http\Browser($loop);
70+
<?php
71+
72+
require __DIR__ . '/vendor/autoload.php';
73+
74+
$browser = new React\Http\Browser();
7275
$wsdl = 'http://example.com/demo.wsdl';
7376

7477
$browser->get($wsdl)->then(function (Psr\Http\Message\ResponseInterface $response) use ($browser) {
@@ -79,8 +82,6 @@ $browser->get($wsdl)->then(function (Psr\Http\Message\ResponseInterface $respons
7982
var_dump('Result', $result);
8083
});
8184
});
82-
83-
$loop->run();
8485
```
8586

8687
See also the [examples](examples).
@@ -90,30 +91,26 @@ See also the [examples](examples).
9091
### Client
9192

9293
The `Client` class is responsible for communication with the remote SOAP
93-
WebService server.
94-
95-
It requires a [`Browser`](https://github.com/reactphp/http#browser) object
96-
bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
97-
in order to handle async requests, the WSDL file contents and an optional
94+
WebService server. It requires the WSDL file contents and an optional
9895
array of SOAP options:
9996

10097
```php
101-
$loop = React\EventLoop\Factory::create();
102-
$browser = new React\Http\Browser($loop);
103-
10498
$wsdl = '<?xml ';
10599
$options = array();
106100
107-
$client = new Clue\React\Soap\Client($browser, $wsdl, $options);
101+
$client = new Clue\React\Soap\Client(null, $wsdl, $options);
108102
```
109103
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.
110106
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
111107
proxy servers etc.), you can explicitly pass a custom instance of the
112108
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
113-
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:
114111
115112
```php
116-
$connector = new React\Socket\Connector($loop, array(
113+
$connector = new React\Socket\Connector(array(
117114
'dns' => '127.0.0.1',
118115
'tcp' => array(
119116
'bindto' => '192.168.10.1:0'
@@ -124,7 +121,7 @@ $connector = new React\Socket\Connector($loop, array(
124121
)
125122
));
126123
127-
$browser = new React\Http\Browser($loop, $connector);
124+
$browser = new React\Http\Browser($connector);
128125
$client = new Clue\React\Soap\Client($browser, $wsdl);
129126
```
130127
@@ -134,7 +131,7 @@ you to use local WSDL files, WSDL files from a cache or the most common form,
134131
downloading the WSDL file contents from an URL through the `Browser`:
135132

136133
```php
137-
$browser = new React\Http\Browser($loop);
134+
$browser = new React\Http\Browser();
138135

139136
$browser->get($url)->then(
140137
function (Psr\Http\Message\ResponseInterface $response) use ($browser) {
@@ -155,7 +152,7 @@ parsed, this will throw a `SoapFault`:
155152

156153
```php
157154
try {
158-
$client = new Clue\React\Soap\Client($browser, $wsdl);
155+
$client = new Clue\React\Soap\Client(null, $wsdl);
159156
} catch (SoapFault $e) {
160157
echo 'Error: ' . $e->getMessage() . PHP_EOL;
161158
}
@@ -179,7 +176,7 @@ the URL of the SOAP server to send the request to, and `uri` is the target
179176
namespace of the SOAP service:
180177

181178
```php
182-
$client = new Clue\React\Soap\Client($browser, null, array(
179+
$client = new Clue\React\Soap\Client(null, null, array(
183180
'location' => 'http://example.com',
184181
'uri' => 'http://ping.example.com',
185182
));
@@ -189,7 +186,7 @@ Similarly, if working in WSDL mode, the `location` option can be used to
189186
explicitly overwrite the URL of the SOAP server to send the request to:
190187

191188
```php
192-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
189+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
193190
'location' => 'http://example.com'
194191
));
195192
```
@@ -198,7 +195,7 @@ You can use the `soap_version` option to change from the default SOAP 1.1 to
198195
use SOAP 1.2 instead:
199196

200197
```php
201-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
198+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
202199
'soap_version' => SOAP_1_2
203200
));
204201
```
@@ -207,7 +204,7 @@ You can use the `classmap` option to map certain WSDL types to PHP classes
207204
like this:
208205

209206
```php
210-
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
207+
$client = new Clue\React\Soap\Client(null, $wsdl, array(
211208
'classmap' => array(
212209
'getBankResponseType' => BankResponse::class
213210
)
@@ -366,7 +363,7 @@ clean up any underlying resources.
366363
```php
367364
$promise = $proxy->demo();
368365

369-
$loop->addTimer(2.0, function () use ($promise) {
366+
Loop::addTimer(2.0, function () use ($promise) {
370367
$promise->cancel();
371368
});
372369
```
@@ -389,7 +386,7 @@ pass the timeout to the [underlying `Browser`](https://github.com/reactphp/http#
389386
like this:
390387
391388
```php
392-
$browser = new React\Http\Browser($loop);
389+
$browser = new React\Http\Browser();
393390
$browser = $browser->withTimeout(10.0);
394391
395392
$client = new Clue\React\Soap\Client($browser, $wsdl);

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/01-client-blz-wsdl.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

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

5-
$loop = React\EventLoop\Factory::create();
6-
$browser = new React\Http\Browser($loop);
5+
$browser = new React\Http\Browser();
76

87
$blz = isset($argv[1]) ? $argv[1] : '12070000';
98

@@ -21,5 +20,3 @@ function (Exception $e) {
2120
}
2221
);
2322
});
24-
25-
$loop->run();

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

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

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

5-
$loop = React\EventLoop\Factory::create();
6-
$browser = new React\Http\Browser($loop);
7-
85
$blz = isset($argv[1]) ? $argv[1] : '12070000';
96

10-
$client = new Clue\React\Soap\Client($browser, null, array(
7+
$client = new Clue\React\Soap\Client(null, null, array(
118
'location' => 'http://www.thomas-bayer.com/axis2/services/BLZService',
129
'uri' => 'http://thomas-bayer.com/blz/',
1310
'use' => SOAP_LITERAL
@@ -23,5 +20,3 @@ function (Exception $e) {
2320
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
2421
}
2522
);
26-
27-
$loop->run();

examples/11-wsdl-info.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

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

5-
$loop = React\EventLoop\Factory::create();
6-
$browser = new React\Http\Browser($loop);
5+
$browser = new React\Http\Browser();
76

87
$wsdl = isset($argv[1]) ? $argv[1] : 'http://www.thomas-bayer.com/axis2/services/BLZService?wsdl';
98

@@ -21,5 +20,3 @@ function (Exception $e) {
2120
echo 'Error: ' . $e->getMessage() . PHP_EOL;
2221
}
2322
);
24-
25-
$loop->run();

src/Client.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +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-
* $loop = React\EventLoop\Factory::create();
23-
* $browser = new React\Http\Browser($loop);
24-
*
2518
* $wsdl = '<?xml …';
2619
* $options = array();
2720
*
28-
* $client = new Clue\React\Soap\Client($browser, $wsdl, $options);
21+
* $client = new Clue\React\Soap\Client(null, $wsdl, $options);
2922
* ```
3023
*
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.
3126
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
3227
* proxy servers etc.), you can explicitly pass a custom instance of the
3328
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
34-
* 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:
3531
*
3632
* ```php
37-
* $connector = new React\Socket\Connector($loop, array(
33+
* $connector = new React\Socket\Connector(array(
3834
* 'dns' => '127.0.0.1',
3935
* 'tcp' => array(
4036
* 'bindto' => '192.168.10.1:0'
@@ -45,7 +41,7 @@
4541
* )
4642
* ));
4743
*
48-
* $browser = new React\Http\Browser($loop, $connector);
44+
* $browser = new React\Http\Browser($connector);
4945
* $client = new Clue\React\Soap\Client($browser, $wsdl);
5046
* ```
5147
*
@@ -55,7 +51,7 @@
5551
* downloading the WSDL file contents from an URL through the `Browser`:
5652
*
5753
* ```php
58-
* $browser = new React\Http\Browser($loop);
54+
* $browser = new React\Http\Browser();
5955
*
6056
* $browser->get($url)->then(
6157
* function (Psr\Http\Message\ResponseInterface $response) use ($browser) {
@@ -76,7 +72,7 @@
7672
*
7773
* ```php
7874
* try {
79-
* $client = new Clue\React\Soap\Client($browser, $wsdl);
75+
* $client = new Clue\React\Soap\Client(null, $wsdl);
8076
* } catch (SoapFault $e) {
8177
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
8278
* }
@@ -100,7 +96,7 @@
10096
* namespace of the SOAP service:
10197
*
10298
* ```php
103-
* $client = new Clue\React\Soap\Client($browser, null, array(
99+
* $client = new Clue\React\Soap\Client(null, null, array(
104100
* 'location' => 'http://example.com',
105101
* 'uri' => 'http://ping.example.com',
106102
* ));
@@ -110,7 +106,7 @@
110106
* explicitly overwrite the URL of the SOAP server to send the request to:
111107
*
112108
* ```php
113-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
109+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
114110
* 'location' => 'http://example.com'
115111
* ));
116112
* ```
@@ -119,7 +115,7 @@
119115
* use SOAP 1.2 instead:
120116
*
121117
* ```php
122-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
118+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
123119
* 'soap_version' => SOAP_1_2
124120
* ));
125121
* ```
@@ -128,7 +124,7 @@
128124
* like this:
129125
*
130126
* ```php
131-
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
127+
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
132128
* 'classmap' => array(
133129
* 'getBankResponseType' => BankResponse::class
134130
* )
@@ -146,29 +142,32 @@
146142
*/
147143
class Client
148144
{
145+
/** @var Browser */
149146
private $browser;
147+
150148
private $encoder;
151149
private $decoder;
152150

153151
/**
154152
* Instantiate a new SOAP client for the given WSDL contents.
155153
*
156-
* @param Browser $browser
157-
* @param string|null $wsdlContents
158-
* @param array $options
154+
* @param ?Browser $browser
155+
* @param ?string $wsdlContents
156+
* @param ?array $options
159157
*/
160-
public function __construct(Browser $browser, ?string $wsdlContents, array $options = array())
158+
public function __construct(?Browser $browser, ?string $wsdlContents, array $options = array())
161159
{
162160
$wsdl = $wsdlContents !== null ? 'data://text/plain;base64,' . base64_encode($wsdlContents) : null;
163161

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

171-
$this->browser = $browser;
172171
$this->encoder = new ClientEncoder($wsdl, $options);
173172
$this->decoder = new ClientDecoder($wsdl, $options);
174173
}

0 commit comments

Comments
 (0)