15:50
Класс APP для микрофреймворка — Лисья нора
§ Для кода
Стартовый код для полезных функции.function config($name, $default = null) { return app::config($name, $default); } class app { static $log_prefix; static $need_stop = 0; static $config = []; static $argv = []; static function init($argv) { self::$argv = $argv; self::reconfig(); // Подгрузка классов по требованию spl_autoload_register([self::class, "lazy"]); } static function lazy($class) { $name = basename(str_replace('\\', '/', $class)); $dirs = [ // Список подключений ]; foreach ($dirs as $fn) { if (file_exists($fn)) { include $fn; return; } } } // Перезагрузка конфигурационого файла static function reconfig() { clearstatcache(true); $files = [ __DIR__ . '/config.ini', __DIR__ . '/config.dev.ini', ]; // Требуется для раздельного тестирования на локальной машини и на проде foreach ($files as $file) if (file_exists($file)) foreach (parse_ini_file($file, true) as $n => $u) foreach ($u as $a => $b) self::$config[$n][$a] = $b; self::$config['database'] = ['connections' => self::$config['database'] ?? []]; } // Получение значений конфига static function config($cname, $default = null) { $config = self::$config; foreach (explode('.', $cname) as $item) { if (isset($config[$item])) $config = $config[$item]; else return $default; } return $config; } static function log($msg) { echo self::$log_prefix . '['.date('Y-m-d H:i:s')."] $msg\n"; } static function success($msg) { self::log("\e[1;32m$msg\e[0m"); } static function error($msg) { self::log("\e[1;31m$msg\e[0m"); } static function warn($msg) { self::log("\e[33m$msg\e[0m"); } static function info($msg) { self::log("\e[36m$msg\e[0m"); } // Аргументы запроса консоли static function argv($name, $def = null) { if (is_integer($name)) { return self::$argv[$name + 2] ?? $def; } else if (strlen($name) == 1) { return getopt($name); } else { foreach (self::$argv as $v) if (preg_match("~^--$name=(.+)$~", $v, $c)) return $c[1]; return $def; } } // Список процессов или процесса static function ps($filter = '') { $r = []; $ps = array_map(function ($e) { return preg_split('~\s+~', $e, 11); }, explode("\n", trim(`ps aux`))); $ps = array_filter($ps, function($e) { return !preg_match('~^(\[|/bin/sh|COMMAND|/sbin/|/lib/|php-fpm)~', $e[10]); }); foreach ($ps as $x) if ($filter === '' || preg_match('~'.$filter.'~', $x[10] ?? "")) $r[$x[1]] = $x[10]; return $r; } // Регистрация отлова вызова SIG-сигнала static function registerSIG() { pcntl_signal(SIGTERM, [app::class, "stopAll"]); pcntl_signal(SIGINT, [app::class, "stopAll"]); } // Остановка скрипта после получения SIG static function stopAll(int $signal) { self::$need_stop = 1; } // Проверка на STOP-сигнал static function stop($stopfile = false) { if (file_exists($stopfile)) { return 1; } else { pcntl_signal_dispatch(); return self::$need_stop; } } } app::init($argv);