SelfChecker.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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",
  23. "mbstring",
  24. "openssl",
  25. "pdo_mysql",
  26. "sockets",
  27. "zlib",
  28. );
  29. $r_modules = array_merge($default, $arr); # 合并指定模块
  30. $diff = array_diff($r_modules, get_loaded_extensions()); # 判断缺失模块
  31. if (count($diff)) { # 确实缺失模块
  32. die("require modules: " . implode(' ,', $diff)); # 直接结束运行
  33. }
  34. }
  35. /**
  36. * 检查PHP版本
  37. * @param string $ver 最低版本字符串类似于7.3.5
  38. */
  39. public static function checkkPHPVersion($ver = null) {
  40. $default_ver = "5.4.16";
  41. $r_ver = isset($ver) ? $ver : $default_ver;
  42. if (version_compare(PHP_VERSION, $r_ver, '<')) {
  43. die('<font color="red">This code request at least PHP version ' . $r_ver #
  44. . ', Current version: ' . PHP_VERSION . ".</font><br/>" . PHP_EOL);
  45. }
  46. }
  47. /**
  48. * 检查配置文件
  49. */
  50. public static function CheckConfig() {
  51. echoLine("检查配置:");
  52. echoLine("GameOnline: " . GAME_ONLINE);
  53. echoLine("MySQL Connect Settings; " . var_export(config::Inst()->paydb, true));
  54. self::TestMysql_RW();
  55. echoLine("NoSql(Redis) Connect Settings: " . var_export(config::Inst()->nosql, true));
  56. self::TestNoSQL_RW();
  57. echoLine("Upload path Settings: " . "本框架暂未涉及.");
  58. }
  59. /**
  60. * DNS检查
  61. * @param type $url
  62. */
  63. public static function CheckDNS($url = 'qq.com') {
  64. echoLine("DNS check $url is " . (checkdnsrr($url, "A") ? "ok!" : "failed!"));
  65. }
  66. public static function CheckNetRead($url = 'qq.com') {
  67. $ret = HttpUtil::makeRequest($url, array());
  68. if ($ret["result"]) {
  69. echoLine("Request to $url ok!");
  70. } else {
  71. echoLine("Net Request Failed!:" . $ret['msg']);
  72. }
  73. }
  74. // <editor-fold defaultstate="collapsed" desc=" 内部函数 ">
  75. /**
  76. * 测试NoSql连接,以及读写性能
  77. */
  78. private static function TestNoSQL_RW() {
  79. $key = "test-" . TimeUtil::tsYmdHis();
  80. echoLine("Redis Server is running: " . gMem()->ping());
  81. echoLine("Redis Write($key, 'Hello world!'):" . gMem()->set($key, "Hello world!"));
  82. echoLine("Redis Read($key): " . gMem()->get($key));
  83. echo("Profile Redis set/get: ");
  84. RenderTime::Test(function ()use($key) {
  85. gMem()->set($key, "Hello world!");
  86. gMem()->get($key);
  87. }, 100);
  88. }
  89. /**
  90. * 测试MySQL连接,以及读写性能
  91. */
  92. private static function TestMysql_RW() {
  93. $tableName = "test-" . date('Ymd');
  94. echoLine("Connection is " . (daoInst()->Ping() ? "ok!" : "gone!"));
  95. $bCretaTable = daoInst()->tableExist($tableName);
  96. if ($bCretaTable) {
  97. echoLine("Table $tableName is Existing.");
  98. } else {
  99. echoLine("Table $tableName is not Existing.");
  100. $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 ; ";
  101. daoInst()->exec($sql);
  102. echoLine("create table $tableName " . (daoInst()->tableExist($tableName) ? "success!" : "failed!"));
  103. }
  104. $row = daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  105. echoLine("Insert into `$tableName` " . ($row == 1 ? "ok!" : "failed!"));
  106. echo("Profile Insert: ");
  107. RenderTime::Test(function()use($tableName) {
  108. daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  109. }, 100);
  110. $sql = "Drop Table `$tableName`;";
  111. daoInst()->exec($sql);
  112. echoLine("delete table $tableName " . (daoInst()->tableExist($tableName) ? "failed!" : "success!"));
  113. }
  114. // </editor-fold>
  115. }
  116. SelfChecker::checkkPHPVersion(); # 检查PHP版本
  117. SelfChecker::checkModules(); # 检查PHP模块