Skip to content

Commit

Permalink
Simplify usage by making Browser optional
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonFrings committed Aug 11, 2021
1 parent 86af3ac commit b15947f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 62 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,23 @@ The `Client` class is responsible for communication with the remote SOAP
WebService server. It requires the WSDL file contents and an optional
array of SOAP options:

It requires a [`Browser`](https://github.com/reactphp/http#browser) object
bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
in order to handle async requests, the WSDL file contents and an optional
array of SOAP options:

```php
$browser = new React\Http\Browser();

$wsdl = '<?xml ';
$options = array();
$client = new Clue\React\Soap\Client($browser, $wsdl, $options);
$client = new Clue\React\Soap\Client(null, $wsdl, $options);
```
This class takes an optional `Browser|null $browser` parameter that can be used to
pass the browser instance to use for this object.
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
to the [`Browser`](https://github.com/reactphp/http#browser) instance:
to the [`Browser`](https://github.com/reactphp/http#browser) instance
and pass it as an additional argument to the `Client` like this:
```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand All @@ -125,7 +121,7 @@ $connector = new React\Socket\Connector(null, array(
)
));
$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);
$client = new Clue\React\Soap\Client($browser, $wsdl);
```
Expand Down Expand Up @@ -156,7 +152,7 @@ parsed, this will throw a `SoapFault`:

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

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

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

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

```php
$client = new Clue\React\Soap\Client($browser, $wsdl, array(
$client = new Clue\React\Soap\Client(null, $wsdl, array(
'classmap' => array(
'getBankResponseType' => BankResponse::class
)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"require": {
"php": ">=7.1",
"react/http": "^1.0",
"react/http": "^1.5",
"react/promise": "^2.1 || ^1.2",
"ext-soap": "*"
},
Expand Down
4 changes: 1 addition & 3 deletions examples/02-client-blz-non-wsdl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

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

$browser = new React\Http\Browser();

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

$client = new Clue\React\Soap\Client($browser, null, array(
$client = new Clue\React\Soap\Client(null, null, array(
'location' => 'http://www.thomas-bayer.com/axis2/services/BLZService',
'uri' => 'http://thomas-bayer.com/blz/',
'use' => SOAP_LITERAL
Expand Down
46 changes: 23 additions & 23 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,26 @@

/**
* The `Client` class is responsible for communication with the remote SOAP
* WebService server.
*
* It requires a [`Browser`](https://github.com/reactphp/http#browser) object
* bound to the main [`EventLoop`](https://github.com/reactphp/event-loop#usage)
* in order to handle async requests, the WSDL file contents and an optional
* WebService server. It requires the WSDL file contents and an optional
* array of SOAP options:
*
* ```php
* $browser = new React\Http\Browser();
*
* $wsdl = '<?xml …';
* $options = array();
*
* $client = new Clue\React\Soap\Client($browser, $wsdl, $options);
* $client = new Clue\React\Soap\Client(null, $wsdl, $options);
* ```
*
* This class takes an optional `Browser|null $browser` parameter that can be used to
* pass the browser instance to use for this object.
* If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
* proxy servers etc.), you can explicitly pass a custom instance of the
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface)
* to the [`Browser`](https://github.com/clue/reactphp/http#browser) instance:
* to the [`Browser`](https://github.com/reactphp/http#browser) instance
* and pass it as an additional argument to the `Client` like this:
*
* ```php
* $connector = new React\Socket\Connector(null, array(
* $connector = new React\Socket\Connector(array(
* 'dns' => '127.0.0.1',
* 'tcp' => array(
* 'bindto' => '192.168.10.1:0'
Expand All @@ -44,7 +41,7 @@
* )
* ));
*
* $browser = new React\Http\Browser(null, $connector);
* $browser = new React\Http\Browser($connector);
* $client = new Clue\React\Soap\Client($browser, $wsdl);
* ```
*
Expand Down Expand Up @@ -75,7 +72,7 @@
*
* ```php
* try {
* $client = new Clue\React\Soap\Client($browser, $wsdl);
* $client = new Clue\React\Soap\Client(null, $wsdl);
* } catch (SoapFault $e) {
* echo 'Error: ' . $e->getMessage() . PHP_EOL;
* }
Expand All @@ -99,7 +96,7 @@
* namespace of the SOAP service:
*
* ```php
* $client = new Clue\React\Soap\Client($browser, null, array(
* $client = new Clue\React\Soap\Client(null, null, array(
* 'location' => 'http://example.com',
* 'uri' => 'http://ping.example.com',
* ));
Expand All @@ -109,7 +106,7 @@
* explicitly overwrite the URL of the SOAP server to send the request to:
*
* ```php
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
* 'location' => 'http://example.com'
* ));
* ```
Expand All @@ -118,7 +115,7 @@
* use SOAP 1.2 instead:
*
* ```php
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
* 'soap_version' => SOAP_1_2
* ));
* ```
Expand All @@ -127,7 +124,7 @@
* like this:
*
* ```php
* $client = new Clue\React\Soap\Client($browser, $wsdl, array(
* $client = new Clue\React\Soap\Client(null, $wsdl, array(
* 'classmap' => array(
* 'getBankResponseType' => BankResponse::class
* )
Expand All @@ -145,29 +142,32 @@
*/
class Client
{
/** @var Browser */
private $browser;

private $encoder;
private $decoder;

/**
* Instantiate a new SOAP client for the given WSDL contents.
*
* @param Browser $browser
* @param string|null $wsdlContents
* @param array $options
* @param ?Browser $browser
* @param ?string $wsdlContents
* @param ?array $options
*/
public function __construct(Browser $browser, ?string $wsdlContents, array $options = array())
public function __construct(?Browser $browser, ?string $wsdlContents, array $options = array())
{
$wsdl = $wsdlContents !== null ? 'data://text/plain;base64,' . base64_encode($wsdlContents) : null;

$this->browser = $browser ?? new Browser();

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

$this->browser = $browser;
$this->encoder = new ClientEncoder($wsdl, $options);
$this->decoder = new ClientDecoder($wsdl, $options);
}
Expand Down
Loading

0 comments on commit b15947f

Please sign in to comment.