Total Complexity | 42 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
15 | class Portal extends Command |
||
16 | { |
||
17 | use LogTrait; |
||
18 | |||
19 | protected $basePath; |
||
20 | |||
21 | /** |
||
22 | * @var InputInterface $input |
||
23 | */ |
||
24 | protected $input; |
||
25 | |||
26 | /** |
||
27 | * @var OutputInterface $output |
||
28 | */ |
||
29 | protected $output; |
||
30 | |||
31 | public function __construct($basePath) |
||
35 | } |
||
36 | |||
37 | protected function configure() |
||
46 | } |
||
47 | |||
48 | protected function execute(InputInterface $input, OutputInterface $output) |
||
102 | } |
||
103 | } |
||
104 | |||
105 | public function start() |
||
106 | { |
||
107 | // Initialize configuration config/laravels.json |
||
108 | $options = $this->input->getOptions(); |
||
109 | unset($options['env']); |
||
110 | $options = array_filter($options); |
||
111 | $optionStr = ''; |
||
112 | foreach ($options as $key => $value) { |
||
113 | $optionStr .= sprintf('--%s%s ', $key, is_bool($value) ? '' : ('=' . $value)); |
||
114 | } |
||
115 | $cmd = trim('laravels config ' . $optionStr); |
||
116 | $this->runArtisanCommand($cmd); |
||
117 | |||
118 | $this->showInfo(); |
||
119 | |||
120 | // Here we go... |
||
121 | $config = $this->getConfig(); |
||
122 | |||
123 | if (!$config['server']['ignore_check_pid'] && file_exists($config['server']['swoole']['pid_file'])) { |
||
124 | $pid = (int)file_get_contents($config['server']['swoole']['pid_file']); |
||
125 | if ($pid > 0 && self::kill($pid, 0)) { |
||
126 | $this->warning(sprintf('Swoole[PID=%d] is already running.', $pid)); |
||
127 | return 1; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | if ($config['server']['swoole']['daemonize']) { |
||
132 | $this->trace('Swoole is running in daemon mode, see "ps -ef|grep laravels".'); |
||
133 | } else { |
||
134 | $this->trace('Swoole is running, press Ctrl+C to quit.'); |
||
135 | } |
||
136 | |||
137 | (new LaravelS($config['server'], $config['laravel']))->run(); |
||
138 | |||
139 | return 0; |
||
140 | } |
||
141 | |||
142 | public function stop() |
||
143 | { |
||
144 | $config = $this->getConfig(); |
||
145 | $pidFile = $config['server']['swoole']['pid_file']; |
||
146 | if (!file_exists($pidFile)) { |
||
147 | $this->warning('It seems that Swoole is not running.'); |
||
148 | return 0; |
||
149 | } |
||
150 | |||
151 | $pid = file_get_contents($pidFile); |
||
152 | if (self::kill($pid, 0)) { |
||
153 | if (self::kill($pid, SIGTERM)) { |
||
154 | // Make sure that master process quit |
||
155 | $time = 1; |
||
156 | $waitTime = isset($config['server']['swoole']['max_wait_time']) ? $config['server']['swoole']['max_wait_time'] : 60; |
||
157 | $this->info("The max time of waiting to forcibly stop is {$waitTime}s."); |
||
158 | while (self::kill($pid, 0)) { |
||
159 | if ($time > $waitTime) { |
||
160 | $this->warning("Swoole [PID={$pid}] cannot be stopped gracefully in {$waitTime}s, will be stopped forced right now."); |
||
161 | return 1; |
||
162 | } |
||
163 | $this->info("Waiting Swoole[PID={$pid}] to stop. [{$time}]"); |
||
164 | sleep(1); |
||
165 | $time++; |
||
166 | } |
||
167 | if (file_exists($pidFile)) { |
||
168 | unlink($pidFile); |
||
169 | } |
||
170 | $this->info("Swoole [PID={$pid}] is stopped."); |
||
171 | return 0; |
||
172 | } else { |
||
173 | $this->error("Swoole [PID={$pid}] is stopped failed."); |
||
174 | return 1; |
||
175 | } |
||
176 | } else { |
||
177 | $this->warning("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
178 | return 0; |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public function restart() |
||
183 | { |
||
184 | $code = $this->stop(); |
||
185 | if ($code !== 0) { |
||
186 | return $code; |
||
187 | } |
||
188 | return $this->start(); |
||
189 | } |
||
190 | |||
191 | public function reload() |
||
192 | { |
||
193 | $config = $this->getConfig(); |
||
194 | $pidFile = $config['server']['swoole']['pid_file']; |
||
195 | if (!file_exists($pidFile)) { |
||
196 | $this->error('It seems that Swoole is not running.'); |
||
197 | return; |
||
198 | } |
||
199 | |||
200 | $pid = file_get_contents($pidFile); |
||
201 | if (!$pid || !self::kill($pid, 0)) { |
||
202 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | if (self::kill($pid, SIGUSR1)) { |
||
207 | $now = date('Y-m-d H:i:s'); |
||
208 | $this->info("Swoole [PID={$pid}] is reloaded at {$now}."); |
||
209 | } else { |
||
210 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | public function showInfo() |
||
215 | { |
||
216 | $this->runArtisanCommand('laravels info'); |
||
217 | } |
||
218 | |||
219 | public function artisanCmd($subCmd) |
||
220 | { |
||
221 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, php_ini_loaded_file()); |
||
222 | $env = $this->input->getOption('env'); |
||
223 | $envs = $env ? "APP_ENV={$env}" : ''; |
||
224 | $artisanCmd = trim(sprintf('%s %s %s/artisan %s', $envs, $phpCmd, $this->basePath, $subCmd)); |
||
225 | return $artisanCmd; |
||
226 | } |
||
227 | |||
228 | public function runArtisanCommand($cmd) |
||
229 | { |
||
230 | $cmd = $this->artisanCmd($cmd); |
||
231 | self::runCommand($cmd); |
||
232 | } |
||
233 | |||
234 | public function getConfig() |
||
235 | { |
||
236 | $json = file_get_contents($this->basePath . '/storage/laravels.json'); |
||
237 | return (array)json_decode($json, true); |
||
238 | } |
||
239 | |||
240 | public static function runCommand($cmd, $input = null) |
||
241 | { |
||
242 | $fp = popen($cmd, 'w'); |
||
243 | if ($fp === false) { |
||
244 | return false; |
||
245 | } |
||
246 | if ($input !== null) { |
||
247 | fwrite($fp, $input); |
||
248 | } |
||
249 | pclose($fp); |
||
250 | return true; |
||
251 | } |
||
252 | |||
253 | public static function kill($pid, $sig) |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 |