Total Complexity | 61 |
Total Lines | 309 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 5 | Features | 2 |
Complex classes like Portal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Portal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Portal extends Command |
||
17 | { |
||
18 | use LogTrait; |
||
19 | |||
20 | |||
21 | /**@var string */ |
||
22 | protected $basePath; |
||
23 | |||
24 | /**@var InputInterface */ |
||
25 | protected $input; |
||
26 | |||
27 | /**@var OutputInterface */ |
||
28 | protected $output; |
||
29 | |||
30 | public function __construct($basePath) |
||
34 | } |
||
35 | |||
36 | protected function configure() |
||
46 | } |
||
47 | |||
48 | protected function execute(InputInterface $input, OutputInterface $output) |
||
99 | } |
||
100 | } |
||
101 | |||
102 | public function start() |
||
103 | { |
||
104 | if (!extension_loaded('swoole')) { |
||
105 | $this->error('LaravelS requires swoole extension, try to `pecl install swoole` and `php --ri swoole`.'); |
||
106 | return 1; |
||
107 | } |
||
108 | |||
109 | // Generate configuration storage/laravels.json |
||
110 | $options = array_filter($this->input->getOptions()); |
||
111 | if (isset($options['env'])) { |
||
112 | putenv('LARAVELS_ENV=' . $options['env']); |
||
113 | } |
||
114 | |||
115 | if (!empty($options['apollo'])) { |
||
116 | // Load Apollo configurations to .env file |
||
117 | $this->loadApollo(); |
||
118 | unset($options['apollo']); |
||
119 | } |
||
120 | unset($options['env']); // Pass env parameter in makeArtisanCmd() |
||
121 | |||
122 | $optionStr = ''; |
||
123 | foreach ($options as $key => $value) { |
||
124 | $optionStr .= sprintf('--%s%s ', $key, is_bool($value) ? '' : ('=' . $value)); |
||
125 | } |
||
126 | $statusCode = $this->runArtisanCommand(trim('laravels config ' . $optionStr)); |
||
127 | if ($statusCode !== 0) { |
||
128 | return $statusCode; |
||
129 | } |
||
130 | |||
131 | // Here we go... |
||
132 | $config = $this->getConfig(); |
||
133 | |||
134 | if (!$config['server']['ignore_check_pid'] && file_exists($config['server']['swoole']['pid_file'])) { |
||
135 | $pid = (int)file_get_contents($config['server']['swoole']['pid_file']); |
||
136 | if ($pid > 0 && self::kill($pid, 0)) { |
||
137 | $this->warning(sprintf('Swoole[PID=%d] is already running.', $pid)); |
||
138 | return 1; |
||
139 | } |
||
140 | } |
||
141 | |||
142 | if ($config['server']['swoole']['daemonize']) { |
||
143 | $this->trace('Swoole is running in daemon mode, see "ps -ef|grep laravels".'); |
||
144 | } else { |
||
145 | $this->trace('Swoole is running, press Ctrl+C to quit.'); |
||
146 | } |
||
147 | |||
148 | (new LaravelS($config['server'], $config['laravel']))->run(); |
||
149 | |||
150 | return 0; |
||
151 | } |
||
152 | |||
153 | public function stop() |
||
154 | { |
||
155 | $config = $this->getConfig(); |
||
156 | $pidFile = $config['server']['swoole']['pid_file']; |
||
157 | if (!file_exists($pidFile)) { |
||
158 | $this->warning('It seems that Swoole is not running.'); |
||
159 | return 0; |
||
160 | } |
||
161 | |||
162 | $pid = file_get_contents($pidFile); |
||
163 | if (self::kill($pid, 0)) { |
||
164 | if (self::kill($pid, SIGTERM)) { |
||
165 | // Make sure that master process quit |
||
166 | $time = 1; |
||
167 | $waitTime = isset($config['server']['swoole']['max_wait_time']) ? $config['server']['swoole']['max_wait_time'] : 60; |
||
168 | $this->info("The max time of waiting to forcibly stop is {$waitTime}s."); |
||
169 | while (self::kill($pid, 0)) { |
||
170 | if ($time > $waitTime) { |
||
171 | $this->warning("Swoole [PID={$pid}] cannot be stopped gracefully in {$waitTime}s, will be stopped forced right now."); |
||
172 | return 1; |
||
173 | } |
||
174 | $this->info("Waiting Swoole[PID={$pid}] to stop. [{$time}]"); |
||
175 | sleep(1); |
||
176 | $time++; |
||
177 | } |
||
178 | $basePath = dirname($pidFile); |
||
179 | $deleteFiles = [ |
||
180 | $pidFile, |
||
181 | $basePath . '/laravels-custom-processes.pid', |
||
182 | $basePath . '/laravels-timer-process.pid', |
||
183 | ]; |
||
184 | foreach ($deleteFiles as $deleteFile) { |
||
185 | if (file_exists($deleteFile)) { |
||
186 | unlink($deleteFile); |
||
187 | } |
||
188 | } |
||
189 | $this->info("Swoole [PID={$pid}] is stopped."); |
||
190 | return 0; |
||
191 | } else { |
||
192 | $this->error("Swoole [PID={$pid}] is stopped failed."); |
||
193 | return 1; |
||
194 | } |
||
195 | } else { |
||
196 | $this->warning("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
197 | return 0; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | public function restart() |
||
202 | { |
||
203 | $code = $this->stop(); |
||
204 | if ($code !== 0) { |
||
205 | return $code; |
||
206 | } |
||
207 | return $this->start(); |
||
208 | } |
||
209 | |||
210 | public function reload() |
||
211 | { |
||
212 | $config = $this->getConfig(); |
||
213 | $pidFile = $config['server']['swoole']['pid_file']; |
||
214 | if (!file_exists($pidFile)) { |
||
215 | $this->error('It seems that Swoole is not running.'); |
||
216 | return 1; |
||
217 | } |
||
218 | |||
219 | // Reload worker processes |
||
220 | $pid = file_get_contents($pidFile); |
||
221 | if (!$pid || !self::kill($pid, 0)) { |
||
222 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
223 | return 1; |
||
224 | } |
||
225 | if (self::kill($pid, SIGUSR1)) { |
||
226 | $this->info("Swoole [PID={$pid}] is reloaded."); |
||
227 | } else { |
||
228 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
229 | } |
||
230 | |||
231 | // Reload custom processes |
||
232 | $pidFile = dirname($pidFile) . '/laravels-custom-processes.pid'; |
||
233 | if (file_exists($pidFile)) { |
||
234 | $pids = (array)explode("\n", trim(file_get_contents($pidFile))); |
||
235 | unlink($pidFile); |
||
236 | foreach ($pids as $pid) { |
||
237 | if (!$pid || !self::kill($pid, 0)) { |
||
238 | $this->error("Custom process[PID={$pid}] does not exist, or permission denied."); |
||
239 | continue; |
||
240 | } |
||
241 | |||
242 | if (self::kill($pid, SIGUSR1)) { |
||
243 | $this->info("Custom process[PID={$pid}] is reloaded."); |
||
244 | } else { |
||
245 | $this->error("Custom process[PID={$pid}] is reloaded failed."); |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // Reload timer process |
||
251 | if (!empty($config['server']['timer']['enable']) && !empty($config['server']['timer']['jobs'])) { |
||
252 | $pidFile = dirname($pidFile) . '/laravels-timer-process.pid'; |
||
253 | $pid = file_get_contents($pidFile); |
||
254 | if (!$pid || !self::kill($pid, 0)) { |
||
255 | $this->error("Timer process[PID={$pid}] does not exist, or permission denied."); |
||
256 | return 1; |
||
257 | } |
||
258 | |||
259 | if (self::kill($pid, SIGUSR1)) { |
||
260 | $this->info("Timer process[PID={$pid}] is reloaded."); |
||
261 | } else { |
||
262 | $this->error("Timer process[PID={$pid}] is reloaded failed."); |
||
263 | } |
||
264 | } |
||
265 | return 0; |
||
266 | } |
||
267 | |||
268 | public function showInfo() |
||
269 | { |
||
270 | return $this->runArtisanCommand('laravels info'); |
||
271 | } |
||
272 | |||
273 | public function loadApollo() |
||
274 | { |
||
275 | $envFile = $this->basePath . '/.env'; |
||
276 | $env = getenv('LARAVELS_ENV'); |
||
277 | if ($env) { |
||
278 | $envFile .= '.' . $env; |
||
279 | } |
||
280 | $keepOld = isset($envs['APOLLO_KEEP_OLD']) ? $envs['APOLLO_KEEP_OLD'] : false; |
||
|
|||
281 | Apollo::createFromEnv()->pullAllAndSave($envFile, $keepOld); |
||
282 | } |
||
283 | |||
284 | public function makeArtisanCmd($subCmd) |
||
285 | { |
||
286 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, php_ini_loaded_file()); |
||
287 | $env = $this->input->getOption('env'); |
||
288 | $envs = $env ? "APP_ENV={$env}" : ''; |
||
289 | return trim(sprintf('%s %s %s/artisan %s', $envs, $phpCmd, $this->basePath, $subCmd)); |
||
290 | } |
||
291 | |||
292 | public function runArtisanCommand($cmd) |
||
293 | { |
||
294 | $cmd = $this->makeArtisanCmd($cmd); |
||
295 | return self::runCommand($cmd); |
||
296 | } |
||
297 | |||
298 | public function getConfig() |
||
299 | { |
||
300 | $json = file_get_contents($this->basePath . '/storage/laravels.json'); |
||
301 | return (array)json_decode($json, true); |
||
302 | } |
||
303 | |||
304 | public static function runCommand($cmd, $input = null) |
||
305 | { |
||
306 | $fp = popen($cmd, 'w'); |
||
307 | if ($fp === false) { |
||
308 | return false; |
||
309 | } |
||
310 | if ($input !== null) { |
||
311 | $bytes = fwrite($fp, $input); |
||
312 | if ($bytes === false) { |
||
313 | return 1; |
||
314 | } |
||
315 | } |
||
316 | return pclose($fp); |
||
317 | } |
||
318 | |||
319 | public static function kill($pid, $sig) |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 |