SelfChecker.php 4.8 KB

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