ManagesFrequencies.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <?php
  2. namespace service\cron;
  3. use Jenssegers\Date\Date;
  4. //共用的一些属性和方法提取出来做来公共trait类
  5. trait ManagesFrequencies
  6. {
  7. /**
  8. * 设置任务执行周期
  9. *
  10. * @param string $expression
  11. * @return $this
  12. */
  13. public function expression($expression)
  14. {
  15. $this->expression = $expression;
  16. return $this;
  17. }
  18. /**
  19. * 设置区间时间
  20. *
  21. * @param string $startTime
  22. * @param string $endTime
  23. * @return $this
  24. */
  25. public function between($startTime, $endTime)
  26. {
  27. return $this->when($this->inTimeInterval($startTime, $endTime));
  28. }
  29. /**
  30. * 排除区间时间
  31. *
  32. * @param string $startTime
  33. * @param string $endTime
  34. * @return $this
  35. */
  36. public function unlessBetween($startTime, $endTime)
  37. {
  38. return $this->skip($this->inTimeInterval($startTime, $endTime));
  39. }
  40. private function inTimeInterval($startTime, $endTime)
  41. {
  42. return function () use ($startTime, $endTime) {
  43. return Date::now($this->timezone)->between(
  44. Date::parse($startTime, $this->timezone),
  45. Date::parse($endTime, $this->timezone),
  46. true
  47. );
  48. };
  49. }
  50. /**
  51. * 按小时执行
  52. *
  53. * @return $this
  54. */
  55. public function hourly()
  56. {
  57. return $this->spliceIntoPosition(1, 0);
  58. }
  59. /**
  60. * 按小时延期执行
  61. *
  62. * @param int $offset
  63. * @return $this
  64. */
  65. public function hourlyAt($offset)
  66. {
  67. return $this->spliceIntoPosition(1, $offset);
  68. }
  69. /**
  70. * 按天执行
  71. *
  72. * @return $this
  73. */
  74. public function daily()
  75. {
  76. return $this->spliceIntoPosition(1, 0)
  77. ->spliceIntoPosition(2, 0);
  78. }
  79. /**
  80. * 指定时间执行
  81. *
  82. * @param string $time
  83. * @return $this
  84. */
  85. public function at($time)
  86. {
  87. return $this->dailyAt($time);
  88. }
  89. /**
  90. * 指定时间执行
  91. *
  92. * @param string $time
  93. * @return $this
  94. */
  95. public function dailyAt($time)
  96. {
  97. $segments = explode(':', $time);
  98. return $this->spliceIntoPosition(2, (int) $segments[0])
  99. ->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0');
  100. }
  101. /**
  102. * 每天执行两次
  103. *
  104. * @param int $first
  105. * @param int $second
  106. * @return $this
  107. */
  108. public function twiceDaily($first = 1, $second = 13)
  109. {
  110. $hours = $first . ',' . $second;
  111. return $this->spliceIntoPosition(1, 0)
  112. ->spliceIntoPosition(2, $hours);
  113. }
  114. /**
  115. * 工作日执行
  116. *
  117. * @return $this
  118. */
  119. public function weekdays()
  120. {
  121. return $this->spliceIntoPosition(5, '1-5');
  122. }
  123. /**
  124. * 周末执行
  125. *
  126. * @return $this
  127. */
  128. public function weekends()
  129. {
  130. return $this->spliceIntoPosition(5, '0,6');
  131. }
  132. /**
  133. * 星期一执行
  134. *
  135. * @return $this
  136. */
  137. public function mondays()
  138. {
  139. return $this->days(1);
  140. }
  141. /**
  142. * 星期二执行
  143. *
  144. * @return $this
  145. */
  146. public function tuesdays()
  147. {
  148. return $this->days(2);
  149. }
  150. /**
  151. * 星期三执行
  152. *
  153. * @return $this
  154. */
  155. public function wednesdays()
  156. {
  157. return $this->days(3);
  158. }
  159. /**
  160. * 星期四执行
  161. *
  162. * @return $this
  163. */
  164. public function thursdays()
  165. {
  166. return $this->days(4);
  167. }
  168. /**
  169. * 星期五执行
  170. *
  171. * @return $this
  172. */
  173. public function fridays()
  174. {
  175. return $this->days(5);
  176. }
  177. /**
  178. * 星期六执行
  179. *
  180. * @return $this
  181. */
  182. public function saturdays()
  183. {
  184. return $this->days(6);
  185. }
  186. /**
  187. * 星期天执行
  188. *
  189. * @return $this
  190. */
  191. public function sundays()
  192. {
  193. return $this->days(0);
  194. }
  195. /**
  196. * 按周执行
  197. *
  198. * @return $this
  199. */
  200. public function weekly()
  201. {
  202. return $this->spliceIntoPosition(1, 0)
  203. ->spliceIntoPosition(2, 0)
  204. ->spliceIntoPosition(5, 0);
  205. }
  206. /**
  207. * 指定每周的时间执行
  208. *
  209. * @param int $day
  210. * @param string $time
  211. * @return $this
  212. */
  213. public function weeklyOn($day, $time = '0:0')
  214. {
  215. $this->dailyAt($time);
  216. return $this->spliceIntoPosition(5, $day);
  217. }
  218. /**
  219. * 按月执行
  220. *
  221. * @return $this
  222. */
  223. public function monthly()
  224. {
  225. return $this->spliceIntoPosition(1, 0)
  226. ->spliceIntoPosition(2, 0)
  227. ->spliceIntoPosition(3, 1);
  228. }
  229. /**
  230. * 指定每月的执行时间
  231. *
  232. * @param int $day
  233. * @param string $time
  234. * @return $this
  235. */
  236. public function monthlyOn($day = 1, $time = '0:0')
  237. {
  238. $this->dailyAt($time);
  239. return $this->spliceIntoPosition(3, $day);
  240. }
  241. /**
  242. * 每月执行两次
  243. *
  244. * @param int $first
  245. * @param int $second
  246. * @return $this
  247. */
  248. public function twiceMonthly($first = 1, $second = 16)
  249. {
  250. $days = $first . ',' . $second;
  251. return $this->spliceIntoPosition(1, 0)
  252. ->spliceIntoPosition(2, 0)
  253. ->spliceIntoPosition(3, $days);
  254. }
  255. /**
  256. * 按季度执行
  257. *
  258. * @return $this
  259. */
  260. public function quarterly()
  261. {
  262. return $this->spliceIntoPosition(1, 0)
  263. ->spliceIntoPosition(2, 0)
  264. ->spliceIntoPosition(3, 1)
  265. ->spliceIntoPosition(4, '*/3');
  266. }
  267. /**
  268. * 按年执行
  269. *
  270. * @return $this
  271. */
  272. public function yearly()
  273. {
  274. return $this->spliceIntoPosition(1, 0)
  275. ->spliceIntoPosition(2, 0)
  276. ->spliceIntoPosition(3, 1)
  277. ->spliceIntoPosition(4, 1);
  278. }
  279. /**
  280. * 每分钟执行
  281. *
  282. * @return $this
  283. */
  284. public function everyMinute()
  285. {
  286. return $this->spliceIntoPosition(1, '*');
  287. }
  288. /**
  289. * 每5分钟执行
  290. *
  291. * @return $this
  292. */
  293. public function everyFiveMinutes()
  294. {
  295. return $this->spliceIntoPosition(1, '*/5');
  296. }
  297. /**
  298. * 每10分钟执行
  299. *
  300. * @return $this
  301. */
  302. public function everyTenMinutes()
  303. {
  304. return $this->spliceIntoPosition(1, '*/10');
  305. }
  306. /**
  307. * 每30分钟执行
  308. *
  309. * @return $this
  310. */
  311. public function everyThirtyMinutes()
  312. {
  313. return $this->spliceIntoPosition(1, '0,30');
  314. }
  315. /**
  316. * 按周设置天执行
  317. *
  318. * @param array|mixed $days
  319. * @return $this
  320. */
  321. public function days($days)
  322. {
  323. $days = is_array($days) ? $days : func_get_args();
  324. return $this->spliceIntoPosition(5, implode(',', $days));
  325. }
  326. /**
  327. * 设置时区
  328. *
  329. * @param \DateTimeZone|string $timezone
  330. * @return $this
  331. */
  332. public function timezone($timezone)
  333. {
  334. $this->timezone = $timezone;
  335. return $this;
  336. }
  337. protected function spliceIntoPosition($position, $value)
  338. {
  339. $segments = explode(' ', $this->expression);
  340. $segments[$position - 1] = $value;
  341. return $this->expression(implode(' ', $segments));
  342. }
  343. }