function addNameSpace($name, $path) { if (!is_dir($path)) { throw new \Exception("{$path} is not real path."); } Swoole\Loader::setRootNS($name, $path); }
/** * 显示命令行指令 */ static function start($startFunction) { if (empty(self::$pidFile)) { throw new \Exception("require pidFile."); } $pid_file = self::$pidFile; if (is_file($pid_file)) { $server_pid = file_get_contents($pid_file); } else { $server_pid = 0; } if (!self::$optionKit) { Swoole\Loader::addNameSpace('GetOptionKit', LIBPATH . '/module/GetOptionKit/src/GetOptionKit'); self::$optionKit = new \GetOptionKit\GetOptionKit(); } $kit = self::$optionKit; foreach (self::$defaultOptions as $k => $v) { //解决Windows平台乱码问题 if (PHP_OS == 'WINNT') { $v = iconv('utf-8', 'gbk', $v); } $kit->add($k, $v); } global $argv; $opt = $kit->parse($argv); if (empty($argv[1]) or isset($opt['help'])) { goto usage; } elseif ($argv[1] == 'reload') { if (empty($server_pid)) { exit("Server is not running"); } if (self::$beforeReloadCallback) { call_user_func(self::$beforeReloadCallback, $opt); } \Swoole::$php->os->kill($server_pid, SIGUSR1); exit; } elseif ($argv[1] == 'stop') { if (empty($server_pid)) { exit("Server is not running\n"); } if (self::$beforeStopCallback) { call_user_func(self::$beforeStopCallback, $opt); } \Swoole::$php->os->kill($server_pid, SIGTERM); exit; } elseif ($argv[1] == 'start') { //已存在ServerPID,并且进程存在 if (!empty($server_pid) and \Swoole::$php->os->kill($server_pid, 0)) { exit("Server is already running.\n"); } } else { usage: $kit->specs->printOptions("php {$argv[0]} start|stop|reload"); exit; } self::$options = $opt; $startFunction($opt); }
/** * [onStart 协程调度器单例模式] * @param \swoole_server $server * @param $workerId */ public function onStart(\swoole_server $server, $workerId) { /* 设置只读模式的配置对象 */ $this->di = Loader::import(APP_CONF_PATH . '/service.php'); $this->app = Application::getInstance(); }