SelfChecker.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 最低版本字符串类似于8.0.0
  40. */
  41. public static function checkkPHPVersion($ver = null) {
  42. $default_ver = "8.0.0";
  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: " . PLAT);
  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("MongoDB参数: " . var_export(config::Inst()->mongo, true));
  60. self::TestMongo_RW();
  61. echoLine("Upload path Settings: " . "本框架暂未涉及.");
  62. }
  63. /**
  64. * DNS检查
  65. * @param type $url
  66. */
  67. public static function CheckDNS($url = 'qq.com') {
  68. echoLine("DNS check $url is " . (checkdnsrr($url, "A") ? "ok!" : "failed!"));
  69. }
  70. public static function CheckNetRead($url = 'qq.com') {
  71. $ret = HttpUtil::makeRequest($url, array());
  72. if ($ret["result"]) {
  73. echoLine("Request to $url ok!");
  74. } else {
  75. echoLine("Net Request Failed!:" . $ret['msg']);
  76. }
  77. }
  78. // <editor-fold defaultstate="collapsed" desc=" 内部函数 ">
  79. /**
  80. * 测试NoSql连接,以及读写性能
  81. */
  82. private static function TestNoSQL_RW() {
  83. $key = "test-" . TimeUtil::tsYmdHis();
  84. echoLine("Redis Server is running: " . gMem()->ping());
  85. echoLine("Redis Write($key, 'Hello world!'):" . gMem()->set($key, "Hello world!"));
  86. echoLine("Redis Read($key): " . gMem()->get($key));
  87. echo("Profile Redis set/get: ");
  88. RenderTime::Test(function ()use ($key) {
  89. gMem()->set($key, "Hello world!");
  90. gMem()->get($key);
  91. }, 100);
  92. }
  93. private static function TestMongo_RW() {
  94. $CName = "T_T_Collection";
  95. $mdb = gMongo();
  96. $addArr = ['Name' => "王刚", 'Date' => TimeUtil::dtCurrent()];
  97. gMongo()->insert($CName, $addArr);
  98. $corsor = gMongo()->find($CName, ['Name' => "王刚"]);
  99. if ($corsor->valid()) {
  100. var_dump($corsor->toArray());
  101. // ok
  102. gMongo()->delete($CName, ['Name' => "王刚"]);
  103. } else {
  104. return false;
  105. }
  106. }
  107. /**
  108. * 测试MySQL连接,以及读写性能
  109. */
  110. private static function TestMysql_RW() {
  111. $tableName = "test-" . date('Ymd');
  112. echoLine("Connection is " . (daoInst()->Ping() ? "ok!" : "gone!"));
  113. $bCretaTable = daoInst()->tableExist($tableName);
  114. if ($bCretaTable) {
  115. echoLine("Table $tableName is Existing.");
  116. } else {
  117. echoLine("Table $tableName is not Existing.");
  118. $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 ; ";
  119. daoInst()->exec($sql);
  120. echoLine("create table $tableName " . (daoInst()->tableExist($tableName) ? "success!" : "failed!"));
  121. }
  122. $row = daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  123. echoLine("Insert into `$tableName` " . ($row == 1 ? "ok!" : "failed!"));
  124. echo("Profile Insert: ");
  125. RenderTime::Test(function ()use ($tableName) {
  126. daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  127. }, 100);
  128. $sql = "Drop Table `$tableName`;";
  129. daoInst()->exec($sql);
  130. echoLine("delete table $tableName " . (daoInst()->tableExist($tableName) ? "failed!" : "success!"));
  131. }
  132. // </editor-fold>
  133. }
  134. SelfChecker::checkkPHPVersion(); # 检查PHP版本
  135. SelfChecker::checkModules(); # 检查PHP模块