Frequencies.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catcher\library\crontab;
  12. use think\helper\Str;
  13. /**
  14. * From Laravel
  15. *
  16. * Trait ManagesFrequencies
  17. * @package catcher\library\crontab
  18. */
  19. trait Frequencies
  20. {
  21. /**
  22. * The Cron expression representing the event's frequency.
  23. *
  24. * @param string $expression
  25. * @return $this
  26. */
  27. public function cron($expression)
  28. {
  29. $this->expression = $expression;
  30. return $this;
  31. }
  32. /**
  33. * 每十秒
  34. *
  35. * @time 2020年07月04日
  36. * @return $this
  37. */
  38. public function everyTenSeconds()
  39. {
  40. $this->second = 10;
  41. return $this;
  42. }
  43. /**
  44. * 每二十秒
  45. *
  46. * @time 2020年07月04日
  47. * @return $this
  48. */
  49. public function everyTwentySeconds()
  50. {
  51. $this->second = 20;
  52. return $this;
  53. }
  54. /**
  55. * 每三十秒
  56. *
  57. * @time 2020年07月04日
  58. * @return $this
  59. */
  60. public function everyThirtySeconds()
  61. {
  62. $this->second = 30;
  63. return $this;
  64. }
  65. /**
  66. * 每分钟
  67. *
  68. * @time 2020年07月04日
  69. * @return Frequencies
  70. */
  71. public function everyMinute()
  72. {
  73. return $this->spliceIntoPosition(1, '*');
  74. }
  75. /**
  76. * 5 分钟
  77. *
  78. * @time 2020年07月04日
  79. * @return Frequencies
  80. */
  81. public function everyFiveMinutes()
  82. {
  83. return $this->spliceIntoPosition(1, '*/5');
  84. }
  85. /**
  86. * 10 分钟
  87. *
  88. * @time 2020年07月04日
  89. * @return Frequencies
  90. */
  91. public function everyTenMinutes()
  92. {
  93. return $this->spliceIntoPosition(1, '*/10');
  94. }
  95. /**
  96. * 15 分钟
  97. *
  98. * @time 2020年07月04日
  99. * @return Frequencies
  100. */
  101. public function everyFifteenMinutes()
  102. {
  103. return $this->spliceIntoPosition(1, '*/15');
  104. }
  105. /**
  106. * 三十分钟
  107. *
  108. * @time 2020年07月04日
  109. * @return Frequencies
  110. */
  111. public function everyThirtyMinutes()
  112. {
  113. return $this->spliceIntoPosition(1, '0,30');
  114. }
  115. /**
  116. * 每小时
  117. *
  118. * @time 2020年07月04日
  119. * @return Frequencies
  120. */
  121. public function hourly()
  122. {
  123. return $this->spliceIntoPosition(1, 0);
  124. }
  125. /**
  126. * 小时的时间
  127. *
  128. * @param array|int $offset
  129. * @return $this
  130. */
  131. public function hourlyAt($offset)
  132. {
  133. $offset = is_array($offset) ? implode(',', $offset) : $offset;
  134. return $this->spliceIntoPosition(1, $offset);
  135. }
  136. /**
  137. * 每天
  138. *
  139. * @time 2020年07月04日
  140. * @return Frequencies
  141. */
  142. public function daily()
  143. {
  144. return $this->spliceIntoPosition(1, 0)
  145. ->spliceIntoPosition(2, 0);
  146. }
  147. /**
  148. * 每天固定时间启动
  149. *
  150. * @param string $time
  151. * @return $this
  152. */
  153. public function at($time)
  154. {
  155. return $this->dailyAt($time);
  156. }
  157. /**
  158. * 每天固定时间启动 (10:00, 19:30, etc).
  159. *
  160. * @param string $time
  161. * @return $this
  162. */
  163. public function dailyAt($time)
  164. {
  165. $segments = explode(':', $time);
  166. return $this->spliceIntoPosition(2, (int) $segments[0])
  167. ->spliceIntoPosition(1, count($segments) === 2 ? (int) $segments[1] : '0');
  168. }
  169. /**
  170. * 每两天一次
  171. *
  172. * @param int $first
  173. * @param int $second
  174. * @return $this
  175. */
  176. public function twiceDaily($first = 1, $second = 13)
  177. {
  178. $hours = $first.','.$second;
  179. return $this->spliceIntoPosition(1, 0)
  180. ->spliceIntoPosition(2, $hours);
  181. }
  182. /**
  183. * 工作日跑
  184. *
  185. * @return $this
  186. */
  187. public function weekdays()
  188. {
  189. return $this->spliceIntoPosition(5, '1-5');
  190. }
  191. /**
  192. * 周末
  193. *
  194. * @time 2020年07月04日
  195. * @return Frequencies
  196. */
  197. public function weekends()
  198. {
  199. return $this->spliceIntoPosition(5, '0,6');
  200. }
  201. /**
  202. * 周一
  203. *
  204. * @time 2020年07月04日
  205. * @return Frequencies
  206. */
  207. public function mondays()
  208. {
  209. return $this->days(1);
  210. }
  211. /**
  212. * 周二
  213. *
  214. * @return $this
  215. */
  216. public function tuesdays()
  217. {
  218. return $this->days(2);
  219. }
  220. /**
  221. * 周三
  222. *
  223. * @return $this
  224. */
  225. public function wednesdays()
  226. {
  227. return $this->days(3);
  228. }
  229. /**
  230. * 周四
  231. *
  232. * @return $this
  233. */
  234. public function thursdays()
  235. {
  236. return $this->days(4);
  237. }
  238. /**
  239. * 周五
  240. *
  241. * @return $this
  242. */
  243. public function fridays()
  244. {
  245. return $this->days(5);
  246. }
  247. /**
  248. * 周六
  249. *
  250. * @return $this
  251. */
  252. public function saturdays()
  253. {
  254. return $this->days(6);
  255. }
  256. /**
  257. * 周日
  258. *
  259. * @return $this
  260. */
  261. public function sundays()
  262. {
  263. return $this->days(0);
  264. }
  265. /**
  266. * 每周
  267. *
  268. * @time 2020年07月04日
  269. * @return Frequencies
  270. */
  271. public function weekly()
  272. {
  273. return $this->spliceIntoPosition(1, 0)
  274. ->spliceIntoPosition(2, 0)
  275. ->spliceIntoPosition(5, 0);
  276. }
  277. /**
  278. * 每周的某个时间
  279. *
  280. * @param int $day
  281. * @param string $time
  282. * @return $this
  283. */
  284. public function weeklyOn($day, $time = '0:0')
  285. {
  286. $this->dailyAt($time);
  287. return $this->spliceIntoPosition(5, $day);
  288. }
  289. /**
  290. * 每月的某天某个时间
  291. *
  292. * @time 2020年07月04日
  293. * @param int $day
  294. * @param string $time
  295. * @return Frequencies
  296. */
  297. public function monthlyOn($day = 1, $time = '0:0')
  298. {
  299. $this->dailyAt($time);
  300. return $this->spliceIntoPosition(3, $day);
  301. }
  302. /**
  303. * 每月两次
  304. *
  305. * @time 2020年07月04日
  306. * @param int $first
  307. * @param int $second
  308. * @return Frequencies
  309. */
  310. public function twiceMonthly($first = 1, $second = 16)
  311. {
  312. $days = $first.','.$second;
  313. return $this->spliceIntoPosition(1, 0)
  314. ->spliceIntoPosition(2, 0)
  315. ->spliceIntoPosition(3, $days);
  316. }
  317. /**
  318. * 每月
  319. *
  320. * @time 2020年07月04日
  321. * @return Frequencies
  322. */
  323. public function monthly()
  324. {
  325. return $this->spliceIntoPosition(1, 0)
  326. ->spliceIntoPosition(2, 0)
  327. ->spliceIntoPosition(3, 1);
  328. }
  329. /**
  330. * 每个季度
  331. *
  332. * @time 2020年07月04日
  333. * @return Frequencies
  334. */
  335. public function quarterly()
  336. {
  337. return $this->spliceIntoPosition(1, 0)
  338. ->spliceIntoPosition(2, 0)
  339. ->spliceIntoPosition(3, 1)
  340. ->spliceIntoPosition(4, '1-12/3');
  341. }
  342. /**
  343. * 一周中的几天运行
  344. *
  345. * @time 2020年07月04日
  346. * @param $days
  347. * @return Frequencies
  348. */
  349. public function days($days)
  350. {
  351. $days = is_array($days) ? $days : func_get_args();
  352. return $this->spliceIntoPosition(5, implode(',', $days));
  353. }
  354. /**
  355. * 每年
  356. *
  357. * @time 2020年07月04日
  358. * @return Frequencies
  359. */
  360. public function yearly()
  361. {
  362. return $this->spliceIntoPosition(1, 0)
  363. ->spliceIntoPosition(2, 0)
  364. ->spliceIntoPosition(3, 1)
  365. ->spliceIntoPosition(4, 1);
  366. }
  367. /**
  368. * Splice the given value into the given position of the expression.
  369. *
  370. * @param int $position
  371. * @param string $value
  372. * @return $this
  373. */
  374. protected function spliceIntoPosition($position, $value)
  375. {
  376. $segments = explode(' ', $this->expression);
  377. $segments[$position - 1] = $value;
  378. return $this->cron(implode(' ', $segments));
  379. }
  380. /**
  381. * call
  382. *
  383. * @time 2020年07月04日
  384. * @param $name
  385. * @param $arguments
  386. * @return $this
  387. */
  388. public function __call($name, $arguments)
  389. {
  390. if (Str::contains($name, 'every')) {
  391. $num = (int)Str::substr(str_replace('every', '',$name), 0, 2);
  392. if (Str::contains($name, 'second')) {
  393. return $this->spliceIntoPosition(1, $num < 60 ? $num : 1);
  394. }
  395. if (Str::contains($name, 'minute')) {
  396. return $this->spliceIntoPosition(2, $num < 60 ? $num : 1);
  397. }
  398. if (Str::contains($name, 'hour')) {
  399. return $this->spliceIntoPosition(3, $num < 24 ? $num : 1);
  400. }
  401. if (Str::contains($name, 'day')) {
  402. return $this->spliceIntoPosition(4, $num < 31 ? $num : 1);
  403. }
  404. if (Str::contains($name, 'month')) {
  405. return $this->spliceIntoPosition(5, $num < 12 ? $num : 1);
  406. }
  407. }
  408. // other to do
  409. return $this;
  410. }
  411. }