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) |
|
52 | |||
53 | /** |
||
54 | * @return swoole_server |
||
55 | */ |
||
56 | public function getSwoole() |
||
60 | |||
61 | /** |
||
62 | * @return Swoole\Server |
||
63 | */ |
||
64 | 1 | public function bootstrap() |
|
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() |
||
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) |
||
167 | { |
||
168 | if ($input->hasParameterOption(['--daemon', '-d'], true)) { |
||
169 | $this->daemon(); |
||
191 | } |
||
192 |