1 | <?php |
||
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 | $application->add('server', $this->server); |
|
50 | |||
51 | 1 | $this->initListeners(); |
|
52 | 1 | $this->initProcesses(); |
|
53 | 1 | } |
|
54 | |||
55 | /** |
||
56 | * @return swoole_server |
||
57 | */ |
||
58 | public function getSwoole() |
||
59 | { |
||
60 | return $this->server->getSwoole(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return Swoole\Server |
||
65 | */ |
||
66 | 1 | public function bootstrap() |
|
67 | { |
||
68 | 1 | return $this->server->bootstrap(); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return $this |
||
73 | */ |
||
74 | 1 | public function initListeners() |
|
75 | { |
||
76 | 1 | $listeners = config()->get('server.listeners', []); |
|
77 | 1 | foreach ($listeners as $listener) { |
|
78 | 1 | $this->server->listen(new $listener['class']( |
|
79 | 1 | app()->getName().' ports', |
|
80 | 1 | $listener['host'], |
|
81 | 1 | isset($listener['options']) ? $listener['options'] : [] |
|
82 | 1 | )); |
|
83 | 1 | } |
|
84 | |||
85 | 1 | return $this; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return $this |
||
90 | */ |
||
91 | 1 | public function initProcesses() |
|
92 | { |
||
93 | 1 | $processes = config()->get('server.processes', []); |
|
94 | 1 | foreach ($processes as $process) { |
|
95 | 1 | $this->server->process(new $process(app()->getName().' process')); |
|
96 | 1 | } |
|
97 | |||
98 | 1 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return $this |
||
103 | */ |
||
104 | public function daemon() |
||
105 | { |
||
106 | $this->server->daemon(); |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return int |
||
113 | */ |
||
114 | public function start() |
||
115 | { |
||
116 | $this->bootstrap(); |
||
117 | |||
118 | return $this->server->start(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return int |
||
123 | */ |
||
124 | public function stop() |
||
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 = ['.']) |
||
162 | |||
163 | /** |
||
164 | * @param InputInterface $input |
||
165 | */ |
||
166 | public function run(InputInterface $input) |
||
194 | } |
||
195 |