CmdProxy.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/5/21
  6. * Time: 16:03
  7. */
  8. namespace app\logic;
  9. ///composer update或者composer install或者composer dump-autoload.
  10. ///
  11. use app\lib\DataPack;
  12. use app\logic\cmdpro\CmdTest as CmdTest;
  13. use app\logic\cmdpro\CmdQuery as CmdQuery;
  14. use app\logic\cmdpro\CmdLogin as CmdLogin;
  15. use app\logic\cmdpro\CmdSubser as CmdSubser;
  16. class CmdProxy
  17. {
  18. private static $Instance = null;
  19. public static function getInstance($cache = true)
  20. {
  21. if ($cache && self::$Instance) {
  22. return self::$Instance;
  23. }
  24. self::$Instance = new self();
  25. return self::$Instance;
  26. }
  27. public function ParaCMD($serv, \Swoole\Server\Task $task)
  28. {
  29. $fd = $task->data->fd;
  30. $dataPack = json_decode(trim($task->data->data),true);
  31. $cmd = isset($dataPack['cmd']) ? $dataPack['cmd'] : '';
  32. $cmdcls = $this->getParaCmd($cmd);
  33. if (!$cmdcls) {
  34. $serv->push($fd, DataPack::toJson(['mt' => 'errorMsg', 'data' => 'error cmd!']));
  35. $serv->disconnect($fd);
  36. return;
  37. }
  38. $cmdObj = $this->getClsObj($cmdcls);
  39. try{
  40. $cmdObj->paraCmd($serv, $task);
  41. }catch (\Exception $e){
  42. }
  43. unset($cmdObj) ;
  44. }
  45. public function getClsObj($cmd)
  46. {
  47. $obj = null ;
  48. switch ($cmd){
  49. case "CmdTest":
  50. $obj = new CmdTest();
  51. break ;
  52. case "CmdQuery":
  53. $obj = new CmdQuery();
  54. break;
  55. case "CmdLogin":
  56. $obj = new CmdLogin();
  57. break;
  58. case "CmdSubser":
  59. $obj = new CmdSubser();
  60. break;
  61. default:
  62. break;
  63. }
  64. return $obj ;
  65. }
  66. public function allCmdMaps()
  67. {
  68. $arrs = [
  69. 'login' => 'CmdLogin',
  70. 'query' => 'CmdQuery',
  71. 'test' => 'CmdTest',
  72. 'subserv' => 'CmdSubser',
  73. ];
  74. return $arrs;
  75. }
  76. public function getParaCmd($cmd)
  77. {
  78. $arr = $this->allCmdMaps();
  79. if (isset($arr[$cmd])) {
  80. return $arr[$cmd];
  81. }
  82. return false;
  83. }
  84. }