Skip to content
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
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ before_install:

install:
- cd $TRAVIS_BUILD_DIR
- go build -o app example/Server.go
- bash ./tests/swoole.install.sh
- phpenv config-rm xdebug.ini || echo "xdebug not available"
- phpenv config-add ./tests/ci.ini
Expand Down
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ func main() {
```php
<?php

use Reasno\GoTask\Relay\CoroutineSocketRelay;
use Spiral\Goridge\RPC;
use Reasno\GoTask\IPC\SocketIPC;
use function Swoole\Coroutine\run;

require_once "../vendor/autoload.php";

run(function(){
$task = new RPC(
new CoroutineSocketRelay("127.0.0.1", 6001)
);
$task = new SocketIPC('127.0.0.1:6379');
var_dump($task->call("App.Hi", "Reasno"));
// 打印 [ "hello" => "Reasno" ]
});
Expand Down Expand Up @@ -101,14 +98,14 @@ declare(strict_types=1);

namespace App\Controller;

use Reasno\GoTask\RemoteGoTask;
use Reasno\GoTask\GoTask;

class IndexController extends AbstractController
{
/**
* @return array
*/
public function index(RemoteGoTask $task)
public function index(GoTask $task)
{
return $task->call('App.Hi', ['Swoole is Awesome,', 'So is Go!']);
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func (a *App) Hi(name interface{}, r *interface{}) error {
func main() {
gotask.Register(new(App))
if err := gotask.Run(); err != nil {
log.Fatal(err)
log.Fatalln(err)
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"sort-packages": true
},
"scripts": {
"test": "phpunit -c phpunit.xml --colors=always",
"test": "go build -o app example/Server.go && phpunit -c phpunit.xml --colors=always",
"analyse": "phpstan analyse --memory-limit 300M -l 0 ./src",
"cs-fix": "php-cs-fixer fix $1"
},
Expand Down
23 changes: 15 additions & 8 deletions example/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

use Reasno\GoTask\Relay\CoroutineSocketRelay;
use Spiral\Goridge\RelayInterface;
use Spiral\Goridge\RPC;
use Reasno\GoTask\GoTask;
use Reasno\GoTask\IPC\SocketIPC;
use Swoole\Process;
use function Swoole\Coroutine\run;

require '../vendor/autoload.php';

const ADDR = '127.0.0.1:6001';

$process = new Process(function (Process $process) {
$process->exec(__DIR__ . '/../app', ['-address', ADDR]);
});
$process->start();

sleep(1);

run(function () {
$task = new RPC(
new CoroutineSocketRelay('127.0.0.1', 6001)
);
$task = new SocketIPC(ADDR);
var_dump($task->call('App.HelloString', 'Reasno'));
var_dump($task->call('App.HelloInterface', ['jack', 'jill']));
var_dump($task->call('App.HelloStruct', [
'firstName' => 'LeBron',
'lastName' => 'James',
'id' => 23,
]));
var_dump($task->call('App.HelloBytes', base64_encode('My Bytes'), RelayInterface::PAYLOAD_RAW));
var_dump($task->call('App.HelloBytes', base64_encode('My Bytes'), GoTask::PAYLOAD_RAW));
try {
$task->call('App.HelloError', 'Reasno');
} catch (\Throwable $e) {
Expand Down
2 changes: 1 addition & 1 deletion example/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func (a *App) HelloError(name interface{}, r *interface{}) error {
func main() {
gotask.Register(new(App))
if err := gotask.Run(); err != nil {
log.Fatal(err)
log.Fatalln(err)
}
}
2 changes: 1 addition & 1 deletion publish/gotask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
Expand Down
9 changes: 6 additions & 3 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
Expand All @@ -12,22 +12,25 @@

namespace Reasno\GoTask;

use Reasno\GoTask\Listener\BootApplicationListener;
use Reasno\GoTask\Process\GoTaskProcess;
use Spiral\Goridge\RPC;

class ConfigProvider
{
public function __invoke(): array
{
return [
'dependencies' => [
GoTask::class => RemoteGoTask::class,
GoTask::class => GoTaskFactory::class,
],
'commands' => [
],
'processes' => [
GoTaskProcess::class,
],
'listener' => [
BootApplicationListener::class,
],
'annotations' => [
'scan' => [
'paths' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidGoTaskConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
Expand Down
21 changes: 20 additions & 1 deletion src/GoTask.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Reasno\GoTask;


interface GoTask
{
/** Payload flags.*/
const PAYLOAD_NONE = 2;
const PAYLOAD_RAW = 4;
const PAYLOAD_ERROR = 8;
const PAYLOAD_CONTROL = 16;
/**
* @param mixed $payload an binary data or array of arguments for complex types
* @param int $flags payload control flags
*
* @return mixed
*/
public function call(string $method, $payload, int $flags = 0);
}
7 changes: 4 additions & 3 deletions src/GoTaskConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
Expand Down Expand Up @@ -30,12 +30,13 @@ class GoTaskConnection extends Connection implements ConnectionInterface
* @var RPC
*/
private $connection;

/**
* @var RPCFactory
* @var SocketIPCFactory
*/
private $factory;

public function __construct(ContainerInterface $container, Pool $pool, RPCFactory $factory)
public function __construct(ContainerInterface $container, Pool $pool, SocketIPCFactory $factory)
{
parent::__construct($container, $pool);
$this->factory = $factory;
Expand Down
2 changes: 1 addition & 1 deletion src/GoTaskConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);
/**
* This file is part of Reasno/RemoteGoTask.
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
Expand Down
28 changes: 28 additions & 0 deletions src/GoTaskFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);
/**
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Reasno\GoTask;

use PhpCsFixer\ConfigInterface;
use Psr\Container\ContainerInterface;

class GoTaskFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get(ConfigInterface::class);
if ($config->get('gotask.socket_address', false)) {
return $container->get(SocketGoTask::class);
}
return $container->get(PipeGoTask::class);
}
}
17 changes: 17 additions & 0 deletions src/IPC/IPCInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);
/**
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Reasno\GoTask\IPC;

interface IPCInterface
{
}
47 changes: 47 additions & 0 deletions src/IPC/PipeIPC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Reasno\GoTask\IPC;

use Reasno\GoTask\GoTask;
use Reasno\GoTask\Relay\IPCRelay;
use Spiral\Goridge\RPC;

class PipeIPC implements IPCInterface, GoTask
{
/**
* @var RPC
*/
private $handler;

/**
* PipeIPC constructor.
* @param $process
* @mixin RPC
*/
public function __construct($process)
{
$this->handler = new RPC(
new IPCRelay($process)
);
}

public function __call($name, $arguments)
{
$this->handler->{$name}(...$arguments);
}

public function call(string $method, $payload, int $flags = 0)
{
return $this->handler->call($method, $payload, $flags);
}
}
54 changes: 54 additions & 0 deletions src/IPC/SocketIPC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);
/**
* This file is part of Reasno/GoTask.
*
* @link https://www.github.com/reasno/gotask
* @document https://www.github.com/reasno/gotask
* @contact guxi99@gmail.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Reasno\GoTask\IPC;

use Reasno\GoTask\GoTask;
use Reasno\GoTask\Relay\CoroutineSocketRelay;
use Spiral\Goridge\RPC;

class SocketIPC implements IPCInterface, GoTask
{
/**
* @var RPC
*/
private $handler;

/**
* PipeIPC constructor.
* @mixin RPC
*/
public function __construct(string $address = '127.0.0.1:6001')
{
$split = explode(':', $address, 2);
if (count($split) === 1) {
$this->handler = new RPC(
new CoroutineSocketRelay($split[0], 0, CoroutineSocketRelay::SOCK_UNIX)
);
return;
}
[$host, $port] = $split;
$this->handler = new RPC(
new CoroutineSocketRelay($host, (int) $port, CoroutineSocketRelay::SOCK_TCP)
);
}

public function __call($name, $arguments)
{
$this->handler->{$name}(...$arguments);
}

public function call(string $method, $payload, int $flags = 0)
{
return $this->handler->call($method, $payload, $flags);
}
}
Loading