ManagesFrequencies.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. namespace Illuminate\Console\Scheduling;
  3. use Illuminate\Support\Carbon;
  4. trait ManagesFrequencies
  5. {
  6. /**
  7. * The Cron expression representing the event's frequency.
  8. *
  9. * @param string $expression
  10. * @return $this
  11. */
  12. public function cron($expression)
  13. {
  14. $this->expression = $expression;
  15. return $this;
  16. }
  17. /**
  18. * Schedule the event to run between start and end time.
  19. *
  20. * @param string $startTime
  21. * @param string $endTime
  22. * @return $this
  23. */
  24. public function between($startTime, $endTime)
  25. {
  26. return $this->when($this->inTimeInterval($startTime, $endTime));
  27. }
  28. /**
  29. * Schedule the event to not run between start and end time.
  30. *
  31. * @param string $startTime
  32. * @param string $endTime
  33. * @return $this
  34. */
  35. public function unlessBetween($startTime, $endTime)
  36. {
  37. return $this->skip($this->inTimeInterval($startTime, $endTime));
  38. }
  39. /**
  40. * Schedule the event to run between start and end time.
  41. *
  42. * @param string $startTime
  43. * @param string $endTime
  44. * @return \Closure
  45. */
  46. private function inTimeInterval($startTime, $endTime)
  47. {
  48. return function () use ($startTime, $endTime) {
  49. return Carbon::now($this->timezone)->between(
  50. Carbon::parse($startTime, $this->timezone),
  51. Carbon::parse($endTime, $this->timezone),
  52. true
  53. );
  54. };
  55. }
  56. /**
  57. * Schedule the event to run every minute.
  58. *
  59. * @return $this
  60. */
  61. public function everyMinute()
  62. {
  63. return $this->spliceIntoPosition(1, '*');
  64. }
  65. /**
  66. * Schedule the event to run every five minutes.
  67. *
  68. * @return $this
  69. */
  70. public function everyFiveMinutes()
  71. {
  72. return $this->spliceIntoPosition(1, '*/5');
  73. }
  74. /**
  75. * Schedule the event to run every ten minutes.
  76. *
  77. * @return $this
  78. */
  79. public function everyTenMinutes()
  80. {
  81. return $this->spliceIntoPosition(1, '*/10');
  82. }
  83. /**
  84. * Schedule the event to run every fifteen minutes.
  85. *
  86. * @return $this
  87. */
  88. public function everyFifteenMinutes()
  89. {
  90. return $this->spliceIntoPosition(1, '*/15');
  91. }
  92. /**
  93. * Schedule the event to run every thirty minutes.
  94. *
  95. * @return $this
  96. */
  97. public function everyThirtyMinutes()
  98. {
  99. return $this->spliceIntoPosition(1, '0,30');
  100. }
  101. /**
  102. * Schedule the event to run hourly.
  103. *
  104. * @return $this
  105. */
  106. public function hourly()
  107. {
  108. return $this->spliceIntoPosition(1, 0);
  109. }
  110. /**
  111. * Schedule the event to run hourly at a given offset in the hour.
  112. *
  113. * @param int $offset
  114. * @return $this
  115. */
  116. public function hourlyAt($offset)
  117. {
  118. return $this->spliceIntoPosition(1, $offset);
  119. }
  120. /**
  121. * Schedule the event to run daily.
  122. *
  123. * @return $this
  124. */
  125. public function daily()
  126. {
  127. return $this->spliceIntoPosition(1, 0)
  128. ->spliceIntoPosition(2, 0);
  129. }
  130. /**
  131. * Schedule the command at a given time.
  132. *
  133. * @param string $time
  134. * @return $this
  135. */
  136. public function at($time)
  137. {
  138. return $this->dailyAt($time);
  139. }
  140. /**
  141. * Schedule the event to run daily at a given time (10:00, 19:30, etc).
  142. *
  143. * @param string $time
  144. * @return $this
  145. */
  146. public function dailyAt($time)
  147. {
  148. $segments = explode(':', $time);
  149. return $this->spliceIntoPosition(2, (int) $segments[0])
  150. ->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0');
  151. }
  152. /**
  153. * Schedule the event to run twice daily.
  154. *
  155. * @param int $first
  156. * @param int $second
  157. * @return $this
  158. */
  159. public function twiceDaily($first = 1, $second = 13)
  160. {
  161. $hours = $first.','.$second;
  162. return $this->spliceIntoPosition(1, 0)
  163. ->spliceIntoPosition(2, $hours);
  164. }
  165. /**
  166. * Schedule the event to run only on weekdays.
  167. *
  168. * @return $this
  169. */
  170. public function weekdays()
  171. {
  172. return $this->spliceIntoPosition(5, '1-5');
  173. }
  174. /**
  175. * Schedule the event to run only on weekends.
  176. *
  177. * @return $this
  178. */
  179. public function weekends()
  180. {
  181. return $this->spliceIntoPosition(5, '0,6');
  182. }
  183. /**
  184. * Schedule the event to run only on Mondays.
  185. *
  186. * @return $this
  187. */
  188. public function mondays()
  189. {
  190. return $this->days(1);
  191. }
  192. /**
  193. * Schedule the event to run only on Tuesdays.
  194. *
  195. * @return $this
  196. */
  197. public function tuesdays()
  198. {
  199. return $this->days(2);
  200. }
  201. /**
  202. * Schedule the event to run only on Wednesdays.
  203. *
  204. * @return $this
  205. */
  206. public function wednesdays()
  207. {
  208. return $this->days(3);
  209. }
  210. /**
  211. * Schedule the event to run only on Thursdays.
  212. *
  213. * @return $this
  214. */
  215. public function thursdays()
  216. {
  217. return $this->days(4);
  218. }
  219. /**
  220. * Schedule the event to run only on Fridays.
  221. *
  222. * @return $this
  223. */
  224. public function fridays()
  225. {
  226. return $this->days(5);
  227. }
  228. /**
  229. * Schedule the event to run only on Saturdays.
  230. *
  231. * @return $this
  232. */
  233. public function saturdays()
  234. {
  235. return $this->days(6);
  236. }
  237. /**
  238. * Schedule the event to run only on Sundays.
  239. *
  240. * @return $this
  241. */
  242. public function sundays()
  243. {
  244. return $this->days(0);
  245. }
  246. /**
  247. * Schedule the event to run weekly.
  248. *
  249. * @return $this
  250. */
  251. public function weekly()
  252. {
  253. return $this->spliceIntoPosition(1, 0)
  254. ->spliceIntoPosition(2, 0)
  255. ->spliceIntoPosition(5, 0);
  256. }
  257. /**
  258. * Schedule the event to run weekly on a given day and time.
  259. *
  260. * @param int $day
  261. * @param string $time
  262. * @return $this
  263. */
  264. public function weeklyOn($day, $time = '0:0')
  265. {
  266. $this->dailyAt($time);
  267. return $this->spliceIntoPosition(5, $day);
  268. }
  269. /**
  270. * Schedule the event to run monthly.
  271. *
  272. * @return $this
  273. */
  274. public function monthly()
  275. {
  276. return $this->spliceIntoPosition(1, 0)
  277. ->spliceIntoPosition(2, 0)
  278. ->spliceIntoPosition(3, 1);
  279. }
  280. /**
  281. * Schedule the event to run monthly on a given day and time.
  282. *
  283. * @param int $day
  284. * @param string $time
  285. * @return $this
  286. */
  287. public function monthlyOn($day = 1, $time = '0:0')
  288. {
  289. $this->dailyAt($time);
  290. return $this->spliceIntoPosition(3, $day);
  291. }
  292. /**
  293. * Schedule the event to run twice monthly.
  294. *
  295. * @param int $first
  296. * @param int $second
  297. * @return $this
  298. */
  299. public function twiceMonthly($first = 1, $second = 16)
  300. {
  301. $days = $first.','.$second;
  302. return $this->spliceIntoPosition(1, 0)
  303. ->spliceIntoPosition(2, 0)
  304. ->spliceIntoPosition(3, $days);
  305. }
  306. /**
  307. * Schedule the event to run quarterly.
  308. *
  309. * @return $this
  310. */
  311. public function quarterly()
  312. {
  313. return $this->spliceIntoPosition(1, 0)
  314. ->spliceIntoPosition(2, 0)
  315. ->spliceIntoPosition(3, 1)
  316. ->spliceIntoPosition(4, '1-12/3');
  317. }
  318. /**
  319. * Schedule the event to run yearly.
  320. *
  321. * @return $this
  322. */
  323. public function yearly()
  324. {
  325. return $this->spliceIntoPosition(1, 0)
  326. ->spliceIntoPosition(2, 0)
  327. ->spliceIntoPosition(3, 1)
  328. ->spliceIntoPosition(4, 1);
  329. }
  330. /**
  331. * Set the days of the week the command should run on.
  332. *
  333. * @param array|mixed $days
  334. * @return $this
  335. */
  336. public function days($days)
  337. {
  338. $days = is_array($days) ? $days : func_get_args();
  339. return $this->spliceIntoPosition(5, implode(',', $days));
  340. }
  341. /**
  342. * Set the timezone the date should be evaluated on.
  343. *
  344. * @param \DateTimeZone|string $timezone
  345. * @return $this
  346. */
  347. public function timezone($timezone)
  348. {
  349. $this->timezone = $timezone;
  350. return $this;
  351. }
  352. /**
  353. * Splice the given value into the given position of the expression.
  354. *
  355. * @param int $position
  356. * @param string $value
  357. * @return $this
  358. */
  359. protected function spliceIntoPosition($position, $value)
  360. {
  361. $segments = explode(' ', $this->expression);
  362. $segments[$position - 1] = $value;
  363. return $this->cron(implode(' ', $segments));
  364. }
  365. }