1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see https://fastdlabs.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD; |
11
|
|
|
|
12
|
|
|
use FastD\Process\Queue; |
13
|
|
|
use FastD\ServiceProvider\SwooleServiceProvider; |
14
|
|
|
use FastD\Servitization\Server\HTTPServer; |
15
|
|
|
use swoole_server; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class App. |
20
|
|
|
*/ |
21
|
|
|
class Server |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Application |
25
|
|
|
*/ |
26
|
|
|
protected $application; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \FastD\Swoole\Server |
30
|
|
|
*/ |
31
|
|
|
protected $server; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Server constructor. |
35
|
|
|
* |
36
|
|
|
* @param Application $application |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct(Application $application) |
39
|
|
|
{ |
40
|
1 |
|
$application->register(new SwooleServiceProvider()); |
41
|
|
|
|
42
|
1 |
|
$server = config()->get('server.class', HTTPServer::class); |
43
|
|
|
|
44
|
1 |
|
$this->server = $server::createServer( |
45
|
1 |
|
$application->getName(), |
46
|
1 |
|
config()->get('server.host'), |
47
|
1 |
|
config()->get('server.options', []) |
48
|
1 |
|
); |
49
|
|
|
|
50
|
1 |
|
$application->add('server', $this->server); |
51
|
|
|
|
52
|
1 |
|
$this->initListeners(); |
53
|
1 |
|
$this->initProcesses(); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return swoole_server |
58
|
|
|
*/ |
59
|
|
|
public function getSwoole() |
60
|
|
|
{ |
61
|
|
|
return $this->server->getSwoole(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return Swoole\Server |
66
|
|
|
*/ |
67
|
1 |
|
public function bootstrap() |
68
|
|
|
{ |
69
|
1 |
|
return $this->server->bootstrap(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
1 |
|
public function initListeners() |
76
|
|
|
{ |
77
|
1 |
|
$listeners = config()->get('server.listeners', []); |
78
|
1 |
|
foreach ($listeners as $listener) { |
79
|
1 |
|
$this->server->listen(new $listener['class']( |
80
|
1 |
|
app()->getName().' ports', |
81
|
1 |
|
$listener['host'], |
82
|
1 |
|
isset($listener['options']) ? $listener['options'] : [] |
83
|
1 |
|
)); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
1 |
|
public function initProcesses() |
93
|
|
|
{ |
94
|
1 |
|
$processes = config()->get('server.processes', []); |
95
|
1 |
|
foreach ($processes as $process) { |
96
|
1 |
|
$this->server->process(new $process(app()->getName().' process')); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function daemon() |
106
|
|
|
{ |
107
|
|
|
$this->server->daemon(); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function useQueue() |
116
|
|
|
{ |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function start() |
123
|
|
|
{ |
124
|
|
|
$this->bootstrap(); |
125
|
|
|
|
126
|
|
|
return $this->server->start(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
|
|
public function stop() |
133
|
|
|
{ |
134
|
|
|
return $this->server->shutdown(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function restart() |
141
|
|
|
{ |
142
|
|
|
return $this->server->restart(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return int |
147
|
|
|
*/ |
148
|
|
|
public function reload() |
149
|
|
|
{ |
150
|
|
|
return $this->server->reload(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
|
|
public function status() |
157
|
|
|
{ |
158
|
|
|
return $this->server->status(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $dir |
163
|
|
|
* |
164
|
|
|
* @return int |
165
|
|
|
*/ |
166
|
|
|
public function watch(array $dir = ['.']) |
167
|
|
|
{ |
168
|
|
|
return $this->server->watch($dir); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param InputInterface $input |
173
|
|
|
*/ |
174
|
|
|
public function run(InputInterface $input) |
175
|
|
|
{ |
176
|
|
|
if ($input->hasParameterOption(['--daemon', '-d'], true)) { |
177
|
|
|
$this->daemon(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($input->hasParameterOption(['--queue', '-q'], true)) { |
181
|
|
|
$this->useQueue(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
switch ($input->getArgument('action')) { |
185
|
|
|
case 'start': |
186
|
|
|
if ($input->hasParameterOption(['--dir'])) { |
187
|
|
|
$this->watch([$input->getOption('dir')]); |
188
|
|
|
} else { |
189
|
|
|
$this->start(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
break; |
193
|
|
|
case 'stop': |
194
|
|
|
$this->stop(); |
195
|
|
|
|
196
|
|
|
break; |
197
|
|
|
case 'reload': |
198
|
|
|
$this->reload(); |
199
|
|
|
|
200
|
|
|
break; |
201
|
|
|
case 'status': |
202
|
|
|
default: |
203
|
|
|
$this->status(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|