DevService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Module\Dev\Services;
  3. use App\Module\Dev\Enums\DEV_LOG_TYPE;
  4. use App\Module\Dev\Logics\DevLogic;
  5. use App\Module\Dev\Models\Dev;
  6. use App\Module\Dev\Models\DevConfig;
  7. use App\Module\Dev\Models\DevLog;
  8. use Illuminate\Support\Facades\Cache;
  9. /**
  10. * 开发工具服务类
  11. *
  12. * 提供开发工具相关的服务方法,供其他模块调用
  13. */
  14. class DevService
  15. {
  16. /**
  17. * 获取开发工具列表
  18. *
  19. * @param array $params 查询参数
  20. * @return \Illuminate\Pagination\LengthAwarePaginator
  21. */
  22. public static function getDevTools(array $params = [])
  23. {
  24. return DevLogic::getDevTools($params);
  25. }
  26. /**
  27. * 获取开发工具详情
  28. *
  29. * @param int $id 开发工具ID
  30. * @return Dev|null
  31. */
  32. public static function getDevToolDetail(int $id): ?Dev
  33. {
  34. return Dev::find($id);
  35. }
  36. /**
  37. * 创建开发工具
  38. *
  39. * @param array $data 开发工具数据
  40. * @return Dev
  41. */
  42. public static function createDevTool(array $data): Dev
  43. {
  44. return DevLogic::createDevTool($data);
  45. }
  46. /**
  47. * 更新开发工具
  48. *
  49. * @param int $id 开发工具ID
  50. * @param array $data 开发工具数据
  51. * @return bool
  52. */
  53. public static function updateDevTool(int $id, array $data): bool
  54. {
  55. return DevLogic::updateDevTool($id, $data);
  56. }
  57. /**
  58. * 删除开发工具
  59. *
  60. * @param int $id 开发工具ID
  61. * @return bool
  62. */
  63. public static function deleteDevTool(int $id): bool
  64. {
  65. return DevLogic::deleteDevTool($id);
  66. }
  67. /**
  68. * 记录开发日志
  69. *
  70. * @param string $type 日志类型
  71. * @param string $content 日志内容
  72. * @param array $extraData 额外数据
  73. * @return DevLog
  74. */
  75. public static function log(string $type, string $content, array $extraData = []): DevLog
  76. {
  77. return DevLogic::log($type, $content, $extraData);
  78. }
  79. /**
  80. * 记录信息日志
  81. *
  82. * @param string $content 日志内容
  83. * @param array $extraData 额外数据
  84. * @return DevLog
  85. */
  86. public static function logInfo(string $content, array $extraData = []): DevLog
  87. {
  88. return self::log(DEV_LOG_TYPE::INFO->value, $content, $extraData);
  89. }
  90. /**
  91. * 记录警告日志
  92. *
  93. * @param string $content 日志内容
  94. * @param array $extraData 额外数据
  95. * @return DevLog
  96. */
  97. public static function logWarning(string $content, array $extraData = []): DevLog
  98. {
  99. return self::log(DEV_LOG_TYPE::WARNING->value, $content, $extraData);
  100. }
  101. /**
  102. * 记录错误日志
  103. *
  104. * @param string $content 日志内容
  105. * @param array $extraData 额外数据
  106. * @return DevLog
  107. */
  108. public static function logError(string $content, array $extraData = []): DevLog
  109. {
  110. return self::log(DEV_LOG_TYPE::ERROR->value, $content, $extraData);
  111. }
  112. /**
  113. * 记录调试日志
  114. *
  115. * @param string $content 日志内容
  116. * @param array $extraData 额外数据
  117. * @return DevLog
  118. */
  119. public static function logDebug(string $content, array $extraData = []): DevLog
  120. {
  121. return self::log(DEV_LOG_TYPE::DEBUG->value, $content, $extraData);
  122. }
  123. /**
  124. * 获取开发配置
  125. *
  126. * @param string $key 配置键
  127. * @param mixed $default 默认值
  128. * @return mixed
  129. */
  130. public static function getConfig(string $key, $default = null)
  131. {
  132. $cacheKey = 'dev_config_' . $key;
  133. return Cache::remember($cacheKey, 3600, function () use ($key, $default) {
  134. $config = DevConfig::where('key', $key)->first();
  135. return $config ? $config->value : $default;
  136. });
  137. }
  138. /**
  139. * 设置开发配置
  140. *
  141. * @param string $key 配置键
  142. * @param mixed $value 配置值
  143. * @param string $description 描述
  144. * @return DevConfig
  145. */
  146. public static function setConfig(string $key, $value, string $description = ''): DevConfig
  147. {
  148. $config = DevConfig::updateOrCreate(
  149. ['key' => $key],
  150. [
  151. 'value' => $value,
  152. 'description' => $description,
  153. 'status' => 1,
  154. ]
  155. );
  156. // 清除缓存
  157. Cache::forget('dev_config_' . $key);
  158. return $config;
  159. }
  160. /**
  161. * 获取所有开发配置
  162. *
  163. * @return \Illuminate\Database\Eloquent\Collection
  164. */
  165. public static function getAllConfigs()
  166. {
  167. return DevConfig::all();
  168. }
  169. /**
  170. * 获取系统信息
  171. *
  172. * @return array
  173. */
  174. public static function getSystemInfo(): array
  175. {
  176. return [
  177. 'php_version' => PHP_VERSION,
  178. 'laravel_version' => app()->version(),
  179. 'server_software' => $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown',
  180. 'server_os' => PHP_OS,
  181. 'database_connection' => config('database.default'),
  182. 'cache_driver' => config('cache.default'),
  183. 'session_driver' => config('session.driver'),
  184. 'queue_connection' => config('queue.default'),
  185. 'timezone' => config('app.timezone'),
  186. 'locale' => config('app.locale'),
  187. 'env' => config('app.env'),
  188. 'debug' => config('app.debug'),
  189. 'url' => config('app.url'),
  190. 'memory_limit' => ini_get('memory_limit'),
  191. 'max_execution_time' => ini_get('max_execution_time'),
  192. 'upload_max_filesize' => ini_get('upload_max_filesize'),
  193. 'post_max_size' => ini_get('post_max_size'),
  194. ];
  195. }
  196. }