Skip to content

Commit

Permalink
Add CronExpressionPosition enum
Browse files Browse the repository at this point in the history
  • Loading branch information
shaedrich authored Dec 19, 2024
1 parent 5d81b45 commit 256dff6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 35 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Console/Enums/CronExpressionPosition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Illuminate\Console\Enums;

enum CronExpressionPosition: int
{
case Minutes = 1;
case Hours = 2;
case DayOfMonth = 3;
case Month = 4;
case DayOfWeek = 5;
}
75 changes: 40 additions & 35 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace Illuminate\Console\Scheduling;

use Illuminate\Console\Enums\CronExpressionPosition;
use Illuminate\Support\Carbon;
use InvalidArgumentException;

use function Illuminate\Support\enum_value;

trait ManagesFrequencies
{
/**
Expand Down Expand Up @@ -164,7 +167,7 @@ protected function repeatEvery($seconds)
*/
public function everyMinute()
{
return $this->spliceIntoPosition(1, '*');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*');
}

/**
Expand All @@ -174,7 +177,7 @@ public function everyMinute()
*/
public function everyTwoMinutes()
{
return $this->spliceIntoPosition(1, '*/2');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/2');
}

/**
Expand All @@ -184,7 +187,7 @@ public function everyTwoMinutes()
*/
public function everyThreeMinutes()
{
return $this->spliceIntoPosition(1, '*/3');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/3');
}

/**
Expand All @@ -194,7 +197,7 @@ public function everyThreeMinutes()
*/
public function everyFourMinutes()
{
return $this->spliceIntoPosition(1, '*/4');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/4');
}

/**
Expand All @@ -204,7 +207,7 @@ public function everyFourMinutes()
*/
public function everyFiveMinutes()
{
return $this->spliceIntoPosition(1, '*/5');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/5');
}

/**
Expand All @@ -214,7 +217,7 @@ public function everyFiveMinutes()
*/
public function everyTenMinutes()
{
return $this->spliceIntoPosition(1, '*/10');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/10');
}

/**
Expand All @@ -224,7 +227,7 @@ public function everyTenMinutes()
*/
public function everyFifteenMinutes()
{
return $this->spliceIntoPosition(1, '*/15');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/15');
}

/**
Expand All @@ -234,7 +237,7 @@ public function everyFifteenMinutes()
*/
public function everyThirtyMinutes()
{
return $this->spliceIntoPosition(1, '*/30');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, '*/30');
}

/**
Expand All @@ -244,7 +247,7 @@ public function everyThirtyMinutes()
*/
public function hourly()
{
return $this->spliceIntoPosition(1, 0);
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, 0);
}

/**
Expand Down Expand Up @@ -390,8 +393,8 @@ protected function hourBasedSchedule($minutes, $hours)

$hours = is_array($hours) ? implode(',', $hours) : $hours;

return $this->spliceIntoPosition(1, $minutes)
->spliceIntoPosition(2, $hours);
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, $minutes)
->spliceIntoPosition(CronExpressionPosition::Hours, $hours);
}

/**
Expand Down Expand Up @@ -491,9 +494,9 @@ public function sundays()
*/
public function weekly()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(5, 0);
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, 0)
->spliceIntoPosition(CronExpressionPosition::Hours, 0)
->spliceIntoPosition(CronExpressionPosition::DayOfWeek, 0);
}

/**
Expand All @@ -517,9 +520,9 @@ public function weeklyOn($dayOfWeek, $time = '0:0')
*/
public function monthly()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1);
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, 0)
->spliceIntoPosition(CronExpressionPosition::Hours, 0)
->spliceIntoPosition(CronExpressionPosition::DayOfMonth, 1);
}

/**
Expand All @@ -533,7 +536,7 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0')
{
$this->dailyAt($time);

return $this->spliceIntoPosition(3, $dayOfMonth);
return $this->spliceIntoPosition(CronExpressionPosition::DayOfMonth, $dayOfMonth);
}

/**
Expand All @@ -550,7 +553,7 @@ public function twiceMonthly($first = 1, $second = 16, $time = '0:0')

$this->dailyAt($time);

return $this->spliceIntoPosition(3, $daysOfMonth);
return $this->spliceIntoPosition(CronExpressionPosition::DayOfMonth, $daysOfMonth);
}

/**
Expand All @@ -563,7 +566,7 @@ public function lastDayOfMonth($time = '0:0')
{
$this->dailyAt($time);

return $this->spliceIntoPosition(3, Carbon::now()->endOfMonth()->day);
return $this->spliceIntoPosition(CronExpressionPosition::DayOfMonth, Carbon::now()->endOfMonth()->day);
}

/**
Expand All @@ -573,10 +576,10 @@ public function lastDayOfMonth($time = '0:0')
*/
public function quarterly()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1)
->spliceIntoPosition(4, '1-12/3');
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, 0)
->spliceIntoPosition(CronExpressionPosition::Hours, 0)
->spliceIntoPosition(CronExpressionPosition::DayOfMonth, 1)
->spliceIntoPosition(CronExpressionPosition::Month, '1-12/3');
}

/**
Expand All @@ -590,8 +593,8 @@ public function quarterlyOn($dayOfQuarter = 1, $time = '0:0')
{
$this->dailyAt($time);

return $this->spliceIntoPosition(3, $dayOfQuarter)
->spliceIntoPosition(4, '1-12/3');
return $this->spliceIntoPosition(CronExpressionPosition::DayOfMonth, $dayOfQuarter)
->spliceIntoPosition(CronExpressionPosition::Month, '1-12/3');
}

/**
Expand All @@ -601,10 +604,10 @@ public function quarterlyOn($dayOfQuarter = 1, $time = '0:0')
*/
public function yearly()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1)
->spliceIntoPosition(4, 1);
return $this->spliceIntoPosition(CronExpressionPosition::Minutes, 0)
->spliceIntoPosition(CronExpressionPosition::Hours, 0)
->spliceIntoPosition(CronExpressionPosition::DayOfMonth, 1)
->spliceIntoPosition(CronExpressionPosition::Month, 1);
}

/**
Expand All @@ -619,8 +622,8 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
{
$this->dailyAt($time);

return $this->spliceIntoPosition(3, $dayOfMonth)
->spliceIntoPosition(4, $month);
return $this->spliceIntoPosition(CronExpressionPosition::DayOfMonth, $dayOfMonth)
->spliceIntoPosition(CronExpressionPosition::Month, $month);
}

/**
Expand All @@ -633,7 +636,7 @@ public function days($days)
{
$days = is_array($days) ? $days : func_get_args();

return $this->spliceIntoPosition(5, implode(',', $days));
return $this->spliceIntoPosition(CronExpressionPosition::DayOfWeek, implode(',', $days));
}

/**
Expand All @@ -652,15 +655,17 @@ public function timezone($timezone)
/**
* Splice the given value into the given position of the expression.
*
* @param int $position
* @param int|\Illuminate\Console\Enums\CronExpressionPosition $position
* @param string $value
* @return $this
*/
protected function spliceIntoPosition($position, $value)
{
$segments = preg_split("/\s+/", $this->expression);

$segments[$position - 1] = $value;
$index = enum_value($position) - 1;

$segments[$index] = $value;

return $this->cron(implode(' ', $segments));
}
Expand Down

0 comments on commit 256dff6

Please sign in to comment.