SelfChecker.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  75. * 测试NoSql连接,以及读写性能
  76. */
  77. private static function TestNoSQL_RW() {
  78. $key = "test-" . TimeUtil::tsYmdHis();
  79. echoLine("Redis Server is running: " . gMem()->ping());
  80. echoLine("Redis Write($key, 'Hello world!'):" . gMem()->set($key, "Hello world!"));
  81. echoLine("Redis Read($key): " . gMem()->get($key));
  82. echo("Profile Redis set/get: ");
  83. RenderTime::Test(function ()use($key) {
  84. gMem()->set($key, "Hello world!");
  85. gMem()->get($key);
  86. }, 100);
  87. }
  88. /**
  89. * 测试MySQL连接,以及读写性能
  90. */
  91. private static function TestMysql_RW() {
  92. $tableName = "test-" . date('Ymd');
  93. echoLine("Connection is " . (daoInst()->Ping() ? "ok!" : "gone!"));
  94. $bCretaTable = daoInst()->tableExist($tableName);
  95. if ($bCretaTable) {
  96. echoLine("Table $tableName is Existing.");
  97. } else {
  98. echoLine("Table $tableName is not Existing.");
  99. $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 ; ";
  100. daoInst()->exec($sql);
  101. echoLine("create table $tableName " . (daoInst()->tableExist($tableName) ? "success!" : "failed!"));
  102. }
  103. $row = daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  104. echoLine("Insert into `$tableName` " . ($row == 1 ? "ok!" : "failed!"));
  105. echo("Profile Insert: ");
  106. RenderTime::Test(function()use($tableName) {
  107. daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  108. }, 100);
  109. $sql = "Drop Table `$tableName`;";
  110. daoInst()->exec($sql);
  111. echoLine("delete table $tableName " . (daoInst()->tableExist($tableName) ? "failed!" : "success!"));
  112. }
  113. }
  114. SelfChecker::checkkPHPVersion(); # 检查PHP版本
  115. SelfChecker::checkModules(); # 检查PHP模块