-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServerFactory.php
More file actions
122 lines (100 loc) · 3.29 KB
/
ServerFactory.php
File metadata and controls
122 lines (100 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace Queue\Swoole;
use ArrayAccess;
use Psr\Container\ContainerInterface;
use Queue\Swoole\Exception\InvalidArgumentException;
use Swoole\Runtime as SwooleRuntime;
use Swoole\Server as SwooleServer;
use Webmozart\Assert\Assert;
use function assert;
use function defined;
use function in_array;
use function is_array;
use const SWOOLE_BASE;
use const SWOOLE_PROCESS;
use const SWOOLE_SOCK_TCP;
use const SWOOLE_SOCK_TCP6;
use const SWOOLE_SOCK_UDP;
use const SWOOLE_SOCK_UDP6;
use const SWOOLE_SSL;
use const SWOOLE_UNIX_DGRAM;
use const SWOOLE_UNIX_STREAM;
class ServerFactory
{
/**
* @var string
*/
public const DEFAULT_HOST = '127.0.0.1';
/**
* @var int
*/
public const DEFAULT_PORT = 8080;
/**
* Swoole server supported modes
*
* @var int[]
*/
private const MODES = [
SWOOLE_BASE,
SWOOLE_PROCESS,
];
/**
* Swoole server supported protocols
*
* @var int[]
*/
private const PROTOCOLS = [
SWOOLE_SOCK_TCP,
SWOOLE_SOCK_TCP6,
SWOOLE_SOCK_UDP,
SWOOLE_SOCK_UDP6,
SWOOLE_UNIX_DGRAM,
SWOOLE_UNIX_STREAM,
];
/**
* @see https://www.swoole.co.uk/docs/modules/swoole-server-methods#swoole_server-__construct
* @see https://www.swoole.co.uk/docs/modules/swoole-server/predefined-constants for $mode and $protocol constant
*
* @throws InvalidArgumentException For invalid $port values.
* @throws InvalidArgumentException For invalid $mode values.
* @throws InvalidArgumentException For invalid $protocol values.
* @throws \ErrorException
*/
public function __invoke(ContainerInterface $container): SwooleServer
{
$config = $container->get('config');
assert(is_array($config) || $config instanceof ArrayAccess);
$swooleConfig = $config['dotkernel-queue-swoole'] ?? [];
Assert::isMap($swooleConfig);
$serverConfig = $swooleConfig['swoole-tcp-server'] ?? [];
Assert::isMap($serverConfig);
$host = $serverConfig['host'] ?? static::DEFAULT_HOST;
$port = $serverConfig['port'] ?? static::DEFAULT_PORT;
$mode = $serverConfig['mode'] ?? SWOOLE_BASE;
$protocol = $serverConfig['protocol'] ?? SWOOLE_SOCK_TCP;
if ($port < 1 || $port > 65535) {
throw new InvalidArgumentException('Invalid port');
}
if (! in_array($mode, self::MODES, true)) {
throw new InvalidArgumentException('Invalid server mode');
}
$validProtocols = self::PROTOCOLS;
if (defined('SWOOLE_SSL')) {
$validProtocols[] = SWOOLE_SOCK_TCP | SWOOLE_SSL;
$validProtocols[] = SWOOLE_SOCK_TCP6 | SWOOLE_SSL;
}
if (! in_array($protocol, $validProtocols, true)) {
throw new InvalidArgumentException('Invalid server protocol');
}
$enableCoroutine = $swooleConfig['enable_coroutine'] ?? false;
if ($enableCoroutine) {
SwooleRuntime::enableCoroutine(1);
}
$httpServer = new SwooleServer($host, $port, $mode, $protocol);
$serverOptions = $serverConfig['options'] ?? [];
Assert::isArray($serverOptions);
$httpServer->set($serverOptions);
return $httpServer;
}
}