Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FixedUriConnector decorator to use fixed, preconfigured URI instead #117

Merged
merged 1 commit into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
}
}