DevService.php 5.5 KB

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