SelfChecker.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace loyalsoft;
  3. /**
  4. * Description of SelfChecker
  5. * 框架的自我检查工具
  6. * @author gwang
  7. */
  8. class SelfChecker {
  9. /**
  10. * 检查模块是否加载
  11. * @param string[] $arr 要检查的模块名称
  12. */
  13. public static function checkModules($arr = array()) {
  14. $default = array(# # 必备基础模块
  15. "curl",
  16. "mbstring",
  17. "openssl",
  18. "pdo_mysql",
  19. "sockets",
  20. "zlib",
  21. );
  22. $r_modules = array_merge($default, $arr); # 合并指定模块
  23. $diff = array_diff($r_modules, get_loaded_extensions()); # 判断缺失模块
  24. if (count($diff)) { # 确实缺失模块
  25. die("require modules: " . implode(' ,', $diff)); # 直接结束运行
  26. }
  27. }
  28. /**
  29. * 检查PHP版本
  30. * @param string $ver 最低版本字符串类似于7.3.5
  31. */
  32. public static function checkkPHPVersion($ver = null) {
  33. $default_ver = "5.4.16";
  34. $r_ver = isset($ver) ? $ver : $default_ver;
  35. if (version_compare(PHP_VERSION, $r_ver, '<')) {
  36. die('<font color="red">This code request at least PHP version ' . $r_ver #
  37. . ', Current version: ' . PHP_VERSION . ".</font><br/>" . PHP_EOL);
  38. }
  39. }
  40. /**
  41. * 检查配置文件
  42. */
  43. public static function CheckConfig() {
  44. echoLine("检查配置:");
  45. echoLine("GameOnline: " . GAME_ONLINE);
  46. echoLine("MySQL Connect Settings; " . var_export(config::Inst()->paydb, true));
  47. self::TestMysql_RW();
  48. echoLine("NoSql(Redis) Connect Settings: " . var_export(config::Inst()->nosql, true));
  49. self::TestNoSQL_RW();
  50. echoLine("Upload path Settings: " . "本框架暂未涉及.");
  51. }
  52. /**
  53. * 测试NoSql连接,以及读写性能
  54. */
  55. private static function TestNoSQL_RW() {
  56. $key = "test-" . TimeUtil::tsYmdHis();
  57. echoLine("Redis Server is running: " . gMem()->ping());
  58. echoLine("Redis Write($key, 'Hello world!'):" . gMem()->set($key, "Hello world!"));
  59. echoLine("Redis Read($key): " . gMem()->get($key));
  60. echo("Profile Redis set/get: ");
  61. RenderTime::Test(function ()use($key) {
  62. gMem()->set($key, "Hello world!");
  63. gMem()->get($key);
  64. }, 100);
  65. }
  66. /**
  67. * 测试MySQL连接,以及读写性能
  68. */
  69. private static function TestMysql_RW() {
  70. $tableName = "test-" . date('Ymd');
  71. echoLine("Connection is " . (daoInst()->Ping() ? "ok!" : "gone!"));
  72. $bCretaTable = daoInst()->tableExist($tableName);
  73. if ($bCretaTable) {
  74. echoLine("Table $tableName is Existing.");
  75. } else {
  76. echoLine("Table $tableName is not Existing.");
  77. $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 ; ";
  78. daoInst()->exec($sql);
  79. echoLine("create table $tableName " . (daoInst()->tableExist($tableName) ? "success!" : "failed!"));
  80. }
  81. $row = daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  82. echoLine("Insert into `$tableName` " . ($row == 1 ? "ok!" : "failed!"));
  83. echo("Profile Insert: ");
  84. RenderTime::Test(function()use($tableName) {
  85. daoInst()->insert("`$tableName`")->data(array('msg' => 'Hello world!'))->exec();
  86. }, 10);
  87. $sql = "Drop Table `$tableName`;";
  88. daoInst()->exec($sql);
  89. echoLine("delete table $tableName " . (daoInst()->tableExist($tableName) ? "failed!" : "success!"));
  90. }
  91. }
  92. SelfChecker::checkkPHPVersion(); # 检查PHP版本
  93. SelfChecker::checkModules(); # 检查PHP模块