Skip to content

Commit

Permalink
Upgrade to Hyperf 3 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante authored Jul 12, 2023
1 parent beb7a10 commit c2c5efa
Show file tree
Hide file tree
Showing 42 changed files with 222 additions and 493 deletions.
30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
}
},
"require": {
"php": ">=7.4",
"ext-swoole": ">=4.4",
"hyperf/pool": "^2.2|^3.0",
"hyperf/process": "^2.2|^3.0",
"spiral/goridge": "^2.4.1",
"symfony/event-dispatcher": "^5.1"
"php": ">=8.1",
"ext-swoole": ">=5.0",
"hyperf/pool": "^3.0",
"hyperf/process": "^3.0",
"spiral/goridge": "^2.4",
"symfony/event-dispatcher": "^6.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/command": "^2.2|^3.0",
"hyperf/config": "^2.2|^3.0",
"hyperf/di": "^2.2|^3.0",
"hyperf/framework": "^2.2|^3.0",
"hyperf/testing": "^2.2|^3.0",
"mockery/mockery": "^1.3",
"phpstan/phpstan": "^1.0",
"swoole/ide-helper": "^4.5"
"friendsofphp/php-cs-fixer": "^3.21",
"hyperf/command": "^3.0",
"hyperf/config": "^3.0",
"hyperf/di": "^3.0",
"hyperf/framework": "^3.0",
"hyperf/testing": "^3.0",
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^1.10",
"swoole/ide-helper": "^5.0"
},
"config": {
"sort-packages": true
Expand Down
11 changes: 3 additions & 8 deletions src/Config/DomainConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@

class DomainConfig
{
/**
* @var ConfigInterface
*/
private $config;

public function __construct(ConfigInterface $config)
{
$this->config = $config;
public function __construct(
private ConfigInterface $config
) {
}

public function getProcessName(): string
Expand Down
4 changes: 3 additions & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Hyperf\GoTask\Listener\PipeLockListener;
use Hyperf\GoTask\Process\GoTaskProcess;

use function Hyperf\Support\env;

class ConfigProvider
{
public function __invoke(): array
Expand Down Expand Up @@ -69,7 +71,7 @@ public function __invoke(): array
];
}

public static function address()
public static function address(): string
{
if (defined('BASE_PATH')) {
$root = BASE_PATH . '/runtime';
Expand Down
20 changes: 6 additions & 14 deletions src/GoTaskConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Hyperf\Contract\ConnectionInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\GoTask\IPC\SocketIPCSender;
use Hyperf\Pool\Connection;
use Hyperf\Pool\Exception\ConnectionException;
use Hyperf\Pool\Pool;
Expand All @@ -26,24 +27,15 @@
*/
class GoTaskConnection extends Connection implements ConnectionInterface
{
/**
* @var RPC
*/
private $connection;
private SocketIPCSender $connection;

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

public function __construct(ContainerInterface $container, Pool $pool, SocketIPCFactory $factory)
public function __construct(ContainerInterface $container, Pool $pool, private SocketIPCFactory $factory)
{
parent::__construct($container, $pool);
$this->factory = $factory;
$this->reconnect();
}

public function __call($name, $arguments)
public function __call(string $name, array $arguments): mixed
{
try {
$result = $this->connection->{$name}(...$arguments);
Expand All @@ -67,7 +59,7 @@ public function reconnect(): bool
return true;
}

public function getActiveConnection()
public function getActiveConnection(): self
{
if ($this->check()) {
return $this;
Expand All @@ -80,7 +72,7 @@ public function getActiveConnection()
return $this;
}

protected function retry($name, $arguments, Throwable $exception)
protected function retry($name, $arguments, Throwable $exception): mixed
{
$logger = $this->container->get(StdoutLoggerInterface::class);
$logger->warning(sprintf('RemoteGoTask::__call failed, because ' . $exception->getMessage()));
Expand Down
2 changes: 2 additions & 0 deletions src/GoTaskConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Hyperf\Pool\Pool;
use Psr\Container\ContainerInterface;

use function Hyperf\Support\make;

class GoTaskConnectionPool extends Pool
{
public function __construct(ContainerInterface $container, DomainConfig $config)
Expand Down
2 changes: 1 addition & 1 deletion src/GoTaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class GoTaskFactory
{
public function __invoke(ContainerInterface $container)
public function __invoke(ContainerInterface $container): GoTask
{
$config = $container->get(DomainConfig::class);
if ($config->getAddress()) {
Expand Down
18 changes: 5 additions & 13 deletions src/GoTaskProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,20 @@

class GoTaskProxy implements GoTask
{
/**
* @var GoTask
*/
private $goTask;

public function __construct(GoTask $goTask)
{
$this->goTask = $goTask;
public function __construct(
private GoTask $goTask
) {
}

public function __call($name, $arguments)
public function __call(string $name, array $arguments): mixed
{
$method = ucfirst($name);
$path = explode('\\', static::class);
$class = array_pop($path);
return $this->call($class . '.' . $method, ...$arguments);
}

/**
* {@inheritdoc}
*/
public function call(string $method, $payload, int $flags = 0)
public function call(string $method, mixed $payload, int $flags = 0): mixed
{
return $this->goTask->call($method, $payload, $flags);
}
Expand Down
4 changes: 1 addition & 3 deletions src/IPC/IPCSenderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ interface IPCSenderInterface
/**
* @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);
public function call(string $method, mixed $payload, int $flags = 0): mixed;
}
13 changes: 5 additions & 8 deletions src/IPC/PipeIPCSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,33 @@
use Hyperf\GoTask\GoTask;
use Hyperf\GoTask\Relay\ProcessPipeRelay;
use Spiral\Goridge\RPC;
use Swoole\Process;

/**
* Class PipeIPC uses pipes to communicate.
* It can only be used in one coroutine.
*/
class PipeIPCSender implements IPCSenderInterface, GoTask
{
/**
* @var RPC
*/
private $handler;
private RPC $handler;

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

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

public function call(string $method, $payload, int $flags = 0)
public function call(string $method, $payload, int $flags = 0): mixed
{
return $this->handler->call($method, $payload, $flags);
}
Expand Down
36 changes: 11 additions & 25 deletions src/IPC/SocketIPCReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,28 @@
*/
namespace Hyperf\GoTask\IPC;

use Hyperf\Context\ApplicationContext;
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
use Hyperf\GoTask\GoTask;
use Hyperf\GoTask\Relay\ConnectionRelay;
use Hyperf\GoTask\Wrapper\ByteWrapper;
use Hyperf\Utils\ApplicationContext;
use Spiral\Goridge\Exceptions\PrefixException;
use Spiral\Goridge\Exceptions\ServiceException;
use Spiral\Goridge\Exceptions\TransportException;
use Spiral\Goridge\RelayInterface as Relay;
use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection;
use Throwable;

class SocketIPCReceiver
{
/**
* @var string
*/
private $address;
private string $address;

/**
* @var \Swoole\Coroutine\Server
*/
private $server;
private ?Server $server = null;

/**
* @var int
*/
private $port;
private int $port;

/**
* @var bool
*/
private $quit;
private bool $quit;

public function __construct(string $address = '127.0.0.1:6001')
{
Expand Down Expand Up @@ -114,7 +103,7 @@ public function start(): bool
return true;
}

public function close()
public function close(): void
{
if ($this->server !== null) {
$this->quit = true;
Expand All @@ -123,7 +112,7 @@ public function close()
$this->server = null;
}

protected function dispatch($method, $payload)
protected function dispatch(string $method, mixed $payload): mixed
{
[$class, $handler] = explode('::', $method);
if (ApplicationContext::hasContainer()) {
Expand All @@ -135,20 +124,17 @@ protected function dispatch($method, $payload)
return $instance->{$handler}($payload);
}

protected function isStarted()
protected function isStarted(): bool
{
return $this->server !== null;
}

/**
* Handle response body.
*
* @param string $body
*
* @return mixed
* @throws ServiceException
*/
protected function handleBody($body, int $flags)
protected function handleBody(string $body, int $flags): mixed
{
if ($flags & GoTask::PAYLOAD_ERROR && $flags & GoTask::PAYLOAD_RAW) {
throw new ServiceException("error '{$body}' on '{$this->server}'");
Expand All @@ -161,7 +147,7 @@ protected function handleBody($body, int $flags)
return json_decode($body, true);
}

private function formatError(Throwable $error)
private function formatError(Throwable $error): string
{
$simpleFormat = $error->getMessage() . ':' . $error->getTraceAsString();
if (! ApplicationContext::hasContainer()) {
Expand Down
9 changes: 3 additions & 6 deletions src/IPC/SocketIPCSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
*/
class SocketIPCSender implements IPCSenderInterface, GoTask
{
/**
* @var RPC
*/
private $handler;
private RPC $handler;

/**
* PipeIPC constructor.
Expand All @@ -45,12 +42,12 @@ public function __construct(string $address = '127.0.0.1:6001')
);
}

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

public function call(string $method, $payload, int $flags = 0)
public function call(string $method, $payload, int $flags = 0): mixed
{
return $this->handler->call($method, $payload, $flags);
}
Expand Down
22 changes: 4 additions & 18 deletions src/Listener/CommandListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,20 @@

class CommandListener implements ListenerInterface
{
/**
* @var Process
*/
private $process;
private Process $process;

/**
* @var DomainConfig
*/
private $config;

public function __construct(DomainConfig $config)
{
$this->config = $config;
public function __construct(
private DomainConfig $config
) {
}

/**
* {@inheritdoc}
*/
public function listen(): array
{
return [
ConsoleCommandEvent::class,
];
}

/**
* {@inheritdoc}
*/
public function process(object $event): void
{
if (! $this->config->isEnabled()) {
Expand Down
Loading

0 comments on commit c2c5efa

Please sign in to comment.