From e238299f12ee91a65ac021feca29b870b05f5dd7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 13 Dec 2016 15:57:19 -0600 Subject: [PATCH] move frequency helpers into a trait --- src/Illuminate/Console/Scheduling/Event.php | 356 +---------------- .../Console/Scheduling/ManagesFrequencies.php | 362 ++++++++++++++++++ 2 files changed, 364 insertions(+), 354 deletions(-) create mode 100644 src/Illuminate/Console/Scheduling/ManagesFrequencies.php diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 9cac80f249f9..a37f5d5bf9be 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -15,6 +15,8 @@ class Event { + use ManagesFrequencies; + /** * The cache store implementation. * @@ -337,331 +339,6 @@ public function runsInMaintenanceMode() return $this->evenInMaintenanceMode; } - /** - * 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 hourly. - * - * @return $this - */ - public function hourly() - { - return $this->spliceIntoPosition(1, 0); - } - - /** - * 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 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 quarterly. - * - * @return $this - */ - public function quarterly() - { - return $this->spliceIntoPosition(1, 0) - ->spliceIntoPosition(2, 0) - ->spliceIntoPosition(3, 1) - ->spliceIntoPosition(4, '*/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); - } - - /** - * 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 thirty minutes. - * - * @return $this - */ - public function everyThirtyMinutes() - { - return $this->spliceIntoPosition(1, '0,30'); - } - - /** - * 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)); - } - - /** - * 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) { - $now = Carbon::now()->getTimestamp(); - - return $now >= strtotime($startTime) && $now <= strtotime($endTime); - }; - } - /** * State that the command should run in background. * @@ -674,19 +351,6 @@ public function runInBackground() return $this; } - /** - * Set the timezone the date should be evaluated on. - * - * @param \DateTimeZone|string $timezone - * @return $this - */ - public function timezone($timezone) - { - $this->timezone = $timezone; - - return $this; - } - /** * Set which user the command should run as. * @@ -953,22 +617,6 @@ public function description($description) 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)); - } - /** * Get the summary of the event for display. * diff --git a/src/Illuminate/Console/Scheduling/ManagesFrequencies.php b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php new file mode 100644 index 000000000000..980614dddde2 --- /dev/null +++ b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php @@ -0,0 +1,362 @@ +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) { + $now = Carbon::now()->getTimestamp(); + + return $now >= strtotime($startTime) && $now <= strtotime($endTime); + }; + } + + /** + * Schedule the event to run hourly. + * + * @return $this + */ + public function hourly() + { + return $this->spliceIntoPosition(1, 0); + } + + /** + * 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 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 quarterly. + * + * @return $this + */ + public function quarterly() + { + return $this->spliceIntoPosition(1, 0) + ->spliceIntoPosition(2, 0) + ->spliceIntoPosition(3, 1) + ->spliceIntoPosition(4, '*/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); + } + + /** + * 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 thirty minutes. + * + * @return $this + */ + public function everyThirtyMinutes() + { + return $this->spliceIntoPosition(1, '0,30'); + } + + /** + * 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)); + } +}