Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Date-specific enums and constants #53983

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"src/Illuminate/Filesystem/functions.php",
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Log/functions.php",
"src/Illuminate/Support/Date/constants.php",
"src/Illuminate/Support/functions.php",
"src/Illuminate/Support/helpers.php"
],
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use function Illuminate\Support\enum_value;

use const Illuminate\Support\Date\SECONDS_PER_MINUTE;

class RateLimiter
{
use InteractsWithTime;
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)
{
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)
{
$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)
{
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\{HOURS_PER_DAY, MINUTES_PER_HOUR, SECONDS_PER_MINUTE};

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)
{
$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);
}

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

/**
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);
shaedrich marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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 \Illuminate\Support\Date\Day[]|\Illuminate\Support\Date\Day|array|int<0, 6>|int<0, 6>[]|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<1, 12>|\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<0, 6>[]|int<0, 6>|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_MINUTE * $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
shaedrich marked this conversation as resolved.
Show resolved Hide resolved
{
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(__NAMESPACE__.'\\'.($constant = 'SECONDS_PER_MINUTE'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MINUTES_PER_HOUR'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'HOURS_PER_DAY'))) {
define(__NAMESPACE__.'\\'.$constant, 24);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'DAYS_PER_WEEK'))) {
define(__NAMESPACE__.'\\'.$constant, 7);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MONTHS_PER_YEAR'))) {
define(__NAMESPACE__.'\\'.$constant, 12);
}
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Facades/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@
* @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<0, 6>|int<0, 6>[]|\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')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes lastDayOfMonth(string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes quarterly()
* @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 yearlyOn(int<1, 12>|\Illuminate\Support\Date\Month $month = 1, int|string $dayOfMonth = 1, string $time = '0:0')
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes days(array|\Illuminate\Support\Date\Day|Illuminate\Support\Date\Day[]|int<0, 6>|int<0, 6>[]|string|string[]|mixed $days)
* @method static \Illuminate\Console\Scheduling\PendingEventAttributes timezone(\DateTimeZone|string $timezone)
*
* @see \Illuminate\Console\Scheduling\Schedule
Expand Down