/** * 重新加载Handler * @param \swoole_server $server 要注入到handler中的当前进程的swoole_server对象 */ private function ReloadHandler(\swoole_server &$server) { $whiteList = C('HANDLER_WHITE_LIST'); if (!is_array($whiteList)) { $whiteList = false; } $blackList = C('HANDLER_BLACK_LIST'); if (!is_array($blackList)) { $blackList = []; //未设置则初始话一个默认的空数组 } //重载Common/Handler $handlerFiles = AllFile(DI_APP_COMMON_HANDLER_PATH, true, 'Handler.php'); DILoadHandler($handlerFiles, $whiteList, $blackList, '\\Common\\Handler', $server); //重载Server/Handler $handlerFiles = AllFile(DI_APP_SERVER_HANDLER_PATH, true, 'Handler.php'); DILoadHandler($handlerFiles, $whiteList, $blackList, "\\" . DI_SERVER_NAME . "\\Handler", $server); }
/** * 加载Handler文件 * @param string $handlerFile Handler的文件完整路径 * @param array $whiteList Handler的白名单 * @param array $blackList Handler的黑名单 * @param string $namespace Handler的命名空间 * @param \swoole_server $server 要注入到Handler中的当前进程的swoole_server对象 * @return boolean 加载是否成功 */ function DILoadHandler($handlerFile, $whiteList, $blackList, $namespace = '', \swoole_server &$server) { if (is_array($handlerFile)) { foreach ($handlerFile as $handlerFile) { DILoadHandler($handlerFile, $whiteList, $blackList, $namespace, $server); } } else { if (file_exists($handlerFile)) { $fileName = array_pop(explode('/', $handlerFile)); $sortClassName = str_ireplace('.php', '', $fileName); $className = $namespace . '\\' . $sortClassName; //黑白名单判定逻辑,黑名单优先。 if ($whiteList) { if (array_search($sortClassName, $whiteList) === false) { return; } } if (array_search($sortClassName, $blackList) !== false) { return; } require_cache($handlerFile); $DIHandlerClass = new \ReflectionClass("\\DIServer\\Handler"); try { $handlerClass = new \ReflectionClass($className); if ($handlerClass->isSubclassOf($DIHandlerClass)) { $handler = $handlerClass->newInstanceArgs(['server' => $server]); DIHandler($handler); } } catch (\ReflectionException $ex) { DILog("ReflectionException On {$handlerFile}:{$ex->getMessage()}"); } } else { return FALSE; } } }