/vendor/laravel/framework/src/Illuminate/Console/Scheduling/ManagesFrequencies.php

https://gitlab.com/jjpa2018/dashboard · PHP · 540 lines · 228 code · 62 blank · 250 comment · 3 complexity · 00ef046bc7ca384ec48ac845788af3ed MD5 · raw file

  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. [$now, $startTime, $endTime] = [
  49. Carbon::now($this->timezone),
  50. Carbon::parse($startTime, $this->timezone),
  51. Carbon::parse($endTime, $this->timezone),
  52. ];
  53. if ($endTime->lessThan($startTime)) {
  54. if ($startTime->greaterThan($now)) {
  55. $startTime->subDay(1);
  56. } else {
  57. $endTime->addDay(1);
  58. }
  59. }
  60. return function () use ($now, $startTime, $endTime) {
  61. return $now->between($startTime, $endTime);
  62. };
  63. }
  64. /**
  65. * Schedule the event to run every minute.
  66. *
  67. * @return $this
  68. */
  69. public function everyMinute()
  70. {
  71. return $this->spliceIntoPosition(1, '*');
  72. }
  73. /**
  74. * Schedule the event to run every two minutes.
  75. *
  76. * @return $this
  77. */
  78. public function everyTwoMinutes()
  79. {
  80. return $this->spliceIntoPosition(1, '*/2');
  81. }
  82. /**
  83. * Schedule the event to run every three minutes.
  84. *
  85. * @return $this
  86. */
  87. public function everyThreeMinutes()
  88. {
  89. return $this->spliceIntoPosition(1, '*/3');
  90. }
  91. /**
  92. * Schedule the event to run every four minutes.
  93. *
  94. * @return $this
  95. */
  96. public function everyFourMinutes()
  97. {
  98. return $this->spliceIntoPosition(1, '*/4');
  99. }
  100. /**
  101. * Schedule the event to run every five minutes.
  102. *
  103. * @return $this
  104. */
  105. public function everyFiveMinutes()
  106. {
  107. return $this->spliceIntoPosition(1, '*/5');
  108. }
  109. /**
  110. * Schedule the event to run every ten minutes.
  111. *
  112. * @return $this
  113. */
  114. public function everyTenMinutes()
  115. {
  116. return $this->spliceIntoPosition(1, '*/10');
  117. }
  118. /**
  119. * Schedule the event to run every fifteen minutes.
  120. *
  121. * @return $this
  122. */
  123. public function everyFifteenMinutes()
  124. {
  125. return $this->spliceIntoPosition(1, '*/15');
  126. }
  127. /**
  128. * Schedule the event to run every thirty minutes.
  129. *
  130. * @return $this
  131. */
  132. public function everyThirtyMinutes()
  133. {
  134. return $this->spliceIntoPosition(1, '0,30');
  135. }
  136. /**
  137. * Schedule the event to run hourly.
  138. *
  139. * @return $this
  140. */
  141. public function hourly()
  142. {
  143. return $this->spliceIntoPosition(1, 0);
  144. }
  145. /**
  146. * Schedule the event to run hourly at a given offset in the hour.
  147. *
  148. * @param array|int $offset
  149. * @return $this
  150. */
  151. public function hourlyAt($offset)
  152. {
  153. $offset = is_array($offset) ? implode(',', $offset) : $offset;
  154. return $this->spliceIntoPosition(1, $offset);
  155. }
  156. /**
  157. * Schedule the event to run every two hours.
  158. *
  159. * @return $this
  160. */
  161. public function everyTwoHours()
  162. {
  163. return $this->spliceIntoPosition(1, 0)
  164. ->spliceIntoPosition(2, '*/2');
  165. }
  166. /**
  167. * Schedule the event to run every three hours.
  168. *
  169. * @return $this
  170. */
  171. public function everyThreeHours()
  172. {
  173. return $this->spliceIntoPosition(1, 0)
  174. ->spliceIntoPosition(2, '*/3');
  175. }
  176. /**
  177. * Schedule the event to run every four hours.
  178. *
  179. * @return $this
  180. */
  181. public function everyFourHours()
  182. {
  183. return $this->spliceIntoPosition(1, 0)
  184. ->spliceIntoPosition(2, '*/4');
  185. }
  186. /**
  187. * Schedule the event to run every six hours.
  188. *
  189. * @return $this
  190. */
  191. public function everySixHours()
  192. {
  193. return $this->spliceIntoPosition(1, 0)
  194. ->spliceIntoPosition(2, '*/6');
  195. }
  196. /**
  197. * Schedule the event to run daily.
  198. *
  199. * @return $this
  200. */
  201. public function daily()
  202. {
  203. return $this->spliceIntoPosition(1, 0)
  204. ->spliceIntoPosition(2, 0);
  205. }
  206. /**
  207. * Schedule the command at a given time.
  208. *
  209. * @param string $time
  210. * @return $this
  211. */
  212. public function at($time)
  213. {
  214. return $this->dailyAt($time);
  215. }
  216. /**
  217. * Schedule the event to run daily at a given time (10:00, 19:30, etc).
  218. *
  219. * @param string $time
  220. * @return $this
  221. */
  222. public function dailyAt($time)
  223. {
  224. $segments = explode(':', $time);
  225. return $this->spliceIntoPosition(2, (int) $segments[0])
  226. ->spliceIntoPosition(1, count($segments) === 2 ? (int) $segments[1] : '0');
  227. }
  228. /**
  229. * Schedule the event to run twice daily.
  230. *
  231. * @param int $first
  232. * @param int $second
  233. * @return $this
  234. */
  235. public function twiceDaily($first = 1, $second = 13)
  236. {
  237. return $this->twiceDailyAt($first, $second, 0);
  238. }
  239. /**
  240. * Schedule the event to run twice daily at a given offset.
  241. *
  242. * @param int $first
  243. * @param int $second
  244. * @param int $offset
  245. * @return $this
  246. */
  247. public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
  248. {
  249. $hours = $first.','.$second;
  250. return $this->spliceIntoPosition(1, $offset)
  251. ->spliceIntoPosition(2, $hours);
  252. }
  253. /**
  254. * Schedule the event to run only on weekdays.
  255. *
  256. * @return $this
  257. */
  258. public function weekdays()
  259. {
  260. return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY);
  261. }
  262. /**
  263. * Schedule the event to run only on weekends.
  264. *
  265. * @return $this
  266. */
  267. public function weekends()
  268. {
  269. return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY);
  270. }
  271. /**
  272. * Schedule the event to run only on Mondays.
  273. *
  274. * @return $this
  275. */
  276. public function mondays()
  277. {
  278. return $this->days(Schedule::MONDAY);
  279. }
  280. /**
  281. * Schedule the event to run only on Tuesdays.
  282. *
  283. * @return $this
  284. */
  285. public function tuesdays()
  286. {
  287. return $this->days(Schedule::TUESDAY);
  288. }
  289. /**
  290. * Schedule the event to run only on Wednesdays.
  291. *
  292. * @return $this
  293. */
  294. public function wednesdays()
  295. {
  296. return $this->days(Schedule::WEDNESDAY);
  297. }
  298. /**
  299. * Schedule the event to run only on Thursdays.
  300. *
  301. * @return $this
  302. */
  303. public function thursdays()
  304. {
  305. return $this->days(Schedule::THURSDAY);
  306. }
  307. /**
  308. * Schedule the event to run only on Fridays.
  309. *
  310. * @return $this
  311. */
  312. public function fridays()
  313. {
  314. return $this->days(Schedule::FRIDAY);
  315. }
  316. /**
  317. * Schedule the event to run only on Saturdays.
  318. *
  319. * @return $this
  320. */
  321. public function saturdays()
  322. {
  323. return $this->days(Schedule::SATURDAY);
  324. }
  325. /**
  326. * Schedule the event to run only on Sundays.
  327. *
  328. * @return $this
  329. */
  330. public function sundays()
  331. {
  332. return $this->days(Schedule::SUNDAY);
  333. }
  334. /**
  335. * Schedule the event to run weekly.
  336. *
  337. * @return $this
  338. */
  339. public function weekly()
  340. {
  341. return $this->spliceIntoPosition(1, 0)
  342. ->spliceIntoPosition(2, 0)
  343. ->spliceIntoPosition(5, 0);
  344. }
  345. /**
  346. * Schedule the event to run weekly on a given day and time.
  347. *
  348. * @param array|mixed $dayOfWeek
  349. * @param string $time
  350. * @return $this
  351. */
  352. public function weeklyOn($dayOfWeek, $time = '0:0')
  353. {
  354. $this->dailyAt($time);
  355. return $this->days($dayOfWeek);
  356. }
  357. /**
  358. * Schedule the event to run monthly.
  359. *
  360. * @return $this
  361. */
  362. public function monthly()
  363. {
  364. return $this->spliceIntoPosition(1, 0)
  365. ->spliceIntoPosition(2, 0)
  366. ->spliceIntoPosition(3, 1);
  367. }
  368. /**
  369. * Schedule the event to run monthly on a given day and time.
  370. *
  371. * @param int $dayOfMonth
  372. * @param string $time
  373. * @return $this
  374. */
  375. public function monthlyOn($dayOfMonth = 1, $time = '0:0')
  376. {
  377. $this->dailyAt($time);
  378. return $this->spliceIntoPosition(3, $dayOfMonth);
  379. }
  380. /**
  381. * Schedule the event to run twice monthly at a given time.
  382. *
  383. * @param int $first
  384. * @param int $second
  385. * @param string $time
  386. * @return $this
  387. */
  388. public function twiceMonthly($first = 1, $second = 16, $time = '0:0')
  389. {
  390. $daysOfMonth = $first.','.$second;
  391. $this->dailyAt($time);
  392. return $this->spliceIntoPosition(3, $daysOfMonth);
  393. }
  394. /**
  395. * Schedule the event to run on the last day of the month.
  396. *
  397. * @param string $time
  398. * @return $this
  399. */
  400. public function lastDayOfMonth($time = '0:0')
  401. {
  402. $this->dailyAt($time);
  403. return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day);
  404. }
  405. /**
  406. * Schedule the event to run quarterly.
  407. *
  408. * @return $this
  409. */
  410. public function quarterly()
  411. {
  412. return $this->spliceIntoPosition(1, 0)
  413. ->spliceIntoPosition(2, 0)
  414. ->spliceIntoPosition(3, 1)
  415. ->spliceIntoPosition(4, '1-12/3');
  416. }
  417. /**
  418. * Schedule the event to run yearly.
  419. *
  420. * @return $this
  421. */
  422. public function yearly()
  423. {
  424. return $this->spliceIntoPosition(1, 0)
  425. ->spliceIntoPosition(2, 0)
  426. ->spliceIntoPosition(3, 1)
  427. ->spliceIntoPosition(4, 1);
  428. }
  429. /**
  430. * Schedule the event to run yearly on a given month, day, and time.
  431. *
  432. * @param int $month
  433. * @param int|string $dayOfMonth
  434. * @param string $time
  435. * @return $this
  436. */
  437. public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
  438. {
  439. $this->dailyAt($time);
  440. return $this->spliceIntoPosition(3, $dayOfMonth)
  441. ->spliceIntoPosition(4, $month);
  442. }
  443. /**
  444. * Set the days of the week the command should run on.
  445. *
  446. * @param array|mixed $days
  447. * @return $this
  448. */
  449. public function days($days)
  450. {
  451. $days = is_array($days) ? $days : func_get_args();
  452. return $this->spliceIntoPosition(5, implode(',', $days));
  453. }
  454. /**
  455. * Set the timezone the date should be evaluated on.
  456. *
  457. * @param \DateTimeZone|string $timezone
  458. * @return $this
  459. */
  460. public function timezone($timezone)
  461. {
  462. $this->timezone = $timezone;
  463. return $this;
  464. }
  465. /**
  466. * Splice the given value into the given position of the expression.
  467. *
  468. * @param int $position
  469. * @param string $value
  470. * @return $this
  471. */
  472. protected function spliceIntoPosition($position, $value)
  473. {
  474. $segments = explode(' ', $this->expression);
  475. $segments[$position - 1] = $value;
  476. return $this->cron(implode(' ', $segments));
  477. }
  478. }