Skip to content

Commit

Permalink
Merge pull request #117 from clue-labs/fixed-uri
Browse files Browse the repository at this point in the history
Add FixedUriConnector decorator to use fixed, preconfigured URI instead
  • Loading branch information
WyriHaximus authored Sep 16, 2017
2 parents 03f4f6a + 0c266d8 commit 9c21d05
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ handle multiple concurrent connections without blocking.
* [SecureConnector](#secureconnector)
* [TimeoutConnector](#timeoutconnector)
* [UnixConnector](#unixconnector)
* [FixUriConnector](#fixeduriconnector)
* [Install](#install)
* [Tests](#tests)
* [License](#license)
Expand Down Expand Up @@ -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).
Expand Down
43 changes: 43 additions & 0 deletions src/FixedUriConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace React\Socket;

use React\Socket\ConnectorInterface;

/**
* 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');
* ```
*/
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);
}
}
19 changes: 19 additions & 0 deletions tests/FixedUriConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace React\Tests\Socket;

use React\Socket\FixedUriConnector;
use React\Tests\Socket\TestCase;

class FixedUriConnectorTest extends TestCase
{
public function testWillInvokeGivenConnector()
{
$base = $this->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'));
}
}

0 comments on commit 9c21d05

Please sign in to comment.