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