helpers.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * 助手函数
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2019/3/3
  7. * @time 16:34
  8. */
  9. use Luolongfei\App\Exceptions\LlfException;
  10. use Luolongfei\Libs\Argv;
  11. use Luolongfei\Libs\Config;
  12. use Luolongfei\Libs\Log;
  13. use Luolongfei\Libs\Env;
  14. use Luolongfei\Libs\Lang;
  15. use Luolongfei\Libs\PhpColor;
  16. use Luolongfei\App\Console\MigrateEnvFile;
  17. if (!function_exists('config')) {
  18. /**
  19. * 获取配置
  20. *
  21. * @param string $key 键,支持点式访问
  22. * @param string $default 默认值
  23. *
  24. * @return array|mixed
  25. */
  26. function config($key = '', $default = null)
  27. {
  28. return Config::getInstance()->get($key, $default);
  29. }
  30. }
  31. if (!function_exists('lang')) {
  32. /**
  33. * 读取语言包
  34. *
  35. * @param string $key 键,支持点式访问
  36. *
  37. * @return array|mixed|null
  38. */
  39. function lang($key = '')
  40. {
  41. return Lang::getInstance()->get($key);
  42. }
  43. }
  44. if (!function_exists('system_log')) {
  45. /**
  46. * 写日志
  47. *
  48. * @param $content
  49. * @param array $response
  50. * @param string $fileName
  51. * @description 受支持的着色标签
  52. * 'reset', 'bold', 'dark', 'italic', 'underline', 'blink', 'reverse', 'concealed', 'default', 'black', 'red',
  53. * 'green', 'yellow', 'blue', 'magenta', 'cyan', 'light_gray', 'dark_gray', 'light_red', 'light_green',
  54. * 'light_yellow', 'light_blue', 'light_magenta', 'light_cyan', 'white', 'bg_default', 'bg_black', 'bg_red',
  55. * 'bg_green', 'bg_yellow', 'bg_blue', 'bg_magenta', 'bg_cyan', 'bg_light_gray', 'bg_dark_gray', 'bg_light_red',
  56. * 'bg_light_green','bg_light_yellow', 'bg_light_blue', 'bg_light_magenta', 'bg_light_cyan', 'bg_white'
  57. */
  58. function system_log($content, array $response = [], $fileName = '')
  59. {
  60. try {
  61. $path = sprintf('%s/logs/%s/', ROOT_PATH, date('Y-m'));
  62. $file = $path . ($fileName ?: date('d')) . '.log';
  63. if (!is_dir($path)) {
  64. mkdir($path, 0777, true);
  65. chmod($path, 0777);
  66. }
  67. $handle = fopen($file, 'a'); // 追加而非覆盖
  68. if (!filesize($file)) {
  69. chmod($file, 0666);
  70. }
  71. $msg = sprintf(
  72. "[%s] %s %s\n",
  73. date('Y-m-d H:i:s'),
  74. is_string($content) ? $content : json_encode($content),
  75. $response ? json_encode($response, JSON_UNESCAPED_UNICODE) : '');
  76. // 在 Github Actions 上运行,过滤敏感信息
  77. if (env('ON_GITHUB_ACTIONS')) {
  78. $msg = preg_replace_callback('/(?P<secret>[\w-.]{1,4}?)(?=@[\w-.]+)/i', function ($m) {
  79. return str_ireplace($m['secret'], str_repeat('*', strlen($m['secret'])), $m['secret']);
  80. }, $msg);
  81. }
  82. // 尝试为消息着色
  83. $c = PhpColor::getInstance()->getColorInstance();
  84. echo $c($msg)->colorize();
  85. // 干掉着色标签
  86. $msg = strip_tags($msg); // 不完整或者破损标签将导致更多的数据被删除
  87. fwrite($handle, $msg);
  88. fclose($handle);
  89. flush();
  90. } catch (\Exception $e) {
  91. // do nothing
  92. }
  93. }
  94. }
  95. if (!function_exists('is_locked')) {
  96. /**
  97. * 检查任务是否已被锁定
  98. *
  99. * @param string $taskName
  100. * @param bool $always 是否被永久锁定
  101. *
  102. * @return bool
  103. * @throws Exception
  104. */
  105. function is_locked($taskName = '', $always = false)
  106. {
  107. try {
  108. $lock = sprintf(
  109. '%s/num_limit/%s/%s.lock',
  110. APP_PATH,
  111. $always ? 'always' : date('Y-m-d'),
  112. $taskName
  113. );
  114. return file_exists($lock);
  115. } catch (\Exception $e) {
  116. system_log(sprintf('检查任务%s是否锁定时出错,错误原因:%s', $taskName, $e->getMessage()));
  117. }
  118. return false;
  119. }
  120. }
  121. if (!function_exists('lock_task')) {
  122. /**
  123. * 锁定任务
  124. *
  125. * 防止重复执行
  126. *
  127. * @param string $taskName
  128. * @param bool $always 是否永久锁定
  129. *
  130. * @return bool
  131. */
  132. function lock_task($taskName = '', $always = false)
  133. {
  134. try {
  135. $lock = sprintf(
  136. '%s/num_limit/%s/%s.lock',
  137. APP_PATH,
  138. $always ? 'always' : date('Y-m-d'),
  139. $taskName
  140. );
  141. $path = dirname($lock);
  142. if (!is_dir($path)) {
  143. mkdir($path, 0777, true);
  144. chmod($path, 0777);
  145. }
  146. if (file_exists($lock)) {
  147. return true;
  148. }
  149. $handle = fopen($lock, 'a'); // 追加而非覆盖
  150. if (!filesize($lock)) {
  151. chmod($lock, 0666);
  152. }
  153. fwrite($handle, sprintf(
  154. "Locked at %s.\n",
  155. date('Y-m-d H:i:s')
  156. )
  157. );
  158. fclose($handle);
  159. Log::info(sprintf('%s已被锁定,此任务%s已不会再执行,请知悉', $taskName, $always ? '' : '今天内'));
  160. } catch (\Exception $e) {
  161. system_log(sprintf('创建锁定任务文件%s时出错,错误原因:%s', $lock, $e->getMessage()));
  162. return false;
  163. }
  164. return true;
  165. }
  166. }
  167. if (!function_exists('env')) {
  168. /**
  169. * 获取环境变量值
  170. *
  171. * @param string $key
  172. * @param string $default 默认值
  173. *
  174. * @return array | bool | false | null | string
  175. */
  176. function env($key = '', $default = null)
  177. {
  178. return Env::getInstance()->get($key, $default);
  179. }
  180. }
  181. if (!function_exists('get_argv')) {
  182. /**
  183. * 获取命令行传参
  184. *
  185. * @param string $name
  186. * @param string $default 默认值
  187. *
  188. * @return mixed|string
  189. */
  190. function get_argv(string $name, string $default = '')
  191. {
  192. return Argv::getInstance()->get($name, $default);
  193. }
  194. }
  195. if (!function_exists('system_check')) {
  196. /**
  197. * 检查环境是否满足要求
  198. *
  199. * @throws LlfException
  200. */
  201. function system_check()
  202. {
  203. if (version_compare(PHP_VERSION, '7.0.0') < 0) {
  204. throw new LlfException(34520006);
  205. }
  206. // 如果是在腾讯云函数部署,则不需要检查这几项
  207. if (IS_SCF) {
  208. system_log('检测到运行环境为腾讯云函数,所有环境变量将直接从环境中读取,如果 .env 文件存在的话,则优先使用 .env 文件中的环境变量');
  209. system_log('在腾讯云函数,可以参考此处修改或新增环境变量,无需重建:https://github.com/luolongfei/freenom/blob/main/resources/screenshot/scf_env_modify.png');
  210. } else {
  211. if (!function_exists('putenv')) {
  212. throw new LlfException(34520005);
  213. }
  214. $envFile = ROOT_PATH . '/.env';
  215. if (!file_exists($envFile)) {
  216. throw new LlfException(copy(ROOT_PATH . '/.env.example', $envFile) ? 34520007 : 34520008);
  217. }
  218. // 检查当前 .env 文件版本是否过低,过低自动升级
  219. MigrateEnvFile::getInstance()->handle();
  220. }
  221. if (!extension_loaded('curl')) {
  222. throw new LlfException(34520010);
  223. }
  224. }
  225. }