Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(TaskInformation $taskInformation): TaskInformationView

// List of schedules
$schedules = $this->taskLoader
->load(...\array_values($files))
->load($source, ...\array_values($files))
;

$timezoneForComparisons = $this->timezone
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Command/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
* number: int,
* task: string,
* expression: string,
* eventID: int|string,
* command: string,
* },
* >
Expand All @@ -104,7 +105,7 @@ private function tasks(string $source): array
->all($source)
;
$schedules = $this->taskLoader
->load(...\array_values($tasks))
->load($source, ...\array_values($tasks))
;

$tasksList = [];
Expand All @@ -116,6 +117,7 @@ private function tasks(string $source): array
$tasksList[] = [
'number' => ++$number,
'task' => $event->description ?? '',
'eventID' => $event->getEventID(),
'expression' => $event->getExpression(),
'command' => $event->getCommandForDisplay(),
];
Expand Down Expand Up @@ -148,6 +150,7 @@ private function resolveFormat(InputInterface $input): string
* array{
* number: int,
* task: string,
* eventID: int|string,
* expression: string,
* command: string,
* },
Expand All @@ -165,6 +168,7 @@ private function printList(
[
'#',
'Task',
'Event ID',
'Expression',
'Command to Run',
]
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// List of schedules
$schedules = $this->taskLoader
->load(...\array_values($files))
->load($source, ...\array_values($files))
;
$tasksTimezone = $this->taskTimezone
->timezoneForComparisons()
Expand Down
58 changes: 58 additions & 0 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ class Event implements PingableInterface
*/
protected $cwd;

/**
* Event source file.
*
* @var string|null
*/
protected $sourceFile;

/**
* Position of cron fields.
*
Expand All @@ -153,6 +160,13 @@ class Event implements PingableInterface
'week' => 5,
];

/**
* The event's unique identifier.
*
* @var string|int|null
*/
private $eventID;

/**
* Indicates if the command should not overlap itself.
*/
Expand Down Expand Up @@ -185,6 +199,22 @@ public function __construct(protected $id, $command)
$this->output = $this->getDefaultOutput();
}

/**
* Get or set the source file of the event.
*
* @param string|null $sourceFile
*
* @return string|null
*/
public function sourceFile($sourceFile)
{
if (null !== $sourceFile) {
return $this->sourceFile = $sourceFile;
}

return $this->sourceFile;
}

/**
* Change the current working directory.
*
Expand Down Expand Up @@ -863,6 +893,20 @@ public function description($description)
return $this;
}

/**
* Set event ID.
*
* @param string|int|null $eventID
*
* @return $this
*/
public function eventID($eventID = null)
{
$this->eventID = $eventID;

return $this;
}

/**
* Another way to the frequency of the cron job.
*
Expand Down Expand Up @@ -939,6 +983,20 @@ public function getTo(): \DateTime|string|null
return $this->to;
}

/**
* Get the event ID.
*
* @return string|int
*/
public function getEventID()
{
if (isset($this->eventID) && null !== $this->eventID && '' !== $this->eventID) {
return $this->eventID;
}

return \md5($this->sourceFile . $this->description . $this->expression);
}

/**
* Set the event's command.
*
Expand Down
9 changes: 8 additions & 1 deletion src/Task/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class Loader implements LoaderInterface
{
/** @return Schedule[] */
public function load(\SplFileInfo ...$files): array
public function load(string $source, \SplFileInfo ...$files): array
{
$schedules = [];
foreach ($files as $file) {
Expand All @@ -23,6 +23,13 @@ public function load(\SplFileInfo ...$files): array
throw WrongTaskInstanceException::fromFilePath($file, $schedule);
}

$events = $schedule->events();
foreach ($events as $event_key => $event) {
$events[$event_key]->sourceFile(\str_replace($source, '', $file->getRealPath()));
}

$schedule->events($events);

$schedules[] = $schedule;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Task/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
interface LoaderInterface
{
/** @return Schedule[] */
public function load(\SplFileInfo ...$files): array;
public function load(string $source, \SplFileInfo ...$files): array;
}
2 changes: 1 addition & 1 deletion tests/TestCase/FakeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(private readonly array $schedules = [])
{
}

public function load(\SplFileInfo ...$files): array
public function load(string $source, \SplFileInfo ...$files): array
{
return $this->schedules;
}
Expand Down
15 changes: 9 additions & 6 deletions tests/Unit/Console/Command/ScheduleListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ function (): array {
'format' => 'text',
'schedule' => $schedule,
'expectedOutput' => <<<TXT
+---+-------------+----------------+----------------+
| # | Task | Expression | Command to Run |
+---+-------------+----------------+----------------+
| 1 | PHP version | 15 3 * * 1,3,5 | php -v |
+---+-------------+----------------+----------------+
+---+-------------+----------------------------------+----------------+----------------+
| # | Task | Event ID | Expression | Command to Run |
+---+-------------+----------------------------------+----------------+----------------+
| 1 | PHP version | 088942db8529faec5392514970a88bfa | 15 3 * * 1,3,5 | php -v |
+---+-------------+----------------------------------+----------------+----------------+

TXT,

'assert' => static function (string $expectedOutput, string $actualOutput): void {
self::assertSame($expectedOutput, $actualOutput);
},
Expand All @@ -106,6 +107,7 @@ function (): array {
yield 'json' => [
function (): array {
$commandString = 'php -v';
$eventID = '088942db8529faec5392514970a88bfa';
$cronExpression = '15 3 * * 1,3,5';
$description = 'PHP version';
$schedule = self::createScheduleWithTask(
Expand All @@ -121,6 +123,7 @@ function (): array {
[
[
'command' => $commandString,
'eventID' => $eventID,
'expression' => $cronExpression,
'number' => 1,
'task' => $description,
Expand Down