123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace App\Library\Protocol;
- class ApiException extends \Exception {
- function __construct($code, $message = '')
- {
- if (empty($message)) {
- $message = ErrorCode::message($code);
- if ($message === null) {
- $message = $code;
- $code = 0;
- }
- }
- parent::__construct($message, $code);
- }
- }
- final class _Api {
- public $id;
- public $path;
- public $method;
- public $header;
- public $payload;
- public $response;
- public $desc;
- public function __construct($data) {
- $this->id = (int) $data['id'];
- $this->path = (string) $data['path'];
- $this->method = (string) strtolower($data['method']);
- if ($data['header'] == 'null') {
- $this->header = null;
- } else {
- $klass = sprintf("%s\\%s", __NAMESPACE__, $data['header']);
- $this->header = new $klass;
- }
- if ($data['payload'] == 'null') {
- $this->payload = null;
- } else {
- $klass = sprintf("%s\\%s", __NAMESPACE__, $data['payload']);
- $this->payload = new $klass;
- }
- if ($data['response'] == 'null') {
- $this->response = null;
- } else {
- $klass = sprintf("%s\\%s", __NAMESPACE__, $data['response']);
- $this->response = new $klass;
- }
- $this->desc = (string) $data['desc'];
- }
- public function setHeader($data) {
- if ($this->header === null) {
- throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL);
- }
- if (is_array($data) || $data instanceof $this->header) {
- return $this->header->set($data);
- }
- return false;
- }
- public function setPayload($data) {
- if ($this->payload === null) {
- return false;
- }
- if (is_array($data) || $data instanceof $this->payload) {
- return $this->payload->set($data);
- }
- return $this;
- }
- public function setResponse($data, $value = null) {
- if ($this->response === null) {
- throw new ApiException(ErrorCode::PROTO_TRY_TO_SET_VALUE_ON_NULL);
- }
- if (is_array($data) || $data instanceof $this->response) {
- $this->response->set($data);
- } elseif (is_string($data)) {
- $this->response->set([$data => $value]);
- }
- return $this;
- }
- public function parse() {
- return $this->response->iterate();
- }
- }
- final class Api {
- private static $api = [
- '/api/token/get' => [
- 'payload' => 'token_req_info',
- 'header' => 'app_info',
- 'id' => '2',
- 'path' => '/api/token/get',
- 'method' => 'post',
- 'response' => 'token_info',
- 'desc' => '获取服务端token',
- ],
- '/api/servers/list' => [
- 'payload' => 'null',
- 'header' => 'app_info',
- 'id' => '1',
- 'path' => '/api/servers/list',
- 'method' => 'post',
- 'response' => 'service_list',
- 'desc' => '获取当前客户端可用服务端列表',
- ],
- '/api/token/refresh' => [
- 'payload' => 'token_info',
- 'header' => 'app_info',
- 'id' => '3',
- 'path' => '/api/token/refresh',
- 'method' => 'post',
- 'response' => 'token_info',
- 'desc' => '刷新服务端token',
- ],
- ];
- public static function get($path) {
- if (!isset(self::$api[$path])) {
- throw new ApiException(ErrorCode::PROTO_PATH_NOT_EXIST);
- }
- return new _Api(self::$api[$path]);
- }
- }
|