diff --git a/README.md b/README.md index c4cd7a80..2244664a 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ handle multiple concurrent connections without blocking. * [SecureConnector](#secureconnector) * [TimeoutConnector](#timeoutconnector) * [UnixConnector](#unixconnector) + * [FixUriConnector](#fixeduriconnector) * [Install](#install) * [Tests](#tests) * [License](#license) @@ -1220,6 +1221,26 @@ As such, calling `cancel()` on the resulting promise has no effect. The [`getLocalAddress()`](#getlocaladdress) method will most likely return a `null` value as this value is not applicable to UDS connections here. +#### FixedUriConnector + +The `FixedUriConnector` class implements the +[`ConnectorInterface`](#connectorinterface) and decorates an existing Connector +to always use a fixed, preconfigured URI. + +This can be useful for consumers that do not support certain URIs, such as +when you want to explicitly connect to a Unix domain socket (UDS) path +instead of connecting to a default address assumed by an higher-level API: + +```php +$connector = new FixedUriConnector( + 'unix:///var/run/docker.sock', + new UnixConnector($loop) +); + +// destination will be ignored, actually connects to Unix domain socket +$promise = $connector->connect('localhost:80'); +``` + ## Install The recommended way to install this library is [through Composer](https://getcomposer.org). diff --git a/src/FixedUriConnector.php b/src/FixedUriConnector.php new file mode 100644 index 00000000..62da040f --- /dev/null +++ b/src/FixedUriConnector.php @@ -0,0 +1,43 @@ +connect('localhost:80'); + * ``` + */ +class FixedUriConnector implements ConnectorInterface +{ + private $uri; + private $connector; + + /** + * @param string $uri + * @param ConnectorInterface $connector + */ + public function __construct($uri, ConnectorInterface $connector) + { + $this->uri = $uri; + $this->connector = $connector; + } + + public function connect($_) + { + return $this->connector->connect($this->uri); + } +} diff --git a/tests/FixedUriConnectorTest.php b/tests/FixedUriConnectorTest.php new file mode 100644 index 00000000..f42d74fe --- /dev/null +++ b/tests/FixedUriConnectorTest.php @@ -0,0 +1,19 @@ +getMockBuilder('React\Socket\ConnectorInterface')->getMock(); + $base->expects($this->once())->method('connect')->with('test')->willReturn('ret'); + + $connector = new FixedUriConnector('test', $base); + + $this->assertEquals('ret', $connector->connect('ignored')); + } +}