You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.9 KiB

11 months ago
  1. using Apewer.Network;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Apewer.Web
  7. {
  8. /// <summary>API 行为。</summary>
  9. public sealed class ApiAction : IToJson
  10. {
  11. #region fields
  12. Type _type = null;
  13. MethodInfo _method = null;
  14. string _path = null;
  15. HttpMethod[] _methods = null;
  16. ApiParameter[] _parameters = null;
  17. #endregion
  18. #region propeties
  19. /// <summary>控制器的反射类型。</summary>
  20. public Type Type { get => _type; }
  21. /// <summary>API 行为的反射方法。</summary>
  22. public MethodInfo MethodInfo { get => _method; }
  23. /// <summary>URL 路径。</summary>
  24. public string Path { get => _path; }
  25. /// <summary>HTTP 方法。</summary>
  26. public HttpMethod[] Methods
  27. {
  28. get
  29. {
  30. var result = new HttpMethod[_methods.Length];
  31. if (_methods.Length > 0) _methods.CopyTo(result, 0);
  32. return result;
  33. }
  34. }
  35. /// <summary>参数。</summary>
  36. public ApiParameter[] Parameters
  37. {
  38. get
  39. {
  40. var result = new ApiParameter[_parameters.Length];
  41. if (_parameters.Length > 0) _parameters.CopyTo(result, 0);
  42. return result;
  43. }
  44. }
  45. /// <summary>生成 JSON 实例。</summary>
  46. public Json ToJson() => ToJson(null);
  47. /// <summary>生成 JSON 实例。</summary>
  48. public Json ToJson(ApiActionJsonFormat format)
  49. {
  50. if (format == null) format = ApiActionJsonFormat.Default ?? new ApiActionJsonFormat();
  51. var methods = new Json();
  52. foreach (var method in _methods)
  53. {
  54. methods.AddItem(method.ToString().Lower());
  55. }
  56. var json = new Json();
  57. json.SetProperty("path", _path);
  58. json.SetProperty("methods", methods);
  59. if (format.WithReflection)
  60. {
  61. var reflection = new Json();
  62. reflection.SetProperty("type", _type.FullName);
  63. reflection.SetProperty("method", _method.Name);
  64. json.SetProperty("reflection", reflection);
  65. }
  66. if (format.WithParameters && _parameters.Length > 0)
  67. {
  68. var parameters = Json.NewArray();
  69. foreach (var parameter in _parameters)
  70. {
  71. parameters.AddItem(parameter.ToJson(format.WithReflection));
  72. }
  73. json.SetProperty("parameters", parameters);
  74. }
  75. return json;
  76. }
  77. /// <summary>生成字符串。</summary>
  78. public override string ToString() => _path;
  79. #endregion
  80. #region parse
  81. const string Separator = "/";
  82. /// <summary>创建 API 行为描述实例。</summary>
  83. /// <param name="type">控制器的反射类型。</param>
  84. /// <param name="method">API 行为的反射方法。</param>
  85. /// <param name="path">URL 路径。</param>
  86. /// <param name="methods">HTTP 方法。</param>
  87. /// <param name="parameters">参数。</param>
  88. /// <exception cref="ArgumentNullException" />
  89. /// <exception cref="ArgumentException" />
  90. ApiAction(Type type, MethodInfo method, string path, HttpMethod[] methods, ApiParameter[] parameters)
  91. {
  92. if (type == null) throw new ArgumentNullException(nameof(type));
  93. if (method == null) throw new ArgumentNullException(nameof(method));
  94. if (method.IsAbstract) throw new ArgumentException($"参数 {nameof(method)} 是抽象的。");
  95. if (path.IsEmpty()) throw new ArgumentNullException(nameof(path));
  96. if (methods.IsEmpty()) throw new ArgumentNullException(nameof(methods));
  97. var split = path.Split('/');
  98. var segs = split.Trim();
  99. path = segs.Length < 1 ? Separator : (Separator + string.Join(Separator, segs));
  100. _type = type;
  101. _method = method;
  102. _path = path;
  103. _methods = methods;
  104. _parameters = parameters;
  105. }
  106. /// <summary>解析控制器类型,获取 API 活动。</summary>
  107. /// <exception cref="ArgumentNullException" />
  108. public static ApiAction[] Parse(Type type)
  109. {
  110. if (type == null) throw new ArgumentNullException(nameof(type));
  111. if (type.FullName == "Front.Debug.Controller")
  112. {
  113. }
  114. // 检查类型的属性。
  115. if (!type.IsClass) return new ApiAction[0];
  116. if (type.IsAbstract) return new ApiAction[0];
  117. if (type.IsGenericType) return new ApiAction[0];
  118. if (type.GetGenericArguments().NotEmpty()) return new ApiAction[0];
  119. if (!RuntimeUtility.CanNew(type)) return new ApiAction[0];
  120. // 判断基类。
  121. if (!typeof(ApiController).IsAssignableFrom(type)) return new ApiAction[0];
  122. // 读取 URL 前缀。
  123. var prefixAttribute = RuntimeUtility.GetAttribute<RoutePrefixAttribute>(type, false);
  124. var prefixPath = (prefixAttribute == null || prefixAttribute.Path.IsEmpty()) ? null : prefixAttribute.Path.Split('/').Trim();
  125. // if (prefixPath.IsEmpty())
  126. // {
  127. // // 读取 API 特性。
  128. // var api = RuntimeUtility.GetAttribute<ApiAttribute>(type);
  129. // if (api != null)
  130. // {
  131. // var apiName = api.Name.ToTrim();
  132. // if (apiName.Lower().EndsWith("controller")) apiName = apiName.Substring(0, apiName.Length - 10);
  133. // if (apiName.NotEmpty()) prefixPath = new string[] { apiName };
  134. // }
  135. // }
  136. // 读取方法。
  137. var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
  138. var actions = new List<ApiAction>(methods.Length);
  139. foreach (var method in methods)
  140. {
  141. // 不支持构造函数和泛型。
  142. if (method.IsConstructor) continue;
  143. if (method.IsGenericMethod) continue;
  144. // 抽象类无法创建实例。
  145. if (method.IsAbstract) continue;
  146. if (!method.DeclaringType.Equals(type)) continue;
  147. // 必须有 Route 特性
  148. var route = RuntimeUtility.GetAttribute<RouteAttribute>(method);
  149. if (route == null) continue;
  150. // 确定路径
  151. var path = route.Path;
  152. if (path.IsEmpty())
  153. {
  154. if (prefixPath.IsEmpty()) continue;
  155. path = method.Name;
  156. }
  157. path = ConcatPath(prefixPath, path.Split('/').Trim());
  158. // 必须有 HTTP 方法
  159. var httpMethods = new List<HttpMethod>(9);
  160. if (RuntimeUtility.Contains<HttpConnectAttribute>(method)) httpMethods.Add(HttpMethod.CONNECT);
  161. if (RuntimeUtility.Contains<HttpDeleteAttribute>(method)) httpMethods.Add(HttpMethod.DELETE);
  162. if (RuntimeUtility.Contains<HttpGetAttribute>(method)) httpMethods.Add(HttpMethod.GET);
  163. if (RuntimeUtility.Contains<HttpHeadAttribute>(method)) httpMethods.Add(HttpMethod.HEAD);
  164. if (RuntimeUtility.Contains<HttpOptionsAttribute>(method)) httpMethods.Add(HttpMethod.OPTIONS);
  165. if (RuntimeUtility.Contains<HttpPatchAttribute>(method)) httpMethods.Add(HttpMethod.PATCH);
  166. if (RuntimeUtility.Contains<HttpPostAttribute>(method)) httpMethods.Add(HttpMethod.POST);
  167. if (RuntimeUtility.Contains<HttpPutAttribute>(method)) httpMethods.Add(HttpMethod.PUT);
  168. if (RuntimeUtility.Contains<HttpTraceAttribute>(method)) httpMethods.Add(HttpMethod.TRACE);
  169. if (httpMethods.Count < 1) continue;
  170. // 参数
  171. var parameters = new List<ApiParameter>();
  172. foreach (var pi in method.GetParameters())
  173. {
  174. var parameter = ApiParameter.Parse(pi);
  175. if (parameter == null) continue;
  176. parameters.Add(parameter);
  177. }
  178. var action = new ApiAction(type, method, path, httpMethods.ToArray(), parameters.ToArray());
  179. actions.Add(action);
  180. }
  181. return actions.ToArray();
  182. }
  183. static string ConcatPath(string[] prefix, string[] path)
  184. {
  185. var list = new List<string>();
  186. if (prefix != null) list.AddRange(prefix);
  187. if (path != null) list.AddRange(path);
  188. var segs = list.Trim();
  189. if (segs.Length < 1) return Separator;
  190. var result = Separator + string.Join(Separator, segs);
  191. return result;
  192. }
  193. #endregion
  194. }
  195. }