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

Feature/relay factory #56

Merged
merged 4 commits into from
Apr 30, 2020
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ use Spiral\Goridge;
require "vendor/autoload.php";

$rpc = new Goridge\RPC(new Goridge\SocketRelay("127.0.0.1", 6001));
//or, using factory:
$tcpRPC = Goridge\Relay::create('tcp://127.0.0.1:6001');
$unixRPC = Goridge\Relay::create('unix:///tmp/rpc.sock');
$streamRPC = Goridge\Relay::create('pipes://stdin:stdout');

echo $rpc->call("App.Hi", "Antony");
```
> Factory applies the next format: `<protocol>://<arg1>:<arg2>`

```go
package main
Expand Down
12 changes: 12 additions & 0 deletions php-src/Exceptions/RelayFactoryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Dead simple, high performance, drop-in bridge to Golang RPC with zero dependencies
*
* @author Valentin V
*/

namespace Spiral\Goridge\Exceptions;

class RelayFactoryException extends GoridgeException
{
}
47 changes: 47 additions & 0 deletions php-src/Relay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Dead simple, high performance, drop-in bridge to Golang RPC with zero dependencies
*
* @author Valentin V
*/

namespace Spiral\Goridge;

abstract class Relay
{
public const TCP_SOCKET = 'tcp';
public const UNIX_SOCKET = 'unix';
public const STREAM = 'pipes';

private const CONNECTION = '/(?P<protocol>[^:\/]+):\/\/(?P<arg1>[^:]+)(:(?P<arg2>[^:]+))?/';

public static function create(string $connection): RelayInterface
{
if (!preg_match(self::CONNECTION, strtolower($connection), $match)) {
throw new Exceptions\RelayFactoryException('unsupported connection format');
}

switch ($match['protocol']) {
case self::TCP_SOCKET:
//fall through
case self::UNIX_SOCKET:
return new SocketRelay(
$match['arg1'],
isset($match['arg2']) ? (int)$match['arg2'] : null,
$match['protocol'] === self::TCP_SOCKET ? SocketRelay::SOCK_TCP : SocketRelay::SOCK_UNIX
);

case self::STREAM:
if (!isset($match['arg2'])) {
throw new Exceptions\RelayFactoryException('unsupported stream connection format');
}

return new StreamRelay(
fopen("php://{$match['arg1']}", 'rb'),
fopen("php://{$match['arg2']}", 'wb')
);
default:
throw new Exceptions\RelayFactoryException('unknown connection protocol');
}
}
}
93 changes: 93 additions & 0 deletions tests/Cases/RelayFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* Dead simple, high performance, drop-in bridge to Golang RPC with zero dependencies
*
* @author Valentin V
*/

namespace Cases;

use PHPUnit\Framework\TestCase;
use Spiral\Goridge\Exceptions;
use Spiral\Goridge\Relay;
use Spiral\Goridge\SocketRelay;
use Spiral\Goridge\StreamRelay;
use Throwable;

class RelayFactoryTest extends TestCase
{
/**
* @dataProvider formatProvider
* @param string $connection
* @param bool $expectedException
*/
public function testFormat(string $connection, bool $expectedException = false): void
{
$this->assertTrue(true);
if ($expectedException) {
$this->expectException(Exceptions\RelayFactoryException::class);
}

try {
Relay::create($connection);
} catch (Exceptions\RelayFactoryException $exception) {
throw $exception;
} catch (Throwable $exception) {
//do nothing, that's not a factory issue
}
}

/**
* @return iterable
*/
public function formatProvider(): iterable
{
return [
//format invalid
['tcp:localhost:', true],
['tcp:/localhost:', true],
['tcp//localhost:', true],
['tcp//localhost', true],
//unknown provider
['test://localhost', true],
//pipes require 2 args
['pipes://localhost:', true],
['pipes://localhost', true],
//valid format
['tcp://localhost'],
['tcp://localhost:123'],
['unix://localhost:123'],
['unix://rpc.sock'],
['unix:///tmp/rpc.sock'],
['tcp://localhost:abc'],
['pipes://stdin:stdout'],
];
}

public function testTCP(): void
{
/** @var SocketRelay $relay */
$relay = Relay::create('tcp://localhost:0');
$this->assertInstanceOf(SocketRelay::class, $relay);
$this->assertSame('localhost', $relay->getAddress());
$this->assertSame(0, $relay->getPort());
$this->assertSame(SocketRelay::SOCK_TCP, $relay->getType());
}

public function testUnix(): void
{
/** @var SocketRelay $relay */
$relay = Relay::create('unix:///tmp/rpc.sock');
$this->assertInstanceOf(SocketRelay::class, $relay);
$this->assertSame('/tmp/rpc.sock', $relay->getAddress());
$this->assertSame(SocketRelay::SOCK_UNIX, $relay->getType());
}

public function testPipes(): void
{
/** @var StreamRelay $relay */
$relay = Relay::create('pipes://stdin:stdout');
$this->assertInstanceOf(StreamRelay::class, $relay);
}
}