-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from clue-labs/fixed-uri
Add FixedUriConnector decorator to use fixed, preconfigured URI instead
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |