Skip to content

Commit 31fb0ef

Browse files
authored
Upgrade the minimum php version to 8.0 for websocket. (#4525)
1 parent af92a72 commit 31fb0ef

File tree

9 files changed

+27
-158
lines changed

9 files changed

+27
-158
lines changed

src/Collector/Fd.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@
1313

1414
class Fd
1515
{
16-
/**
17-
* @var int
18-
*/
19-
public $fd;
20-
21-
/**
22-
* @var string
23-
*/
24-
public $class;
25-
26-
public function __construct(int $fd, string $class)
16+
public function __construct(public int $fd, public string $class)
2717
{
28-
$this->fd = $fd;
29-
$this->class = $class;
3018
}
3119
}

src/Collector/FdCollector.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
class FdCollector
1515
{
16-
/**
17-
* @var array
18-
*/
19-
protected static $fds = [];
16+
protected static array $fds = [];
2017

2118
public static function set(int $id, string $class): void
2219
{

src/Context.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ class Context
1818
{
1919
public const FD = 'ws.fd';
2020

21-
/**
22-
* @var array
23-
*/
24-
protected static $container = [];
21+
protected static array $container = [];
2522

2623
public static function set(string $id, $value)
2724
{
@@ -33,14 +30,14 @@ public static function set(string $id, $value)
3330

3431
public static function get(string $id, $default = null, $fd = null)
3532
{
36-
$fd = $fd ?? CoContext::get(Context::FD, 0);
33+
$fd ??= CoContext::get(Context::FD, 0);
3734
$key = sprintf('%d.%s', $fd, $id);
3835
return data_get(self::$container, $key, $default);
3936
}
4037

4138
public static function has(string $id, $fd = null)
4239
{
43-
$fd = $fd ?? CoContext::get(Context::FD, 0);
40+
$fd ??= CoContext::get(Context::FD, 0);
4441
$key = sprintf('%d.%s', $fd, $id);
4542
return data_get(self::$container, $key) !== null;
4643
}
@@ -53,7 +50,7 @@ public static function destroy(string $id)
5350

5451
public static function release(?int $fd = null)
5552
{
56-
$fd = $fd ?? CoContext::get(Context::FD, 0);
53+
$fd ??= CoContext::get(Context::FD, 0);
5754
unset(self::$container[strval($fd)]);
5855
}
5956

src/Exception/Handler/WebSocketExceptionHandler.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,8 @@
2121

2222
class WebSocketExceptionHandler extends ExceptionHandler
2323
{
24-
/**
25-
* @var StdoutLoggerInterface
26-
*/
27-
protected $logger;
28-
29-
/**
30-
* @var FormatterInterface
31-
*/
32-
protected $formatter;
33-
34-
public function __construct(StdoutLoggerInterface $logger, FormatterInterface $formatter)
24+
public function __construct(protected StdoutLoggerInterface $logger, protected FormatterInterface $formatter)
3525
{
36-
$this->logger = $logger;
37-
$this->formatter = $formatter;
3826
}
3927

4028
public function handle(Throwable $throwable, ResponseInterface $response)
@@ -43,7 +31,7 @@ public function handle(Throwable $throwable, ResponseInterface $response)
4331
if ($throwable instanceof HttpException) {
4432
$response = $response->withStatus($throwable->getStatusCode());
4533
}
46-
$stream = new SwooleStream((string) $throwable->getMessage());
34+
$stream = new SwooleStream($throwable->getMessage());
4735
return $response->withBody($stream);
4836
}
4937

src/Listener/InitSenderListener.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@
1818

1919
class InitSenderListener implements ListenerInterface
2020
{
21-
/**
22-
* @var ContainerInterface
23-
*/
24-
private $container;
25-
26-
public function __construct(ContainerInterface $container)
21+
public function __construct(private ContainerInterface $container)
2722
{
28-
$this->container = $container;
2923
}
3024

3125
/**

src/Listener/OnPipeMessageListener.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,8 @@
2121

2222
class OnPipeMessageListener implements ListenerInterface
2323
{
24-
/**
25-
* @var ContainerInterface
26-
*/
27-
private $container;
28-
29-
/**
30-
* @var StdoutLoggerInterface
31-
*/
32-
private $logger;
33-
34-
/**
35-
* @var Sender
36-
*/
37-
private $sender;
38-
39-
public function __construct(ContainerInterface $container, StdoutLoggerInterface $logger, Sender $sender)
24+
public function __construct(private ContainerInterface $container, private StdoutLoggerInterface $logger, private Sender $sender)
4025
{
41-
$this->container = $container;
42-
$this->logger = $logger;
43-
$this->sender = $sender;
4426
}
4527

4628
/**

src/Sender.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Hyperf\Server\CoroutineServer;
1717
use Hyperf\WebSocketServer\Exception\InvalidMethodException;
1818
use Psr\Container\ContainerInterface;
19+
use Psr\Log\LoggerInterface;
1920
use Swoole\Http\Response;
2021
use Swoole\Server;
2122

@@ -25,34 +26,19 @@
2526
*/
2627
class Sender
2728
{
28-
/**
29-
* @var ContainerInterface
30-
*/
31-
protected $container;
29+
protected LoggerInterface $logger;
3230

33-
/**
34-
* @var StdoutLoggerInterface
35-
*/
36-
protected $logger;
37-
38-
/**
39-
* @var int
40-
*/
41-
protected $workerId;
31+
protected ?int $workerId = null;
4232

4333
/**
4434
* @var Response[]
4535
*/
46-
protected $responses = [];
36+
protected array $responses = [];
4737

48-
/**
49-
* @var bool
50-
*/
51-
protected $isCoroutineServer = false;
38+
protected bool $isCoroutineServer = false;
5239

53-
public function __construct(ContainerInterface $container)
40+
public function __construct(protected ContainerInterface $container)
5441
{
55-
$this->container = $container;
5642
$this->logger = $container->get(StdoutLoggerInterface::class);
5743
if ($config = $container->get(ConfigInterface::class)) {
5844
$this->isCoroutineServer = $config->get('server.type') === CoroutineServer::class;

src/SenderPipeMessage.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@
1313

1414
class SenderPipeMessage
1515
{
16-
/**
17-
* @var string
18-
*/
19-
public $name;
20-
21-
/**
22-
* @var array
23-
*/
24-
public $arguments;
25-
26-
public function __construct(string $name, array $arguments)
16+
public function __construct(public string $name, public array $arguments)
2717
{
28-
$this->name = $name;
29-
$this->arguments = $arguments;
3018
}
3119
}

src/Server.php

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -50,68 +50,21 @@
5050

5151
class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface
5252
{
53-
/**
54-
* @var ContainerInterface
55-
*/
56-
protected $container;
57-
58-
/**
59-
* @var HttpDispatcher
60-
*/
61-
protected $dispatcher;
62-
63-
/**
64-
* @var ExceptionHandlerDispatcher
65-
*/
66-
protected $exceptionHandlerDispatcher;
53+
protected ?CoreMiddlewareInterface $coreMiddleware = null;
6754

68-
/**
69-
* @var CoreMiddlewareInterface
70-
*/
71-
protected $coreMiddleware;
55+
protected array $exceptionHandlers = [];
7256

73-
/**
74-
* @var array
75-
*/
76-
protected $exceptionHandlers;
57+
protected array $middlewares = [];
7758

78-
/**
79-
* @var ResponseEmitter
80-
*/
81-
protected $responseEmitter;
82-
83-
/**
84-
* @var StdoutLoggerInterface
85-
*/
86-
protected $logger;
87-
88-
/**
89-
* @var array
90-
*/
91-
protected $middlewares = [];
92-
93-
/**
94-
* @var string
95-
*/
96-
protected $serverName = 'websocket';
59+
protected string $serverName = 'websocket';
9760

9861
/**
9962
* @var null|\Swoole\Coroutine\Http\Server|WebSocketServer
10063
*/
101-
protected $server;
102-
103-
public function __construct(
104-
ContainerInterface $container,
105-
HttpDispatcher $dispatcher,
106-
ExceptionHandlerDispatcher $exceptionHandlerDispatcher,
107-
ResponseEmitter $responseEmitter,
108-
StdoutLoggerInterface $logger
109-
) {
110-
$this->container = $container;
111-
$this->dispatcher = $dispatcher;
112-
$this->exceptionHandlerDispatcher = $exceptionHandlerDispatcher;
113-
$this->responseEmitter = $responseEmitter;
114-
$this->logger = $logger;
64+
protected mixed $server;
65+
66+
public function __construct(protected ContainerInterface $container, protected HttpDispatcher $dispatcher, protected ExceptionHandlerDispatcher $exceptionHandlerDispatcher, protected ResponseEmitter $responseEmitter, protected StdoutLoggerInterface $logger)
67+
{
11568
}
11669

11770
public function initCoreMiddleware(string $serverName): void
@@ -126,10 +79,7 @@ public function initCoreMiddleware(string $serverName): void
12679
]);
12780
}
12881

129-
/**
130-
* @return \Swoole\Coroutine\Http\Server|WebSocketServer
131-
*/
132-
public function getServer()
82+
public function getServer(): \Swoole\Coroutine\Http\Server|WebSocketServer
13383
{
13484
if ($this->server) {
13585
return $this->server;
@@ -282,10 +232,9 @@ protected function getFd($response): int
282232
}
283233

284234
/**
285-
* @param SwooleResponse|WebSocketServer $server
286235
* @param mixed $request
287236
*/
288-
protected function deferOnOpen($request, string $class, $server)
237+
protected function deferOnOpen($request, string $class, SwooleResponse|WebSocketServer $server)
289238
{
290239
$instance = $this->container->get($class);
291240
wait(static function () use ($request, $instance, $server) {

0 commit comments

Comments
 (0)