123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
-
- namespace Illuminate\Console\Scheduling;
-
- use Illuminate\Support\Carbon;
-
- trait ManagesFrequencies
- {
- /**
- * The Cron expression representing the event's frequency.
- *
- * @param string $expression
- * @return $this
- */
- public function cron($expression)
- {
- $this->expression = $expression;
-
- return $this;
- }
-
- /**
- * Schedule the event to run between start and end time.
- *
- * @param string $startTime
- * @param string $endTime
- * @return $this
- */
- public function between($startTime, $endTime)
- {
- return $this->when($this->inTimeInterval($startTime, $endTime));
- }
-
- /**
- * Schedule the event to not run between start and end time.
- *
- * @param string $startTime
- * @param string $endTime
- * @return $this
- */
- public function unlessBetween($startTime, $endTime)
- {
- return $this->skip($this->inTimeInterval($startTime, $endTime));
- }
-
- /**
- * Schedule the event to run between start and end time.
- *
- * @param string $startTime
- * @param string $endTime
- * @return \Closure
- */
- private function inTimeInterval($startTime, $endTime)
- {
- return function () use ($startTime, $endTime) {
- return Carbon::now($this->timezone)->between(
- Carbon::parse($startTime, $this->timezone),
- Carbon::parse($endTime, $this->timezone),
- true
- );
- };
- }
-
- /**
- * Schedule the event to run every minute.
- *
- * @return $this
- */
- public function everyMinute()
- {
- return $this->spliceIntoPosition(1, '*');
- }
-
- /**
- * Schedule the event to run every five minutes.
- *
- * @return $this
- */
- public function everyFiveMinutes()
- {
- return $this->spliceIntoPosition(1, '*/5');
- }
-
- /**
- * Schedule the event to run every ten minutes.
- *
- * @return $this
- */
- public function everyTenMinutes()
- {
- return $this->spliceIntoPosition(1, '*/10');
- }
-
- /**
- * Schedule the event to run every fifteen minutes.
- *
- * @return $this
- */
- public function everyFifteenMinutes()
- {
- return $this->spliceIntoPosition(1, '*/15');
- }
-
- /**
- * Schedule the event to run every thirty minutes.
- *
- * @return $this
- */
- public function everyThirtyMinutes()
- {
- return $this->spliceIntoPosition(1, '0,30');
- }
-
- /**
- * Schedule the event to run hourly.
- *
- * @return $this
- */
- public function hourly()
- {
- return $this->spliceIntoPosition(1, 0);
- }
-
- /**
- * Schedule the event to run hourly at a given offset in the hour.
- *
- * @param int $offset
- * @return $this
- */
- public function hourlyAt($offset)
- {
- return $this->spliceIntoPosition(1, $offset);
- }
-
- /**
- * Schedule the event to run daily.
- *
- * @return $this
- */
- public function daily()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0);
- }
-
- /**
- * Schedule the command at a given time.
- *
- * @param string $time
- * @return $this
- */
- public function at($time)
- {
- return $this->dailyAt($time);
- }
-
- /**
- * Schedule the event to run daily at a given time (10:00, 19:30, etc).
- *
- * @param string $time
- * @return $this
- */
- public function dailyAt($time)
- {
- $segments = explode(':', $time);
-
- return $this->spliceIntoPosition(2, (int) $segments[0])
- ->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0');
- }
-
- /**
- * Schedule the event to run twice daily.
- *
- * @param int $first
- * @param int $second
- * @return $this
- */
- public function twiceDaily($first = 1, $second = 13)
- {
- $hours = $first.','.$second;
-
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, $hours);
- }
-
- /**
- * Schedule the event to run only on weekdays.
- *
- * @return $this
- */
- public function weekdays()
- {
- return $this->spliceIntoPosition(5, '1-5');
- }
-
- /**
- * Schedule the event to run only on weekends.
- *
- * @return $this
- */
- public function weekends()
- {
- return $this->spliceIntoPosition(5, '0,6');
- }
-
- /**
- * Schedule the event to run only on Mondays.
- *
- * @return $this
- */
- public function mondays()
- {
- return $this->days(1);
- }
-
- /**
- * Schedule the event to run only on Tuesdays.
- *
- * @return $this
- */
- public function tuesdays()
- {
- return $this->days(2);
- }
-
- /**
- * Schedule the event to run only on Wednesdays.
- *
- * @return $this
- */
- public function wednesdays()
- {
- return $this->days(3);
- }
-
- /**
- * Schedule the event to run only on Thursdays.
- *
- * @return $this
- */
- public function thursdays()
- {
- return $this->days(4);
- }
-
- /**
- * Schedule the event to run only on Fridays.
- *
- * @return $this
- */
- public function fridays()
- {
- return $this->days(5);
- }
-
- /**
- * Schedule the event to run only on Saturdays.
- *
- * @return $this
- */
- public function saturdays()
- {
- return $this->days(6);
- }
-
- /**
- * Schedule the event to run only on Sundays.
- *
- * @return $this
- */
- public function sundays()
- {
- return $this->days(0);
- }
-
- /**
- * Schedule the event to run weekly.
- *
- * @return $this
- */
- public function weekly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(5, 0);
- }
-
- /**
- * Schedule the event to run weekly on a given day and time.
- *
- * @param int $day
- * @param string $time
- * @return $this
- */
- public function weeklyOn($day, $time = '0:0')
- {
- $this->dailyAt($time);
-
- return $this->spliceIntoPosition(5, $day);
- }
-
- /**
- * Schedule the event to run monthly.
- *
- * @return $this
- */
- public function monthly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1);
- }
-
- /**
- * Schedule the event to run monthly on a given day and time.
- *
- * @param int $day
- * @param string $time
- * @return $this
- */
- public function monthlyOn($day = 1, $time = '0:0')
- {
- $this->dailyAt($time);
-
- return $this->spliceIntoPosition(3, $day);
- }
-
- /**
- * Schedule the event to run twice monthly.
- *
- * @param int $first
- * @param int $second
- * @return $this
- */
- public function twiceMonthly($first = 1, $second = 16)
- {
- $days = $first.','.$second;
-
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, $days);
- }
-
- /**
- * Schedule the event to run quarterly.
- *
- * @return $this
- */
- public function quarterly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1)
- ->spliceIntoPosition(4, '1-12/3');
- }
-
- /**
- * Schedule the event to run yearly.
- *
- * @return $this
- */
- public function yearly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1)
- ->spliceIntoPosition(4, 1);
- }
-
- /**
- * Set the days of the week the command should run on.
- *
- * @param array|mixed $days
- * @return $this
- */
- public function days($days)
- {
- $days = is_array($days) ? $days : func_get_args();
-
- return $this->spliceIntoPosition(5, implode(',', $days));
- }
-
- /**
- * Set the timezone the date should be evaluated on.
- *
- * @param \DateTimeZone|string $timezone
- * @return $this
- */
- public function timezone($timezone)
- {
- $this->timezone = $timezone;
-
- return $this;
- }
-
- /**
- * Splice the given value into the given position of the expression.
- *
- * @param int $position
- * @param string $value
- * @return $this
- */
- protected function spliceIntoPosition($position, $value)
- {
- $segments = explode(' ', $this->expression);
-
- $segments[$position - 1] = $value;
-
- return $this->cron(implode(' ', $segments));
- }
- }
|