123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- <?php
- // +----------------------------------------------------------------------
- // | CatchAdmin [Just Like ~ ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
- // +----------------------------------------------------------------------
- // | Author: JaguarJack [ njphper@gmail.com ]
- // +----------------------------------------------------------------------
- namespace catcher\library\crontab;
- use think\helper\Str;
- /**
- * From Laravel
- *
- * Trait ManagesFrequencies
- * @package catcher\library\crontab
- */
- trait Frequencies
- {
- /**
- * The Cron expression representing the event's frequency.
- *
- * @param string $expression
- * @return $this
- */
- public function cron($expression)
- {
- $this->expression = $expression;
- return $this;
- }
- /**
- * 每十秒
- *
- * @time 2020年07月04日
- * @return $this
- */
- public function everyTenSeconds()
- {
- $this->second = 10;
- return $this;
- }
- /**
- * 每二十秒
- *
- * @time 2020年07月04日
- * @return $this
- */
- public function everyTwentySeconds()
- {
- $this->second = 20;
- return $this;
- }
- /**
- * 每三十秒
- *
- * @time 2020年07月04日
- * @return $this
- */
- public function everyThirtySeconds()
- {
- $this->second = 30;
- return $this;
- }
- /**
- * 每分钟
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function everyMinute()
- {
- return $this->spliceIntoPosition(1, '*');
- }
- /**
- * 5 分钟
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function everyFiveMinutes()
- {
- return $this->spliceIntoPosition(1, '*/5');
- }
- /**
- * 10 分钟
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function everyTenMinutes()
- {
- return $this->spliceIntoPosition(1, '*/10');
- }
- /**
- * 15 分钟
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function everyFifteenMinutes()
- {
- return $this->spliceIntoPosition(1, '*/15');
- }
- /**
- * 三十分钟
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function everyThirtyMinutes()
- {
- return $this->spliceIntoPosition(1, '0,30');
- }
- /**
- * 每小时
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function hourly()
- {
- return $this->spliceIntoPosition(1, 0);
- }
- /**
- * 小时的时间
- *
- * @param array|int $offset
- * @return $this
- */
- public function hourlyAt($offset)
- {
- $offset = is_array($offset) ? implode(',', $offset) : $offset;
- return $this->spliceIntoPosition(1, $offset);
- }
- /**
- * 每天
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function daily()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0);
- }
- /**
- * 每天固定时间启动
- *
- * @param string $time
- * @return $this
- */
- public function at($time)
- {
- return $this->dailyAt($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');
- }
- /**
- * 每两天一次
- *
- * @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);
- }
- /**
- * 工作日跑
- *
- * @return $this
- */
- public function weekdays()
- {
- return $this->spliceIntoPosition(5, '1-5');
- }
- /**
- * 周末
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function weekends()
- {
- return $this->spliceIntoPosition(5, '0,6');
- }
- /**
- * 周一
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function mondays()
- {
- return $this->days(1);
- }
- /**
- * 周二
- *
- * @return $this
- */
- public function tuesdays()
- {
- return $this->days(2);
- }
- /**
- * 周三
- *
- * @return $this
- */
- public function wednesdays()
- {
- return $this->days(3);
- }
- /**
- * 周四
- *
- * @return $this
- */
- public function thursdays()
- {
- return $this->days(4);
- }
- /**
- * 周五
- *
- * @return $this
- */
- public function fridays()
- {
- return $this->days(5);
- }
- /**
- * 周六
- *
- * @return $this
- */
- public function saturdays()
- {
- return $this->days(6);
- }
- /**
- * 周日
- *
- * @return $this
- */
- public function sundays()
- {
- return $this->days(0);
- }
- /**
- * 每周
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function weekly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(5, 0);
- }
- /**
- * 每周的某个时间
- *
- * @param int $day
- * @param string $time
- * @return $this
- */
- public function weeklyOn($day, $time = '0:0')
- {
- $this->dailyAt($time);
- return $this->spliceIntoPosition(5, $day);
- }
- /**
- * 每月的某天某个时间
- *
- * @time 2020年07月04日
- * @param int $day
- * @param string $time
- * @return Frequencies
- */
- public function monthlyOn($day = 1, $time = '0:0')
- {
- $this->dailyAt($time);
- return $this->spliceIntoPosition(3, $day);
- }
- /**
- * 每月两次
- *
- * @time 2020年07月04日
- * @param int $first
- * @param int $second
- * @return Frequencies
- */
- public function twiceMonthly($first = 1, $second = 16)
- {
- $days = $first.','.$second;
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, $days);
- }
- /**
- * 每月
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function monthly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1);
- }
- /**
- * 每个季度
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function quarterly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1)
- ->spliceIntoPosition(4, '1-12/3');
- }
- /**
- * 一周中的几天运行
- *
- * @time 2020年07月04日
- * @param $days
- * @return Frequencies
- */
- public function days($days)
- {
- $days = is_array($days) ? $days : func_get_args();
- return $this->spliceIntoPosition(5, implode(',', $days));
- }
- /**
- * 每年
- *
- * @time 2020年07月04日
- * @return Frequencies
- */
- public function yearly()
- {
- return $this->spliceIntoPosition(1, 0)
- ->spliceIntoPosition(2, 0)
- ->spliceIntoPosition(3, 1)
- ->spliceIntoPosition(4, 1);
- }
- /**
- * 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));
- }
- /**
- * call
- *
- * @time 2020年07月04日
- * @param $name
- * @param $arguments
- * @return $this
- */
- public function __call($name, $arguments)
- {
- if (Str::contains($name, 'every')) {
- $num = (int)Str::substr(str_replace('every', '',$name), 0, 2);
- if (Str::contains($name, 'second')) {
- return $this->spliceIntoPosition(1, $num < 60 ? $num : 1);
- }
- if (Str::contains($name, 'minute')) {
- return $this->spliceIntoPosition(2, $num < 60 ? $num : 1);
- }
- if (Str::contains($name, 'hour')) {
- return $this->spliceIntoPosition(3, $num < 24 ? $num : 1);
- }
- if (Str::contains($name, 'day')) {
- return $this->spliceIntoPosition(4, $num < 31 ? $num : 1);
- }
- if (Str::contains($name, 'month')) {
- return $this->spliceIntoPosition(5, $num < 12 ? $num : 1);
- }
- }
- // other to do
- return $this;
- }
- }
|