Skip to content

Commit e488db0

Browse files
authored
Merge pull request #504 from PrintNow/fix/php82-dynamic-properties
Fix: Add server proxy classes for PHP 8.2+ dynamic properties compatibility
2 parents b4e5451 + a463bbc commit e488db0

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Hhxsv5\LaravelS\Swoole\Proxy;
4+
5+
use Swoole\Http\Server;
6+
7+
/**
8+
* HTTP Server Proxy for PHP 8.2+ dynamic properties compatibility
9+
*
10+
* Extends Swoole\Http\Server to support dynamic properties (e.g., Swoole Tables)
11+
* using the #[\AllowDynamicProperties] attribute.
12+
*/
13+
#[\AllowDynamicProperties]
14+
class HttpServerProxy extends Server
15+
{
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Hhxsv5\LaravelS\Swoole\Proxy;
4+
5+
use Swoole\WebSocket\Server;
6+
7+
/**
8+
* WebSocket Server Proxy for PHP 8.2+ dynamic properties compatibility
9+
*
10+
* Extends Swoole\WebSocket\Server to support dynamic properties (e.g., Swoole Tables)
11+
* using the #[\AllowDynamicProperties] attribute.
12+
*/
13+
#[\AllowDynamicProperties]
14+
class WebSocketServerProxy extends Server
15+
{
16+
}

src/Swoole/Server.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Hhxsv5\LaravelS\Swoole\Task\Event;
1111
use Hhxsv5\LaravelS\Swoole\Task\Listener;
1212
use Hhxsv5\LaravelS\Swoole\Task\Task;
13+
use Hhxsv5\LaravelS\Swoole\Proxy\HttpServerProxy;
14+
use Hhxsv5\LaravelS\Swoole\Proxy\WebSocketServerProxy;
1315
use Swoole\Http\Request as SwooleRequest;
1416
use Swoole\Http\Response as SwooleResponse;
1517
use Swoole\Http\Server as HttpServer;
@@ -22,10 +24,13 @@ class Server
2224
use LogTrait;
2325
use ProcessTitleTrait;
2426

27+
/** Suffix for dynamic table properties to avoid naming conflicts */
28+
public const TABLE_PROPERTY_SUFFIX = 'Table';
29+
2530
/**@var array */
2631
protected $conf;
2732

28-
/**@var HttpServer|WebSocketServer */
33+
/**@var HttpServer|WebSocketServer|HttpServerProxy|WebSocketServerProxy */
2934
protected $swoole;
3035

3136
/**@var bool */
@@ -50,7 +55,10 @@ protected function __construct(array $conf)
5055
$settings = isset($conf['swoole']) ? $conf['swoole'] : [];
5156
$settings['enable_static_handler'] = !empty($conf['handle_static']);
5257

53-
$serverClass = $this->enableWebSocket ? WebSocketServer::class : HttpServer::class;
58+
// Use proxy classes to support dynamic properties in PHP 8.2+
59+
$serverClass = $this->enableWebSocket
60+
? WebSocketServerProxy::class
61+
: HttpServerProxy::class;
5462
if (isset($settings['ssl_cert_file'], $settings['ssl_key_file'])) {
5563
$this->swoole = new $serverClass($ip, $port, SWOOLE_PROCESS, $socketType | SWOOLE_SSL);
5664
} else {
@@ -235,7 +243,7 @@ protected function bindSwooleTables()
235243
}
236244
}
237245
$t->create();
238-
$name .= 'Table'; // Avoid naming conflicts
246+
$name .= self::TABLE_PROPERTY_SUFFIX; // Avoid naming conflicts
239247
$this->swoole->{$name} = $t;
240248
}
241249
}

0 commit comments

Comments
 (0)