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