diff --git a/src/Application/Query/TaskInformation/TaskInformationHandler.php b/src/Application/Query/TaskInformation/TaskInformationHandler.php index 9439aab..35071ce 100644 --- a/src/Application/Query/TaskInformation/TaskInformationHandler.php +++ b/src/Application/Query/TaskInformation/TaskInformationHandler.php @@ -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 diff --git a/src/Console/Command/ScheduleListCommand.php b/src/Console/Command/ScheduleListCommand.php index bc5f98b..0390d99 100644 --- a/src/Console/Command/ScheduleListCommand.php +++ b/src/Console/Command/ScheduleListCommand.php @@ -93,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int * number: int, * task: string, * expression: string, + * eventID: int|string, * command: string, * }, * > @@ -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 = []; @@ -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(), ]; @@ -148,6 +150,7 @@ private function resolveFormat(InputInterface $input): string * array{ * number: int, * task: string, + * eventID: int|string, * expression: string, * command: string, * }, @@ -165,6 +168,7 @@ private function printList( [ '#', 'Task', + 'Event ID', 'Expression', 'Command to Run', ] diff --git a/src/Console/Command/ScheduleRunCommand.php b/src/Console/Command/ScheduleRunCommand.php index cfb3873..f163493 100644 --- a/src/Console/Command/ScheduleRunCommand.php +++ b/src/Console/Command/ScheduleRunCommand.php @@ -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() diff --git a/src/Event.php b/src/Event.php index 0f67428..a580a32 100644 --- a/src/Event.php +++ b/src/Event.php @@ -140,6 +140,13 @@ class Event implements PingableInterface */ protected $cwd; + /** + * Event source file. + * + * @var string|null + */ + protected $sourceFile; + /** * Position of cron fields. * @@ -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. */ @@ -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. * @@ -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. * @@ -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. * diff --git a/src/Task/Loader.php b/src/Task/Loader.php index b200e13..6817a5a 100644 --- a/src/Task/Loader.php +++ b/src/Task/Loader.php @@ -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) { @@ -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; } diff --git a/src/Task/LoaderInterface.php b/src/Task/LoaderInterface.php index d66422e..4ed7bd2 100644 --- a/src/Task/LoaderInterface.php +++ b/src/Task/LoaderInterface.php @@ -9,5 +9,5 @@ interface LoaderInterface { /** @return Schedule[] */ - public function load(\SplFileInfo ...$files): array; + public function load(string $source, \SplFileInfo ...$files): array; } diff --git a/tests/TestCase/FakeLoader.php b/tests/TestCase/FakeLoader.php index 9c76ab9..4cb6e4f 100644 --- a/tests/TestCase/FakeLoader.php +++ b/tests/TestCase/FakeLoader.php @@ -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; } diff --git a/tests/Unit/Console/Command/ScheduleListCommandTest.php b/tests/Unit/Console/Command/ScheduleListCommandTest.php index 10f4714..5d52cba 100644 --- a/tests/Unit/Console/Command/ScheduleListCommandTest.php +++ b/tests/Unit/Console/Command/ScheduleListCommandTest.php @@ -89,13 +89,14 @@ function (): array { 'format' => 'text', 'schedule' => $schedule, 'expectedOutput' => << static function (string $expectedOutput, string $actualOutput): void { self::assertSame($expectedOutput, $actualOutput); }, @@ -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( @@ -121,6 +123,7 @@ function (): array { [ [ 'command' => $commandString, + 'eventID' => $eventID, 'expression' => $cronExpression, 'number' => 1, 'task' => $description,