api.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Library\Protocol;
  3. class ApiException extends \Exception {
  4. function __construct($code, $message = '')
  5. {
  6. if (empty($message)) {
  7. $message = ErrorCode::message($code);
  8. if ($message === null) {
  9. $message = $code;
  10. $code = 0;
  11. }
  12. }
  13. parent::__construct($message, $code);
  14. }
  15. }
  16. final class _Api {
  17. public $id;
  18. public $path;
  19. public $method;
  20. public $header;
  21. public $payload;
  22. public $response;
  23. public $desc;
  24. public function __construct($data) {
  25. $this->id = (int) $data['id'];
  26. $this->path = (string) $data['path'];
  27. $this->method = (string) strtolower($data['method']);
  28. if ($data['header'] == 'null') {
  29. $this->header = null;
  30. } else {
  31. $klass = sprintf("%s\\%s", __NAMESPACE__, $data['header']);
  32. $this->header = new $klass;
  33. }
  34. if ($data['payload'] == 'null') {
  35. $this->payload = null;
  36. } else {
  37. $klass = sprintf("%s\\%s", __NAMESPACE__, $data['payload']);
  38. $this->payload = new $klass;
  39. }
  40. if ($data['response'] == 'null') {
  41. $this->response = null;
  42. } else {
  43. $klass = sprintf("%s\\%s", __NAMESPACE__, $data['response']);
  44. $this->response = new $klass;
  45. }
  46. $this->desc = (string) $data['desc'];
  47. }
  48. public function setHeader($data) {
  49. if ($this->header === null) {
  50. throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL);
  51. }
  52. if (is_array($data) || $data instanceof $this->header) {
  53. return $this->header->set($data);
  54. }
  55. return false;
  56. }
  57. public function setPayload($data) {
  58. if ($this->payload === null) {
  59. return false;
  60. }
  61. if (is_array($data) || $data instanceof $this->payload) {
  62. return $this->payload->set($data);
  63. }
  64. return $this;
  65. }
  66. public function setResponse($data, $value = null) {
  67. if ($this->response === null) {
  68. throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL);
  69. }
  70. if (is_array($data) || $data instanceof $this->response) {
  71. $this->response->set($data);
  72. } elseif (is_string($data)) {
  73. $this->response->set([$data => $value]);
  74. }
  75. return $this;
  76. }
  77. public function parse() {
  78. return $this->response->iterate();
  79. }
  80. }
  81. final class Api {
  82. private static $api = [
  83. '/api/token/get' => [
  84. 'payload' => 'token_req_info',
  85. 'header' => 'app_info',
  86. 'id' => '2',
  87. 'path' => '/api/token/get',
  88. 'method' => 'post',
  89. 'response' => 'token_info',
  90. 'desc' => '获取服务端token',
  91. ],
  92. '/api/servers/list' => [
  93. 'payload' => 'null',
  94. 'header' => 'app_info',
  95. 'id' => '1',
  96. 'path' => '/api/servers/list',
  97. 'method' => 'post',
  98. 'response' => 'service_list',
  99. 'desc' => '获取当前客户端可用服务端列表',
  100. ],
  101. '/api/token/refresh' => [
  102. 'payload' => 'token_info',
  103. 'header' => 'app_info',
  104. 'id' => '3',
  105. 'path' => '/api/token/refresh',
  106. 'method' => 'post',
  107. 'response' => 'token_info',
  108. 'desc' => '刷新服务端token',
  109. ],
  110. ];
  111. public static function get($path) {
  112. if (!isset(self::$api[$path])) {
  113. throw new ApiException(ErrorCode::PROTO_PATH_NOT_EXIST);
  114. }
  115. return new _Api(self::$api[$path]);
  116. }
  117. }