Skip to content

Commit

Permalink
Date-specific enums and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich authored Dec 19, 2024
1 parent 5744f80 commit 88cc256
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 26 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;

use const Illuminate\Support\Date\SECONDS_PER_MINUTE;

Check failure on line 10 in src/Illuminate/Cache/RateLimiter.php

View workflow job for this annotation

GitHub Actions / Source Code

Used constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.

use function Illuminate\Support\enum_value;

class RateLimiter
Expand Down Expand Up @@ -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)

Check failure on line 149 in src/Illuminate/Cache/RateLimiter.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.
{
return $this->increment($key, $decaySeconds);
}
Expand All @@ -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)

Check failure on line 162 in src/Illuminate/Cache/RateLimiter.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.
{
$key = $this->cleanRateLimiterKey($key);

Expand All @@ -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)

Check failure on line 189 in src/Illuminate/Cache/RateLimiter.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.
{
return $this->increment($key, $decaySeconds, $amount * -1);
}
Expand Down
12 changes: 7 additions & 5 deletions src/Illuminate/Cache/RateLimiting/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Cache\RateLimiting;

use const Illuminate\Support\Date\{SECONDS_PER_MINUTE, MINUTES_PER_HOUR, HOURS_PER_DAY};

Check failure on line 5 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Used constant Illuminate\Support\Date\HOURS_PER_DAY not found.

Check failure on line 5 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Used constant Illuminate\Support\Date\MINUTES_PER_HOUR not found.

Check failure on line 5 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Used constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.

class Limit
{
/**
Expand Down Expand Up @@ -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)

Check failure on line 45 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\SECONDS_PER_MINUTE not found.
{
$this->key = $key;
$this->maxAttempts = $maxAttempts;
Expand Down Expand Up @@ -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);

Check failure on line 73 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\MINUTES_PER_HOUR not found.
}

/**
Expand All @@ -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);

Check failure on line 85 in src/Illuminate/Cache/RateLimiting/Limit.php

View workflow job for this annotation

GitHub Actions / Source Code

Constant Illuminate\Support\Date\MINUTES_PER_HOUR not found.
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
34 changes: 20 additions & 14 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -421,7 +425,7 @@ public function weekends()
*/
public function mondays()
{
return $this->days(Schedule::MONDAY);
return $this->days(Day::MONDAY);
}

/**
Expand All @@ -431,7 +435,7 @@ public function mondays()
*/
public function tuesdays()
{
return $this->days(Schedule::TUESDAY);
return $this->days(Day::TUESDAY);
}

/**
Expand All @@ -441,7 +445,7 @@ public function tuesdays()
*/
public function wednesdays()
{
return $this->days(Schedule::WEDNESDAY);
return $this->days(Day::WEDNESDAY);
}

/**
Expand All @@ -451,7 +455,7 @@ public function wednesdays()
*/
public function thursdays()
{
return $this->days(Schedule::THURSDAY);
return $this->days(Day::THURSDAY);
}

/**
Expand All @@ -461,7 +465,7 @@ public function thursdays()
*/
public function fridays()
{
return $this->days(Schedule::FRIDAY);
return $this->days(Day::FRIDAY);
}

/**
Expand All @@ -471,7 +475,7 @@ public function fridays()
*/
public function saturdays()
{
return $this->days(Schedule::SATURDAY);
return $this->days(Day::SATURDAY);
}

/**
Expand All @@ -481,7 +485,7 @@ public function saturdays()
*/
public function sundays()
{
return $this->days(Schedule::SUNDAY);
return $this->days(Day::SUNDAY);
}

/**
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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));
}

Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Psr\Log\LoggerInterface;
use Throwable;

use const Illuminate\Support\Date\DAYS_PER_WEEK;

/**
* @mixin \Illuminate\Log\Logger
*/
Expand Down Expand Up @@ -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()] : []);
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use RuntimeException;
use Symfony\Component\HttpFoundation\Response;

use const Illuminate\Support\Date\SECONDS_PER_MINUTE;

class ThrottleRequests
{
use InteractsWithTime;
Expand Down Expand Up @@ -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,
],
]
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Support/Date/Day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Support\Date;

enum Day: int
{
case Sunday = 0;
case Monday = 1;
case Tuesday = 2;
case Wednesday = 3;
case Thursday = 4;
case Friday = 5;
case Saturday = 6;
}
19 changes: 19 additions & 0 deletions src/Illuminate/Support/Date/Month.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Support\Date;

enum Month: int
{
case January = 1;
case February = 2;
case March = 3;
case April = 4;
case May = 5;
case June = 6;
case July = 7;
case August = 8;
case September = 9;
case October = 10;
case November = 11;
case December = 12;
}
23 changes: 23 additions & 0 deletions src/Illuminate/Support/Date/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Support\Date;

if (! defined('SECONDS_PER_MINUTE')) {
define('SECONDS_PER_MINUTE', 60);
}

if (! defined('MINUTES_PER_HOUR')) {
define('MINUTES_PER_HOUR', 60);
}

if (! defined('HOURS_PER_DAY')) {
define('HOURS_PER_DAY', 24);
}

if (! defined('DAYS_PER_WEEK')) {
define('DAYS_PER_WEEK', 7);
}

if (! defined('MONTHS_PER_YEAR')) {
define('MONTHS_PER_YEAR', 12);
}
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Facades/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes saturdays()
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes sundays()
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes weekly()
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes weeklyOn(array|mixed $dayOfWeek, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes weeklyOn(array|int|int[]|\Illuminate\Support\Date\Day|Illuminate\Support\Date\Day[]|mixed $dayOfWeek, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes monthly()
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes monthlyOn(int $dayOfMonth = 1, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes twiceMonthly(int $first = 1, int $second = 16, string $time = '0:0')
Expand All @@ -80,7 +80,7 @@
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes quarterlyOn(int $dayOfQuarter = 1, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes yearly()
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes yearlyOn(int $month = 1, int|string $dayOfMonth = 1, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes days(array|mixed $days)
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes days(array|\Illuminate\Support\Date\Day|Illuminate\Support\Date\Day[]|int|int[]|string|string[]|mixed $days)
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes timezone(\DateTimeZone|string $timezone)
*
* @see \Illuminate\Console\Scheduling\Schedule
Expand Down

0 comments on commit 88cc256

Please sign in to comment.