Conditions | 97 |
Paths | > 20000 |
Total Lines | 334 |
Code Lines | 220 |
Lines | 59 |
Ratio | 17.66 % |
Changes | 5 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
101 | public static function init($configFile = null) |
||
|
|||
102 | { |
||
103 | if (!version_compare(PHP_VERSION, '5.6.0', '>=')) { |
||
104 | Daemon::log('PHP >= 5.6.0 required.'); |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | //run without composer |
||
109 | if (!function_exists('setTimeout')) { |
||
110 | require 'PHPDaemon/Utils/func.php'; |
||
111 | } |
||
112 | |||
113 | Daemon::initSettings(); |
||
114 | FileSystem::init(); |
||
115 | Daemon::$runName = basename($_SERVER['argv'][0]); |
||
116 | |||
117 | $error = false; |
||
118 | $argv = $_SERVER['argv']; |
||
119 | $runmode = isset($argv[1]) ? str_replace('-', '', $argv[1]) : ''; |
||
120 | $args = Bootstrap::getArgs($argv); |
||
121 | |||
122 | if (!isset(self::$params[$runmode]) && !in_array($runmode, self::$commands)) { |
||
123 | if ('' !== $runmode) { |
||
124 | echo('Unrecognized command: ' . $runmode . "\n"); |
||
125 | } |
||
126 | |||
127 | self::printUsage(); |
||
128 | exit; |
||
129 | } elseif ('help' === $runmode) { |
||
130 | self::printHelp(); |
||
131 | exit; |
||
132 | } |
||
133 | |||
134 | $n = null; |
||
135 | if ('log' === $runmode) { |
||
136 | if (isset($args['n'])) { |
||
137 | $n = $args['n']; |
||
138 | unset($args['n']); |
||
139 | } else { |
||
140 | $n = 20; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | if (isset($configFile)) { |
||
145 | Daemon::$config->configfile->setHumanValue($configFile); |
||
146 | } |
||
147 | if (isset($args['configfile'])) { |
||
148 | Daemon::$config->configfile->setHumanValue($args['configfile']); |
||
149 | } |
||
150 | |||
151 | if (!Daemon::$config->loadCmdLineArgs($args)) { |
||
152 | $error = true; |
||
153 | } |
||
154 | |||
155 | if (!Daemon::loadConfig(Daemon::$config->configfile->value)) { |
||
156 | $error = true; |
||
157 | } |
||
158 | |||
159 | if ('log' === $runmode) { |
||
160 | passthru('tail -n ' . $n . ' -f ' . escapeshellarg(Daemon::$config->logstorage->value)); |
||
161 | exit; |
||
162 | } |
||
163 | |||
164 | if (extension_loaded('apc') && ini_get('apc.enabled')) { |
||
165 | Daemon::log('Detected pecl-apc extension enabled. Usage of bytecode caching (APC/eAccelerator/xcache/...) makes no sense at all in case of using phpDaemon \'cause phpDaemon includes files just in time itself.'); |
||
166 | } |
||
167 | |||
168 | if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') { |
||
169 | setlocale(LC_ALL, array_map('trim', explode(',', Daemon::$config->locale->value))); |
||
170 | } |
||
171 | |||
172 | if (Daemon::$config->autoreimport->value && !is_callable('runkit_import')) { |
||
173 | Daemon::log('[WARN] runkit extension not found. You should install it or disable --auto-reimport. Non-critical error.'); |
||
174 | } |
||
175 | |||
176 | if (!is_callable('posix_kill')) { |
||
177 | Daemon::log('[EMERG] Posix not found. You should compile PHP without \'--disable-posix\'.'); |
||
178 | $error = true; |
||
179 | } |
||
180 | |||
181 | if (!is_callable('pcntl_signal')) { |
||
182 | Daemon::log('[EMERG] PCNTL not found. You should compile PHP with \'--enable-pcntl\'.'); |
||
183 | $error = true; |
||
184 | } |
||
185 | |||
186 | if (extension_loaded('libevent')) { |
||
187 | Daemon::log('[EMERG] libevent extension found. You have to remove libevent.so extension.'); |
||
188 | $error = true; |
||
189 | } |
||
190 | |||
191 | $eventVer = '1.6.1'; |
||
192 | $eventVerType = 'stable'; |
||
193 | if (!Daemon::loadModuleIfAbsent('event', $eventVer . '-' . $eventVerType)) { |
||
194 | Daemon::log('[EMERG] event extension >= ' . $eventVer . ' not found (or OUTDATED). You have to install it. `pecl install http://pecl.php.net/get/event`'); |
||
195 | $error = true; |
||
196 | } |
||
197 | |||
198 | if (!is_callable('socket_create')) { |
||
199 | Daemon::log('[EMERG] Sockets extension not found. You should compile PHP with \'--enable-sockets\'.'); |
||
200 | $error = true; |
||
201 | } |
||
202 | |||
203 | if (!is_callable('shmop_open')) { |
||
204 | Daemon::log('[EMERG] Shmop extension not found. You should compile PHP with \'--enable-shmop\'.'); |
||
205 | $error = true; |
||
206 | } |
||
207 | |||
208 | if (!isset(Daemon::$config->user)) { |
||
209 | Daemon::log('[EMERG] You must set \'user\' parameter.'); |
||
210 | $error = true; |
||
211 | } |
||
212 | |||
213 | if (!isset(Daemon::$config->path)) { |
||
214 | Daemon::log('[EMERG] You must set \'path\' parameter (path to your application resolver).'); |
||
215 | $error = true; |
||
216 | } |
||
217 | |||
218 | if (!file_exists(Daemon::$config->pidfile->value)) { |
||
219 | View Code Duplication | if (!touch(Daemon::$config->pidfile->value)) { |
|
220 | Daemon::log('[EMERG] Couldn\'t create pid-file \'' . Daemon::$config->pidfile->value . '\'.'); |
||
221 | $error = true; |
||
222 | } |
||
223 | |||
224 | Bootstrap::$pid = 0; |
||
225 | View Code Duplication | } elseif (!is_file(Daemon::$config->pidfile->value)) { |
|
226 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be a regular file.'); |
||
227 | Bootstrap::$pid = false; |
||
228 | $error = true; |
||
229 | } elseif (!is_writable(Daemon::$config->pidfile->value)) { |
||
230 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be writable.'); |
||
231 | $error = true; |
||
232 | View Code Duplication | } elseif (!is_readable(Daemon::$config->pidfile->value)) { |
|
233 | Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be readable.'); |
||
234 | Bootstrap::$pid = false; |
||
235 | $error = true; |
||
236 | } else { |
||
237 | Bootstrap::$pid = (int)file_get_contents(Daemon::$config->pidfile->value); |
||
238 | } |
||
239 | |||
240 | if (Daemon::$config->chroot->value !== '/') { |
||
241 | if (posix_getuid() != 0) { |
||
242 | Daemon::log('You must have the root privileges to change root.'); |
||
243 | $error = true; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | $pathList = preg_split('~\s*;\s*~', Daemon::$config->path->value); |
||
248 | $found = false; |
||
249 | foreach ($pathList as $path) { |
||
250 | if (@is_file($path)) { |
||
251 | Daemon::$appResolverPath = $path; |
||
252 | $found = true; |
||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | if (!$found) { |
||
257 | Daemon::log('Your application resolver \'' . Daemon::$config->path->value . '\' is not available (config directive \'path\').'); |
||
258 | $error = true; |
||
259 | } |
||
260 | |||
261 | Daemon::$appResolver = require Daemon::$appResolverPath; |
||
262 | |||
263 | View Code Duplication | if (isset(Daemon::$config->group->value) && is_callable('posix_getgid')) { |
|
264 | if (($sg = posix_getgrnam(Daemon::$config->group->value)) === false) { |
||
265 | Daemon::log('Unexisting group \'' . Daemon::$config->group->value . '\'. You have to replace config-variable \'group\' with existing group-name.'); |
||
266 | $error = true; |
||
267 | } elseif (($sg['gid'] != posix_getgid()) && (posix_getuid() != 0)) { |
||
268 | Daemon::log('You must have the root privileges to change group.'); |
||
269 | $error = true; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | View Code Duplication | if (isset(Daemon::$config->user->value) && is_callable('posix_getuid')) { |
|
274 | if (($su = posix_getpwnam(Daemon::$config->user->value)) === false) { |
||
275 | Daemon::log('Unexisting user \'' . Daemon::$config->user->value . '\', user not found. You have to replace config-variable \'user\' with existing username.'); |
||
276 | $error = true; |
||
277 | } elseif (($su['uid'] != posix_getuid()) && (posix_getuid() != 0)) { |
||
278 | Daemon::log('You must have the root privileges to change user.'); |
||
279 | $error = true; |
||
280 | } |
||
281 | } |
||
282 | |||
283 | if (isset(Daemon::$config->minspareworkers->value) |
||
284 | && Daemon::$config->minspareworkers->value > 0 |
||
285 | && isset(Daemon::$config->maxspareworkers->value) |
||
286 | && Daemon::$config->maxspareworkers->value > 0 |
||
287 | ) { |
||
288 | if (Daemon::$config->minspareworkers->value > Daemon::$config->maxspareworkers->value) { |
||
289 | Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.'); |
||
290 | $error = true; |
||
291 | } |
||
292 | } |
||
293 | |||
294 | if (isset(Daemon::$config->addincludepath->value)) { |
||
295 | ini_set( |
||
296 | 'include_path', |
||
297 | ini_get('include_path') . ':' . implode(':', Daemon::$config->addincludepath->value) |
||
298 | ); |
||
299 | } |
||
300 | |||
301 | if (isset(Daemon::$config->minworkers->value) && isset(Daemon::$config->maxworkers->value)) { |
||
302 | if (Daemon::$config->minworkers->value > Daemon::$config->maxworkers->value) { |
||
303 | Daemon::$config->minworkers->value = Daemon::$config->maxworkers->value; |
||
304 | } |
||
305 | } |
||
306 | |||
307 | if ($runmode === 'start') { |
||
308 | if ($error === false) { |
||
309 | Bootstrap::start(); |
||
310 | } else { |
||
311 | exit(6); |
||
312 | } |
||
313 | } elseif ($runmode === 'runworker') { |
||
314 | if ($error === false) { |
||
315 | Bootstrap::runworker(); |
||
316 | } else { |
||
317 | exit(6); |
||
318 | } |
||
319 | } elseif ($runmode === 'status' || $runmode === 'fullstatus') { |
||
320 | $status = Bootstrap::$pid && Thread\Generic::ifExistsByPid(Bootstrap::$pid); |
||
321 | echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon::$config->pidfile->value . ").\n"; |
||
322 | |||
323 | if ($status && ($runmode === 'fullstatus')) { |
||
324 | echo 'Uptime: ' . DateTime::diffAsText(filemtime(Daemon::$config->pidfile->value), time()) . "\n"; |
||
325 | |||
326 | Daemon::$shm_wstate = new ShmEntity(Daemon::$config->pidfile->value, Daemon::SHM_WSTATE_SIZE, 'wstate'); |
||
327 | |||
328 | $stat = Daemon::getStateOfWorkers(); |
||
329 | |||
330 | echo "State of workers:\n"; |
||
331 | echo "\tTotal: " . $stat['alive'] . "\n"; |
||
332 | echo "\tIdle: " . $stat['idle'] . "\n"; |
||
333 | echo "\tBusy: " . $stat['busy'] . "\n"; |
||
334 | echo "\tShutdown: " . $stat['shutdown'] . "\n"; |
||
335 | echo "\tPre-init: " . $stat['preinit'] . "\n"; |
||
336 | echo "\tInit: " . $stat['init'] . "\n"; |
||
337 | } |
||
338 | |||
339 | echo "\n"; |
||
340 | View Code Duplication | } elseif ($runmode === 'update') { |
|
341 | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGHUP))) { |
||
342 | echo '[UPDATE] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
343 | } |
||
344 | } elseif ($runmode === 'reopenlog') { |
||
345 | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR1))) { |
||
346 | echo '[REOPEN-LOG] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
347 | } |
||
348 | View Code Duplication | } elseif ($runmode === 'reload') { |
|
349 | if ((!Bootstrap::$pid) || (!posix_kill(Bootstrap::$pid, SIGUSR2))) { |
||
350 | echo '[RELOAD] ERROR. It seems that phpDaemon is not running' . (Bootstrap::$pid ? ' (PID ' . Bootstrap::$pid . ')' : '') . ".\n"; |
||
351 | } |
||
352 | } elseif ($runmode === 'restart') { |
||
353 | if ($error === false) { |
||
354 | Bootstrap::stop(2); |
||
355 | Bootstrap::start(); |
||
356 | } |
||
357 | } elseif ($runmode === 'hardrestart') { |
||
358 | Bootstrap::stop(3); |
||
359 | Bootstrap::start(); |
||
360 | } elseif ($runmode === 'ipcpath') { |
||
361 | $i = Daemon::$appResolver->getInstanceByAppName('\PHPDaemon\IPCManager\IPCManager'); |
||
362 | echo $i->getSocketUrl() . PHP_EOL; |
||
363 | } elseif ($runmode === 'configtest') { |
||
364 | $term = new Terminal; |
||
365 | $term->enableColor(); |
||
366 | |||
367 | echo "\n"; |
||
368 | |||
369 | $rows = []; |
||
370 | |||
371 | $rows[] = [ |
||
372 | 'parameter' => 'PARAMETER', |
||
373 | 'value' => 'VALUE', |
||
374 | '_color' => '37', |
||
375 | '_bold' => true, |
||
376 | ]; |
||
377 | |||
378 | foreach (Daemon::$config as $name => $entry) { |
||
379 | if (!$entry instanceof Generic) { |
||
380 | continue; |
||
381 | } |
||
382 | |||
383 | $row = [ |
||
384 | 'parameter' => $name, |
||
385 | 'value' => var_export($entry->humanValue, true), |
||
386 | ]; |
||
387 | |||
388 | if ($entry->defaultValue != $entry->humanValue) { |
||
389 | $row['value'] .= ' (' . var_export($entry->defaultValue, true) . ')'; |
||
390 | } |
||
391 | |||
392 | $rows[] = $row; |
||
393 | } |
||
394 | |||
395 | $term->drawtable($rows); |
||
396 | |||
397 | echo "\n"; |
||
398 | } elseif ($runmode === 'stop') { |
||
399 | Bootstrap::stop(); |
||
400 | } elseif ($runmode === 'gracefulstop') { |
||
401 | Bootstrap::stop(4); |
||
402 | } elseif ($runmode === 'hardstop') { |
||
403 | echo '[HARDSTOP] Sending SIGINT to ' . Bootstrap::$pid . '... '; |
||
404 | |||
405 | $ok = Bootstrap::$pid && posix_kill(Bootstrap::$pid, SIGINT); |
||
406 | |||
407 | echo $ok ? 'OK.' : 'ERROR. It seems that phpDaemon is not running.'; |
||
408 | |||
409 | if ($ok) { |
||
410 | $i = 0; |
||
411 | |||
412 | while ($r = Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
||
413 | usleep(500000); |
||
414 | |||
415 | if ($i === 9) { |
||
416 | echo "\nphpDaemon master-process hasn't finished. Sending SIGKILL... "; |
||
417 | posix_kill(Bootstrap::$pid, SIGKILL); |
||
418 | sleep(0.2); |
||
419 | if (!Thread\Generic::ifExistsByPid(Bootstrap::$pid)) { |
||
420 | echo " Oh, his blood is on my hands :'("; |
||
421 | } else { |
||
422 | echo "ERROR: Process alive. Permissions?"; |
||
423 | } |
||
424 | |||
425 | break; |
||
426 | } |
||
427 | |||
428 | ++$i; |
||
429 | } |
||
430 | } |
||
431 | |||
432 | echo "\n"; |
||
433 | } |
||
434 | } |
||
435 | |||
567 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: