Skip to content

Commit c2c5efa

Browse files
Upgrade to Hyperf 3 (#62)
1 parent beb7a10 commit c2c5efa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+222
-493
lines changed

composer.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
}
1919
},
2020
"require": {
21-
"php": ">=7.4",
22-
"ext-swoole": ">=4.4",
23-
"hyperf/pool": "^2.2|^3.0",
24-
"hyperf/process": "^2.2|^3.0",
25-
"spiral/goridge": "^2.4.1",
26-
"symfony/event-dispatcher": "^5.1"
21+
"php": ">=8.1",
22+
"ext-swoole": ">=5.0",
23+
"hyperf/pool": "^3.0",
24+
"hyperf/process": "^3.0",
25+
"spiral/goridge": "^2.4",
26+
"symfony/event-dispatcher": "^6.3"
2727
},
2828
"require-dev": {
29-
"friendsofphp/php-cs-fixer": "^3.0",
30-
"hyperf/command": "^2.2|^3.0",
31-
"hyperf/config": "^2.2|^3.0",
32-
"hyperf/di": "^2.2|^3.0",
33-
"hyperf/framework": "^2.2|^3.0",
34-
"hyperf/testing": "^2.2|^3.0",
35-
"mockery/mockery": "^1.3",
36-
"phpstan/phpstan": "^1.0",
37-
"swoole/ide-helper": "^4.5"
29+
"friendsofphp/php-cs-fixer": "^3.21",
30+
"hyperf/command": "^3.0",
31+
"hyperf/config": "^3.0",
32+
"hyperf/di": "^3.0",
33+
"hyperf/framework": "^3.0",
34+
"hyperf/testing": "^3.0",
35+
"mockery/mockery": "^1.6",
36+
"phpstan/phpstan": "^1.10",
37+
"swoole/ide-helper": "^5.0"
3838
},
3939
"config": {
4040
"sort-packages": true

src/Config/DomainConfig.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515

1616
class DomainConfig
1717
{
18-
/**
19-
* @var ConfigInterface
20-
*/
21-
private $config;
22-
23-
public function __construct(ConfigInterface $config)
24-
{
25-
$this->config = $config;
18+
public function __construct(
19+
private ConfigInterface $config
20+
) {
2621
}
2722

2823
public function getProcessName(): string

src/ConfigProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Hyperf\GoTask\Listener\PipeLockListener;
1818
use Hyperf\GoTask\Process\GoTaskProcess;
1919

20+
use function Hyperf\Support\env;
21+
2022
class ConfigProvider
2123
{
2224
public function __invoke(): array
@@ -69,7 +71,7 @@ public function __invoke(): array
6971
];
7072
}
7173

72-
public static function address()
74+
public static function address(): string
7375
{
7476
if (defined('BASE_PATH')) {
7577
$root = BASE_PATH . '/runtime';

src/GoTaskConnection.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Hyperf\Contract\ConnectionInterface;
1515
use Hyperf\Contract\StdoutLoggerInterface;
16+
use Hyperf\GoTask\IPC\SocketIPCSender;
1617
use Hyperf\Pool\Connection;
1718
use Hyperf\Pool\Exception\ConnectionException;
1819
use Hyperf\Pool\Pool;
@@ -26,24 +27,15 @@
2627
*/
2728
class GoTaskConnection extends Connection implements ConnectionInterface
2829
{
29-
/**
30-
* @var RPC
31-
*/
32-
private $connection;
30+
private SocketIPCSender $connection;
3331

34-
/**
35-
* @var SocketIPCFactory
36-
*/
37-
private $factory;
38-
39-
public function __construct(ContainerInterface $container, Pool $pool, SocketIPCFactory $factory)
32+
public function __construct(ContainerInterface $container, Pool $pool, private SocketIPCFactory $factory)
4033
{
4134
parent::__construct($container, $pool);
42-
$this->factory = $factory;
4335
$this->reconnect();
4436
}
4537

46-
public function __call($name, $arguments)
38+
public function __call(string $name, array $arguments): mixed
4739
{
4840
try {
4941
$result = $this->connection->{$name}(...$arguments);
@@ -67,7 +59,7 @@ public function reconnect(): bool
6759
return true;
6860
}
6961

70-
public function getActiveConnection()
62+
public function getActiveConnection(): self
7163
{
7264
if ($this->check()) {
7365
return $this;
@@ -80,7 +72,7 @@ public function getActiveConnection()
8072
return $this;
8173
}
8274

83-
protected function retry($name, $arguments, Throwable $exception)
75+
protected function retry($name, $arguments, Throwable $exception): mixed
8476
{
8577
$logger = $this->container->get(StdoutLoggerInterface::class);
8678
$logger->warning(sprintf('RemoteGoTask::__call failed, because ' . $exception->getMessage()));

src/GoTaskConnectionPool.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Hyperf\Pool\Pool;
1818
use Psr\Container\ContainerInterface;
1919

20+
use function Hyperf\Support\make;
21+
2022
class GoTaskConnectionPool extends Pool
2123
{
2224
public function __construct(ContainerInterface $container, DomainConfig $config)

src/GoTaskFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class GoTaskFactory
1818
{
19-
public function __invoke(ContainerInterface $container)
19+
public function __invoke(ContainerInterface $container): GoTask
2020
{
2121
$config = $container->get(DomainConfig::class);
2222
if ($config->getAddress()) {

src/GoTaskProxy.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,20 @@
1313

1414
class GoTaskProxy implements GoTask
1515
{
16-
/**
17-
* @var GoTask
18-
*/
19-
private $goTask;
20-
21-
public function __construct(GoTask $goTask)
22-
{
23-
$this->goTask = $goTask;
16+
public function __construct(
17+
private GoTask $goTask
18+
) {
2419
}
2520

26-
public function __call($name, $arguments)
21+
public function __call(string $name, array $arguments): mixed
2722
{
2823
$method = ucfirst($name);
2924
$path = explode('\\', static::class);
3025
$class = array_pop($path);
3126
return $this->call($class . '.' . $method, ...$arguments);
3227
}
3328

34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function call(string $method, $payload, int $flags = 0)
29+
public function call(string $method, mixed $payload, int $flags = 0): mixed
3830
{
3931
return $this->goTask->call($method, $payload, $flags);
4032
}

src/IPC/IPCSenderInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ interface IPCSenderInterface
2525
/**
2626
* @param mixed $payload an binary data or array of arguments for complex types
2727
* @param int $flags payload control flags
28-
*
29-
* @return mixed
3028
*/
31-
public function call(string $method, $payload, int $flags = 0);
29+
public function call(string $method, mixed $payload, int $flags = 0): mixed;
3230
}

src/IPC/PipeIPCSender.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,33 @@
1414
use Hyperf\GoTask\GoTask;
1515
use Hyperf\GoTask\Relay\ProcessPipeRelay;
1616
use Spiral\Goridge\RPC;
17+
use Swoole\Process;
1718

1819
/**
1920
* Class PipeIPC uses pipes to communicate.
2021
* It can only be used in one coroutine.
2122
*/
2223
class PipeIPCSender implements IPCSenderInterface, GoTask
2324
{
24-
/**
25-
* @var RPC
26-
*/
27-
private $handler;
25+
private RPC $handler;
2826

2927
/**
3028
* PipeIPC constructor.
3129
* @mixin RPC
32-
* @param mixed $process
3330
*/
34-
public function __construct($process)
31+
public function __construct(Process $process)
3532
{
3633
$this->handler = new RPC(
3734
new ProcessPipeRelay($process)
3835
);
3936
}
4037

41-
public function __call($name, $arguments)
38+
public function __call(string $name, array $arguments): void
4239
{
4340
$this->handler->{$name}(...$arguments);
4441
}
4542

46-
public function call(string $method, $payload, int $flags = 0)
43+
public function call(string $method, $payload, int $flags = 0): mixed
4744
{
4845
return $this->handler->call($method, $payload, $flags);
4946
}

src/IPC/SocketIPCReceiver.php

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,28 @@
1111
*/
1212
namespace Hyperf\GoTask\IPC;
1313

14+
use Hyperf\Context\ApplicationContext;
1415
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
1516
use Hyperf\GoTask\GoTask;
1617
use Hyperf\GoTask\Relay\ConnectionRelay;
1718
use Hyperf\GoTask\Wrapper\ByteWrapper;
18-
use Hyperf\Utils\ApplicationContext;
1919
use Spiral\Goridge\Exceptions\PrefixException;
2020
use Spiral\Goridge\Exceptions\ServiceException;
2121
use Spiral\Goridge\Exceptions\TransportException;
2222
use Spiral\Goridge\RelayInterface as Relay;
23+
use Swoole\Coroutine\Server;
2324
use Swoole\Coroutine\Server\Connection;
2425
use Throwable;
2526

2627
class SocketIPCReceiver
2728
{
28-
/**
29-
* @var string
30-
*/
31-
private $address;
29+
private string $address;
3230

33-
/**
34-
* @var \Swoole\Coroutine\Server
35-
*/
36-
private $server;
31+
private ?Server $server = null;
3732

38-
/**
39-
* @var int
40-
*/
41-
private $port;
33+
private int $port;
4234

43-
/**
44-
* @var bool
45-
*/
46-
private $quit;
35+
private bool $quit;
4736

4837
public function __construct(string $address = '127.0.0.1:6001')
4938
{
@@ -114,7 +103,7 @@ public function start(): bool
114103
return true;
115104
}
116105

117-
public function close()
106+
public function close(): void
118107
{
119108
if ($this->server !== null) {
120109
$this->quit = true;
@@ -123,7 +112,7 @@ public function close()
123112
$this->server = null;
124113
}
125114

126-
protected function dispatch($method, $payload)
115+
protected function dispatch(string $method, mixed $payload): mixed
127116
{
128117
[$class, $handler] = explode('::', $method);
129118
if (ApplicationContext::hasContainer()) {
@@ -135,20 +124,17 @@ protected function dispatch($method, $payload)
135124
return $instance->{$handler}($payload);
136125
}
137126

138-
protected function isStarted()
127+
protected function isStarted(): bool
139128
{
140129
return $this->server !== null;
141130
}
142131

143132
/**
144133
* Handle response body.
145134
*
146-
* @param string $body
147-
*
148-
* @return mixed
149135
* @throws ServiceException
150136
*/
151-
protected function handleBody($body, int $flags)
137+
protected function handleBody(string $body, int $flags): mixed
152138
{
153139
if ($flags & GoTask::PAYLOAD_ERROR && $flags & GoTask::PAYLOAD_RAW) {
154140
throw new ServiceException("error '{$body}' on '{$this->server}'");
@@ -161,7 +147,7 @@ protected function handleBody($body, int $flags)
161147
return json_decode($body, true);
162148
}
163149

164-
private function formatError(Throwable $error)
150+
private function formatError(Throwable $error): string
165151
{
166152
$simpleFormat = $error->getMessage() . ':' . $error->getTraceAsString();
167153
if (! ApplicationContext::hasContainer()) {

0 commit comments

Comments
 (0)