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

https://gitlab.com/nmhieucoder/laravel_tintuc · PHP · 527 lines · 224 code · 61 blank · 242 comment · 3 complexity · cd45b1f0c9900f21e11a6ba8677c8e38 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. $hours = $first.','.$second;
  238. return $this->spliceIntoPosition(1, 0)
  239. ->spliceIntoPosition(2, $hours);
  240. }
  241. /**
  242. * Schedule the event to run only on weekdays.
  243. *
  244. * @return $this
  245. */
  246. public function weekdays()
  247. {
  248. return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY);
  249. }
  250. /**
  251. * Schedule the event to run only on weekends.
  252. *
  253. * @return $this
  254. */
  255. public function weekends()
  256. {
  257. return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY);
  258. }
  259. /**
  260. * Schedule the event to run only on Mondays.
  261. *
  262. * @return $this
  263. */
  264. public function mondays()
  265. {
  266. return $this->days(Schedule::MONDAY);
  267. }
  268. /**
  269. * Schedule the event to run only on Tuesdays.
  270. *
  271. * @return $this
  272. */
  273. public function tuesdays()
  274. {
  275. return $this->days(Schedule::TUESDAY);
  276. }
  277. /**
  278. * Schedule the event to run only on Wednesdays.
  279. *
  280. * @return $this
  281. */
  282. public function wednesdays()
  283. {
  284. return $this->days(Schedule::WEDNESDAY);
  285. }
  286. /**
  287. * Schedule the event to run only on Thursdays.
  288. *
  289. * @return $this
  290. */
  291. public function thursdays()
  292. {
  293. return $this->days(Schedule::THURSDAY);
  294. }
  295. /**
  296. * Schedule the event to run only on Fridays.
  297. *
  298. * @return $this
  299. */
  300. public function fridays()
  301. {
  302. return $this->days(Schedule::FRIDAY);
  303. }
  304. /**
  305. * Schedule the event to run only on Saturdays.
  306. *
  307. * @return $this
  308. */
  309. public function saturdays()
  310. {
  311. return $this->days(Schedule::SATURDAY);
  312. }
  313. /**
  314. * Schedule the event to run only on Sundays.
  315. *
  316. * @return $this
  317. */
  318. public function sundays()
  319. {
  320. return $this->days(Schedule::SUNDAY);
  321. }
  322. /**
  323. * Schedule the event to run weekly.
  324. *
  325. * @return $this
  326. */
  327. public function weekly()
  328. {
  329. return $this->spliceIntoPosition(1, 0)
  330. ->spliceIntoPosition(2, 0)
  331. ->spliceIntoPosition(5, 0);
  332. }
  333. /**
  334. * Schedule the event to run weekly on a given day and time.
  335. *
  336. * @param int $dayOfWeek
  337. * @param string $time
  338. * @return $this
  339. */
  340. public function weeklyOn($dayOfWeek, $time = '0:0')
  341. {
  342. $this->dailyAt($time);
  343. return $this->days($dayOfWeek);
  344. }
  345. /**
  346. * Schedule the event to run monthly.
  347. *
  348. * @return $this
  349. */
  350. public function monthly()
  351. {
  352. return $this->spliceIntoPosition(1, 0)
  353. ->spliceIntoPosition(2, 0)
  354. ->spliceIntoPosition(3, 1);
  355. }
  356. /**
  357. * Schedule the event to run monthly on a given day and time.
  358. *
  359. * @param int $dayOfMonth
  360. * @param string $time
  361. * @return $this
  362. */
  363. public function monthlyOn($dayOfMonth = 1, $time = '0:0')
  364. {
  365. $this->dailyAt($time);
  366. return $this->spliceIntoPosition(3, $dayOfMonth);
  367. }
  368. /**
  369. * Schedule the event to run twice monthly at a given time.
  370. *
  371. * @param int $first
  372. * @param int $second
  373. * @param string $time
  374. * @return $this
  375. */
  376. public function twiceMonthly($first = 1, $second = 16, $time = '0:0')
  377. {
  378. $daysOfMonth = $first.','.$second;
  379. $this->dailyAt($time);
  380. return $this->spliceIntoPosition(3, $daysOfMonth);
  381. }
  382. /**
  383. * Schedule the event to run on the last day of the month.
  384. *
  385. * @param string $time
  386. * @return $this
  387. */
  388. public function lastDayOfMonth($time = '0:0')
  389. {
  390. $this->dailyAt($time);
  391. return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day);
  392. }
  393. /**
  394. * Schedule the event to run quarterly.
  395. *
  396. * @return $this
  397. */
  398. public function quarterly()
  399. {
  400. return $this->spliceIntoPosition(1, 0)
  401. ->spliceIntoPosition(2, 0)
  402. ->spliceIntoPosition(3, 1)
  403. ->spliceIntoPosition(4, '1-12/3');
  404. }
  405. /**
  406. * Schedule the event to run yearly.
  407. *
  408. * @return $this
  409. */
  410. public function yearly()
  411. {
  412. return $this->spliceIntoPosition(1, 0)
  413. ->spliceIntoPosition(2, 0)
  414. ->spliceIntoPosition(3, 1)
  415. ->spliceIntoPosition(4, 1);
  416. }
  417. /**
  418. * Schedule the event to run yearly on a given month, day, and time.
  419. *
  420. * @param int $month
  421. * @param int|string $dayOfMonth
  422. * @param string $time
  423. * @return $this
  424. */
  425. public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
  426. {
  427. $this->dailyAt($time);
  428. return $this->spliceIntoPosition(3, $dayOfMonth)
  429. ->spliceIntoPosition(4, $month);
  430. }
  431. /**
  432. * Set the days of the week the command should run on.
  433. *
  434. * @param array|mixed $days
  435. * @return $this
  436. */
  437. public function days($days)
  438. {
  439. $days = is_array($days) ? $days : func_get_args();
  440. return $this->spliceIntoPosition(5, implode(',', $days));
  441. }
  442. /**
  443. * Set the timezone the date should be evaluated on.
  444. *
  445. * @param \DateTimeZone|string $timezone
  446. * @return $this
  447. */
  448. public function timezone($timezone)
  449. {
  450. $this->timezone = $timezone;
  451. return $this;
  452. }
  453. /**
  454. * Splice the given value into the given position of the expression.
  455. *
  456. * @param int $position
  457. * @param string $value
  458. * @return $this
  459. */
  460. protected function spliceIntoPosition($position, $value)
  461. {
  462. $segments = explode(' ', $this->expression);
  463. $segments[$position - 1] = $value;
  464. return $this->cron(implode(' ', $segments));
  465. }
  466. }