Skip to content

Commit c26f7fb

Browse files
committed
Fix AsyncSocket constants for environments without Swoole loaded
1 parent 43e7d86 commit c26f7fb

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/Socket/AsyncSocket.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ class AsyncSocket
1919
private int $type;
2020
private float $timeout;
2121

22-
public const TYPE_TCP = \SWOOLE_SOCK_TCP;
23-
public const TYPE_UDP = \SWOOLE_SOCK_UDP;
24-
public const TYPE_TCP6 = \SWOOLE_SOCK_TCP6;
25-
public const TYPE_UDP6 = \SWOOLE_SOCK_UDP6;
26-
public const TYPE_UNIX_STREAM = \SWOOLE_SOCK_UNIX_STREAM;
27-
public const TYPE_UNIX_DGRAM = \SWOOLE_SOCK_UNIX_DGRAM;
28-
29-
public function __construct(int $type = self::TYPE_TCP, float $timeout = 5.0)
30-
{
22+
// Constants defined conditionally to avoid errors when Swoole is not loaded
23+
public const TYPE_TCP = 1; // SWOOLE_SOCK_TCP
24+
public const TYPE_UDP = 2; // SWOOLE_SOCK_UDP
25+
public const TYPE_TCP6 = 3; // SWOOLE_SOCK_TCP6
26+
public const TYPE_UDP6 = 4; // SWOOLE_SOCK_UDP6
27+
public const TYPE_UNIX_STREAM = 5; // SWOOLE_SOCK_UNIX_STREAM
28+
public const TYPE_UNIX_DGRAM = 6; // SWOOLE_SOCK_UNIX_DGRAM
29+
30+
public function __construct(?int $type = null, float $timeout = 5.0)
31+
{
32+
// Use Swoole constants if available, otherwise use our fallback values
33+
if ($type === null) {
34+
$type = \defined('SWOOLE_SOCK_TCP') ? \SWOOLE_SOCK_TCP : self::TYPE_TCP;
35+
}
3136
$this->type = $type;
3237
$this->timeout = $timeout;
3338
}

0 commit comments

Comments
 (0)