SelfChecker.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of SelfChecker
  5. * 框架的自我检查工具
  6. * @author gwang
  7. */
  8. class SelfChecker {
  9. public static function CheckAll() {
  10. self:: checkkPHPVersion();
  11. self::checkModules();
  12. self::CheckDNS();
  13. self::CheckNetRead();
  14. self::CheckConfig();
  15. }
  16. /**
  17. * 检查模块是否加载
  18. * @param string[] $arr 要检查的模块名称
  19. */
  20. public static function checkModules($arr = array()) {
  21. $default = array(# # 必备基础模块
  22. "curl", # # 服务器间http请求
  23. "mbstring", # # 设计到utf-8多语言支持
  24. "mongodb", # # mongodb支持
  25. "openssl", # # 用于通讯时的加密、验签;
  26. "pdo_mysql", # # mysql
  27. // "shmop", # # 九游SDK代码中用到,共享内存操作,还没怎么玩儿过
  28. "sockets", # # 用于服务器间通讯
  29. "zlib", # # 压缩库
  30. );
  31. $r_modules = array_merge($default, $arr); # 合并指定模块
  32. $diff = array_diff($r_modules, get_loaded_extensions()); # 判断缺失模块
  33. if (count($diff)) { # 确实缺失模块
  34. die("require modules: " . implode(' ,', $diff)); # 直接结束运行
  35. }
  36. }
  37. /**
  38. * 检查PHP版本
  39. * @param string $ver 最低版本字符串类似于7.3.5
  40. */
  41. public static function checkkPHPVersion($ver = null) {
  42. $default_ver = "5.4.16";
  43. $r_ver = isset($ver) ? $ver : $default_ver;
  44. if (version_compare(PHP_VERSION, $r_ver, '<')) {
  45. die('<font color="red">This code request at least PHP version ' . $r_ver #
  46. . ', Current version: ' . PHP_VERSION . ".</font><br/>" . PHP_EOL);
  47. }
  48. }
  49. /**
  50. * 检查配置文件
  51. */
  52. public static function CheckConfig() {
  53. echoLine("检查配置:");
  54. echoLine("GameOnline: " . GAME_ONLINE);
  55. echoLine("MySQL Connect Settings; " . var_export(config::Inst()->paydb, true));
  56. self::TestMysql_RW();
  57. echoLine("NoSql(Redis) Connect Settings: " . var_export(config::Inst()->nosql, true));
  58. self::TestNoSQL_RW();
  59. echoLine("Upload path Settings: " . "本框架暂未涉及.");
  60. }
  61. /**
  62. * DNS检查
  63. * @param type $url
  64. */
  65. public static function CheckDNS($url = 'qq.com') {
  66. echoLine("DNS check $url is " . (checkdnsrr($url, "A") ? "ok!" : "failed!"));
  67. }
  68. public static function CheckNetRead($url = 'qq.com') {
  69. $ret = HttpUtil::makeRequest($url, array());
  70. if ($ret["result"]) {
  71. echoLine("Request to $url ok!");
  72. } else {
  73. echoLine("Net Request Failed!:" . $ret['msg']);
  74. }
  75. }
  76. // <editor-fold defaultstate="collapsed" desc=" 内部函数 ">
  77. /**
  78. * 测试NoSql连接,以及读写性能
  79. */
  80. private static function TestNoSQL_RW() {
  81. $key = "test-" . TimeUtil::tsYmdHis();
  82. echoLine("Redis Server is running: " . gMem()->ping());
  83. echoLine("Redis Write($key, 'Hello world!'):" . gMem()->set($key, "Hello world!"));
  84. echoLine("Redis Read($key): " . gMem()->get($key));
  85. echo("Profile Redis set/get: ");
  86. RenderTime::Test(function ()use ($key) {
  87. gMem()->set($key, "Hello world!");
  88. gMem()->get($key);
  89. }, 100);
  90. }
  91. /**
  92. * 测试MySQL连接,以及读写性能
  93. */
  94. private static function TestMysql_RW() {
  95. $tableName = "test-" . date('Ymd');
  96. echoLine("Connection is " . (daoInst()->Ping() ? "ok!" : "gone!"));
  97. $bCretaTable = daoInst()->tableExist($tableName);
  98. if ($bCretaTable) {
  99. echoLine("Table $tableName is Existing.");
  100. } else {
  101. echoLine("Table $tableName is not Existing.");
  102. $sql = " CREATE TABLE if NOT exists `$tableName` (`row` INT(11) NOT NULL AUTO_INCREMENT COMMENT '自增行号',`msg` TEXT NULL DEFAULT NULL COMMENT '消息内容' COLLATE 'utf8_general_ci', PRIMARY KEY (`row`) USING BTREE) COLLATE='utf8_general_ci' ENGINE=INNODB ; ";
  103. daoInst()->exec($sql);
  104. echoLine("create table $tableName " . (daoInst()->tableExist($tableName) ? "success!" : "failed!"));
  105. }
  106. $row = daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  107. echoLine("Insert into `$tableName` " . ($row == 1 ? "ok!" : "failed!"));
  108. echo("Profile Insert: ");
  109. RenderTime::Test(function ()use ($tableName) {
  110. daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  111. }, 100);
  112. $sql = "Drop Table `$tableName`;";
  113. daoInst()->exec($sql);
  114. echoLine("delete table $tableName " . (daoInst()->tableExist($tableName) ? "failed!" : "success!"));
  115. }
  116. // </editor-fold>
  117. }
  118. SelfChecker::checkkPHPVersion(); # 检查PHP版本
  119. SelfChecker::checkModules(); # 检查PHP模块