Skip to content

Commit 9c21d05

Browse files
authored
Merge pull request #117 from clue-labs/fixed-uri
Add FixedUriConnector decorator to use fixed, preconfigured URI instead
2 parents 03f4f6a + 0c266d8 commit 9c21d05

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ handle multiple concurrent connections without blocking.
4646
* [SecureConnector](#secureconnector)
4747
* [TimeoutConnector](#timeoutconnector)
4848
* [UnixConnector](#unixconnector)
49+
* [FixUriConnector](#fixeduriconnector)
4950
* [Install](#install)
5051
* [Tests](#tests)
5152
* [License](#license)
@@ -1220,6 +1221,26 @@ As such, calling `cancel()` on the resulting promise has no effect.
12201221
The [`getLocalAddress()`](#getlocaladdress) method will most likely return a
12211222
`null` value as this value is not applicable to UDS connections here.
12221223

1224+
#### FixedUriConnector
1225+
1226+
The `FixedUriConnector` class implements the
1227+
[`ConnectorInterface`](#connectorinterface) and decorates an existing Connector
1228+
to always use a fixed, preconfigured URI.
1229+
1230+
This can be useful for consumers that do not support certain URIs, such as
1231+
when you want to explicitly connect to a Unix domain socket (UDS) path
1232+
instead of connecting to a default address assumed by an higher-level API:
1233+
1234+
```php
1235+
$connector = new FixedUriConnector(
1236+
'unix:///var/run/docker.sock',
1237+
new UnixConnector($loop)
1238+
);
1239+
1240+
// destination will be ignored, actually connects to Unix domain socket
1241+
$promise = $connector->connect('localhost:80');
1242+
```
1243+
12231244
## Install
12241245

12251246
The recommended way to install this library is [through Composer](https://getcomposer.org).

src/FixedUriConnector.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace React\Socket;
4+
5+
use React\Socket\ConnectorInterface;
6+
7+
/**
8+
* Decorates an existing Connector to always use a fixed, preconfigured URI
9+
*
10+
* This can be useful for consumers that do not support certain URIs, such as
11+
* when you want to explicitly connect to a Unix domain socket (UDS) path
12+
* instead of connecting to a default address assumed by an higher-level API:
13+
*
14+
* ```php
15+
* $connector = new FixedUriConnector(
16+
* 'unix:///var/run/docker.sock',
17+
* new UnixConnector($loop)
18+
* );
19+
*
20+
* // destination will be ignored, actually connects to Unix domain socket
21+
* $promise = $connector->connect('localhost:80');
22+
* ```
23+
*/
24+
class FixedUriConnector implements ConnectorInterface
25+
{
26+
private $uri;
27+
private $connector;
28+
29+
/**
30+
* @param string $uri
31+
* @param ConnectorInterface $connector
32+
*/
33+
public function __construct($uri, ConnectorInterface $connector)
34+
{
35+
$this->uri = $uri;
36+
$this->connector = $connector;
37+
}
38+
39+
public function connect($_)
40+
{
41+
return $this->connector->connect($this->uri);
42+
}
43+
}

tests/FixedUriConnectorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace React\Tests\Socket;
4+
5+
use React\Socket\FixedUriConnector;
6+
use React\Tests\Socket\TestCase;
7+
8+
class FixedUriConnectorTest extends TestCase
9+
{
10+
public function testWillInvokeGivenConnector()
11+
{
12+
$base = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
13+
$base->expects($this->once())->method('connect')->with('test')->willReturn('ret');
14+
15+
$connector = new FixedUriConnector('test', $base);
16+
17+
$this->assertEquals('ret', $connector->connect('ignored'));
18+
}
19+
}

0 commit comments

Comments
 (0)