From 88cc256c9c11e5dc8275fa0fb259eac3075faa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20H=C3=A4drich?= <11225821+shaedrich@users.noreply.github.com> Date: Thu, 19 Dec 2024 21:53:15 +0100 Subject: [PATCH] Date-specific enums and constants --- src/Illuminate/Cache/RateLimiter.php | 8 +++-- src/Illuminate/Cache/RateLimiting/Limit.php | 12 ++++--- .../Console/Scheduling/ManagesFrequencies.php | 34 +++++++++++-------- src/Illuminate/Log/LogManager.php | 4 ++- .../Routing/Middleware/ThrottleRequests.php | 4 ++- src/Illuminate/Support/Date/Day.php | 14 ++++++++ src/Illuminate/Support/Date/Month.php | 19 +++++++++++ src/Illuminate/Support/Date/constants.php | 23 +++++++++++++ src/Illuminate/Support/Facades/Schedule.php | 4 +-- 9 files changed, 96 insertions(+), 26 deletions(-) create mode 100644 src/Illuminate/Support/Date/Day.php create mode 100644 src/Illuminate/Support/Date/Month.php create mode 100644 src/Illuminate/Support/Date/constants.php diff --git a/src/Illuminate/Cache/RateLimiter.php b/src/Illuminate/Cache/RateLimiter.php index 8d8afe30a14c..6e65b4f12a54 100644 --- a/src/Illuminate/Cache/RateLimiter.php +++ b/src/Illuminate/Cache/RateLimiter.php @@ -7,6 +7,8 @@ use Illuminate\Support\Collection; use Illuminate\Support\InteractsWithTime; +use const Illuminate\Support\Date\SECONDS_PER_MINUTE; + use function Illuminate\Support\enum_value; class RateLimiter @@ -144,7 +146,7 @@ public function tooManyAttempts($key, $maxAttempts) * @param int $decaySeconds * @return int */ - public function hit($key, $decaySeconds = 60) + public function hit($key, $decaySeconds = SECONDS_PER_MINUTE) { return $this->increment($key, $decaySeconds); } @@ -157,7 +159,7 @@ public function hit($key, $decaySeconds = 60) * @param int $amount * @return int */ - public function increment($key, $decaySeconds = 60, $amount = 1) + public function increment($key, $decaySeconds = SECONDS_PER_MINUTE, $amount = 1) { $key = $this->cleanRateLimiterKey($key); @@ -184,7 +186,7 @@ public function increment($key, $decaySeconds = 60, $amount = 1) * @param int $amount * @return int */ - public function decrement($key, $decaySeconds = 60, $amount = 1) + public function decrement($key, $decaySeconds = SECONDS_PER_MINUTE, $amount = 1) { return $this->increment($key, $decaySeconds, $amount * -1); } diff --git a/src/Illuminate/Cache/RateLimiting/Limit.php b/src/Illuminate/Cache/RateLimiting/Limit.php index ed4c20258fe2..b8deae8ca624 100644 --- a/src/Illuminate/Cache/RateLimiting/Limit.php +++ b/src/Illuminate/Cache/RateLimiting/Limit.php @@ -2,6 +2,8 @@ namespace Illuminate\Cache\RateLimiting; +use const Illuminate\Support\Date\{SECONDS_PER_MINUTE, MINUTES_PER_HOUR, HOURS_PER_DAY}; + class Limit { /** @@ -40,7 +42,7 @@ class Limit * @param int $decaySeconds * @return void */ - public function __construct($key = '', int $maxAttempts = 60, int $decaySeconds = 60) + public function __construct($key = '', int $maxAttempts = 60, int $decaySeconds = SECONDS_PER_MINUTE) { $this->key = $key; $this->maxAttempts = $maxAttempts; @@ -68,7 +70,7 @@ public static function perSecond($maxAttempts, $decaySeconds = 1) */ public static function perMinute($maxAttempts, $decayMinutes = 1) { - return new static('', $maxAttempts, 60 * $decayMinutes); + return new static('', $maxAttempts, MINUTES_PER_HOUR * $decayMinutes); } /** @@ -80,7 +82,7 @@ public static function perMinute($maxAttempts, $decayMinutes = 1) */ public static function perMinutes($decayMinutes, $maxAttempts) { - return new static('', $maxAttempts, 60 * $decayMinutes); + return new static('', $maxAttempts, MINUTES_PER_HOUR * $decayMinutes); } /** @@ -92,7 +94,7 @@ public static function perMinutes($decayMinutes, $maxAttempts) */ public static function perHour($maxAttempts, $decayHours = 1) { - return new static('', $maxAttempts, 60 * 60 * $decayHours); + return new static('', $maxAttempts, SECONDS_PER_MINUTE * MINUTES_PER_HOUR * $decayHours); } /** @@ -104,7 +106,7 @@ public static function perHour($maxAttempts, $decayHours = 1) */ public static function perDay($maxAttempts, $decayDays = 1) { - return new static('', $maxAttempts, 60 * 60 * 24 * $decayDays); + return new static('', $maxAttempts, SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY * $decayDays); } /** diff --git a/src/Illuminate/Console/Scheduling/ManagesFrequencies.php b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php index d974a91b769e..0e471e648340 100644 --- a/src/Illuminate/Console/Scheduling/ManagesFrequencies.php +++ b/src/Illuminate/Console/Scheduling/ManagesFrequencies.php @@ -3,8 +3,12 @@ namespace Illuminate\Console\Scheduling; use Illuminate\Support\Carbon; +use Illuminate\Support\Date\Day; +use Illuminate\Support\Date\Month; use InvalidArgumentException; +use function Illuminate\Support\enum_value; + trait ManagesFrequencies { /** @@ -401,7 +405,7 @@ protected function hourBasedSchedule($minutes, $hours) */ public function weekdays() { - return $this->days(Schedule::MONDAY.'-'.Schedule::FRIDAY); + return $this->days(Day::Monday->value.'-'.Day::Friday->value); } /** @@ -411,7 +415,7 @@ public function weekdays() */ public function weekends() { - return $this->days(Schedule::SATURDAY.','.Schedule::SUNDAY); + return $this->days(Day::Saturday->value.','.Day::Sunday->value); } /** @@ -421,7 +425,7 @@ public function weekends() */ public function mondays() { - return $this->days(Schedule::MONDAY); + return $this->days(Day::MONDAY); } /** @@ -431,7 +435,7 @@ public function mondays() */ public function tuesdays() { - return $this->days(Schedule::TUESDAY); + return $this->days(Day::TUESDAY); } /** @@ -441,7 +445,7 @@ public function tuesdays() */ public function wednesdays() { - return $this->days(Schedule::WEDNESDAY); + return $this->days(Day::WEDNESDAY); } /** @@ -451,7 +455,7 @@ public function wednesdays() */ public function thursdays() { - return $this->days(Schedule::THURSDAY); + return $this->days(Day::THURSDAY); } /** @@ -461,7 +465,7 @@ public function thursdays() */ public function fridays() { - return $this->days(Schedule::FRIDAY); + return $this->days(Day::FRIDAY); } /** @@ -471,7 +475,7 @@ public function fridays() */ public function saturdays() { - return $this->days(Schedule::SATURDAY); + return $this->days(Day::SATURDAY); } /** @@ -481,7 +485,7 @@ public function saturdays() */ public function sundays() { - return $this->days(Schedule::SUNDAY); + return $this->days(Day::SUNDAY); } /** @@ -499,7 +503,7 @@ public function weekly() /** * Schedule the event to run weekly on a given day and time. * - * @param array|mixed $dayOfWeek + * @param Day[]|Day|array|int|int[]|mixed $dayOfWeek * @param string $time * @return $this */ @@ -610,7 +614,7 @@ public function yearly() /** * Schedule the event to run yearly on a given month, day, and time. * - * @param int $month + * @param int|\Illuminate\Support\Date\Month $month * @param int|string $dayOfMonth * @param string $time * @return $this @@ -620,19 +624,21 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0') $this->dailyAt($time); return $this->spliceIntoPosition(3, $dayOfMonth) - ->spliceIntoPosition(4, $month); + ->spliceIntoPosition(4, enum_value($month)); } /** * Set the days of the week the command should run on. * - * @param array|mixed $days + * @param \Illuminate\Support\Date\Day[]|\Illuminate\Support\Date\Day|array|int[]|int|string[]|string|mixed $days * @return $this */ public function days($days) { $days = is_array($days) ? $days : func_get_args(); + $days = array_map(enum_value(...), $days); + return $this->spliceIntoPosition(5, implode(',', $days)); } @@ -660,7 +666,7 @@ protected function spliceIntoPosition($position, $value) { $segments = preg_split("/\s+/", $this->expression); - $segments[$position - 1] = $value; + $segments[$position - 1] = enum_value($value); return $this->cron(implode(' ', $segments)); } diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index 7563e35cb837..4c9330469b02 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -23,6 +23,8 @@ use Psr\Log\LoggerInterface; use Throwable; +use const Illuminate\Support\Date\DAYS_PER_WEEK; + /** * @mixin \Illuminate\Log\Logger */ @@ -328,7 +330,7 @@ protected function createDailyDriver(array $config) { return new Monolog($this->parseChannel($config), [ $this->prepareHandler(new RotatingFileHandler( - $config['path'], $config['days'] ?? 7, $this->level($config), + $config['path'], $config['days'] ?? DAYS_PER_WEEK, $this->level($config), $config['bubble'] ?? true, $config['permission'] ?? null, $config['locking'] ?? false ), $config), ], $config['replace_placeholders'] ?? false ? [new PsrLogMessageProcessor()] : []); diff --git a/src/Illuminate/Routing/Middleware/ThrottleRequests.php b/src/Illuminate/Routing/Middleware/ThrottleRequests.php index d9859558c30c..049dea8923cf 100644 --- a/src/Illuminate/Routing/Middleware/ThrottleRequests.php +++ b/src/Illuminate/Routing/Middleware/ThrottleRequests.php @@ -13,6 +13,8 @@ use RuntimeException; use Symfony\Component\HttpFoundation\Response; +use const Illuminate\Support\Date\SECONDS_PER_MINUTE; + class ThrottleRequests { use InteractsWithTime; @@ -96,7 +98,7 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes (object) [ 'key' => $prefix.$this->resolveRequestSignature($request), 'maxAttempts' => $this->resolveMaxAttempts($request, $maxAttempts), - 'decaySeconds' => 60 * $decayMinutes, + 'decaySeconds' => SECONDS_PER_DAY * $decayMinutes, 'responseCallback' => null, ], ] diff --git a/src/Illuminate/Support/Date/Day.php b/src/Illuminate/Support/Date/Day.php new file mode 100644 index 000000000000..be71427bed53 --- /dev/null +++ b/src/Illuminate/Support/Date/Day.php @@ -0,0 +1,14 @@ +