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.

179 lines
5.8 KiB

4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
7 months ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace Apewer.Web
  5. {
  6. /// <summary></summary>
  7. public sealed class ApiApplication : IToJson
  8. {
  9. #region fields
  10. Type _type = null;
  11. string _module = null;
  12. // ApiAttribute
  13. string _name = null;
  14. string _caption = null;
  15. string _description = null;
  16. // invoke & enumerate
  17. bool _independent = false;
  18. bool _hidden = false;
  19. // functions
  20. Dictionary<string, ApiFunction> _dict = new Dictionary<string, ApiFunction>();
  21. List<ApiFunction> _list = new List<ApiFunction>();
  22. #endregion
  23. #region properties
  24. /// <summary></summary>
  25. public Type Type { get => _type; }
  26. /// <summary></summary>
  27. public string Module { get => _module; }
  28. /// <summary></summary>
  29. public string Name { get => _name; }
  30. /// <summary></summary>
  31. public string Caption { get => _caption; }
  32. /// <summary></summary>
  33. public string Description { get => _description; }
  34. /// <summary></summary>
  35. public bool Independent { get => _independent; }
  36. /// <summary></summary>
  37. public bool Hidden { get => _hidden; }
  38. /// <summary></summary>
  39. public ApiFunction[] Functions { get => _list.ToArray(); }
  40. #endregion
  41. /// <summary></summary>
  42. public ApiApplication(Type type, ApiAttribute api)
  43. {
  44. // type
  45. _type = type;
  46. // api
  47. if (api == null)
  48. {
  49. _name = type?.Name;
  50. }
  51. else
  52. {
  53. _name = string.IsNullOrEmpty(api.Name) ? type?.Name : api.Name;
  54. _caption = api.Caption;
  55. _description = api.Description;
  56. }
  57. if (type != null)
  58. {
  59. // caption
  60. if (string.IsNullOrEmpty(_caption))
  61. {
  62. var captions = type.GetCustomAttributes(typeof(CaptionAttribute), true);
  63. if (captions.Length > 0)
  64. {
  65. var caption = (CaptionAttribute)captions[0];
  66. _caption = caption.Title;
  67. if (string.IsNullOrEmpty(_description))
  68. {
  69. _description = caption.Description;
  70. }
  71. }
  72. }
  73. // hidden
  74. if (type.Contains<HiddenAttribute>(false)) _hidden = true;
  75. // independent
  76. if (type.Contains<IndependentAttribute>(false)) _independent = true;
  77. // Module
  78. var assemblyName = type.Assembly.GetName();
  79. _module = TextUtility.Join("-", assemblyName.Name, assemblyName.Version.ToString());
  80. // functions
  81. var funcs = new Dictionary<string, ApiFunction>();
  82. var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
  83. foreach (var method in methods)
  84. {
  85. var func = ApiFunction.Parse(this, method);
  86. if (func == null) continue;
  87. var funcKey = func.Name.Lower();
  88. if (funcs.ContainsKey(funcKey)) continue;
  89. funcs.Add(funcKey, func);
  90. }
  91. _dict = funcs;
  92. _list.AddRange(funcs.Values);
  93. _list.Sort(new Comparison<ApiFunction>((a, b) => a.Name.CompareTo(b.Name)));
  94. }
  95. }
  96. internal ApiFunction GetFunction(string name)
  97. {
  98. if (string.IsNullOrEmpty(name)) return null;
  99. if (_dict.TryGetValue(name.ToLower(), out var func)) return func;
  100. return null;
  101. }
  102. /// <summary></summary>
  103. public Json ToJson() => ToJson(new ApiOptions());
  104. internal Json ToJson(ApiOptions options)
  105. {
  106. if (Hidden) return null;
  107. var json = Json.NewObject();
  108. json.SetProperty("name", _name);
  109. if (!string.IsNullOrEmpty(_caption)) json.SetProperty("caption", _caption);
  110. if (!string.IsNullOrEmpty(_description)) json.SetProperty("description", _description);
  111. if (options != null)
  112. {
  113. if (options.WithTypeName) json.SetProperty("type", _type.FullName);
  114. if (options.WithModuleName) json.SetProperty("mudule", _module);
  115. if (options.AllowEnumerate) json.SetProperty("functions", Json.From(_list));
  116. }
  117. return json;
  118. }
  119. /// <summary>解析类型,获取 <see cref="ApiApplication"/> 实例。</summary>
  120. /// <param name="type">要解析的类型。</param>
  121. /// <param name="requireAttribute">要求此类型拥有 Api 特性。</param>
  122. /// <returns>解析成功返回实例,解析失败返回 NULL 值。</returns>
  123. public static ApiApplication Parse(Type type, bool requireAttribute)
  124. {
  125. if (type == null) return null;
  126. // 检查类型的属性。
  127. if (!type.IsClass) return null;
  128. if (type.IsAbstract) return null;
  129. if (type.IsGenericType) return null;
  130. if (type.GetGenericArguments().NotEmpty()) return null;
  131. if (!RuntimeUtility.CanNew(type)) return null;
  132. // 判断基类。
  133. if (!typeof(ApiController).IsAssignableFrom(type)) return null;
  134. // 检查类型的特性。
  135. var apis = type.GetCustomAttributes(typeof(ApiAttribute), false);
  136. var api = apis.Length > 0 ? (ApiAttribute)apis[0] : null;
  137. if (requireAttribute && api == null) return null;
  138. return new ApiApplication(type, api);
  139. }
  140. }
  141. }