From af45c97a257111a5ea104d50ef514b669a59354f Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 17:39:24 +0200 Subject: [PATCH 01/15] EventID getter and setter --- src/Event.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/Event.php b/src/Event.php index 0f67428..309efd8 100644 --- a/src/Event.php +++ b/src/Event.php @@ -50,6 +50,13 @@ class Event implements PingableInterface */ public $description; + /** + * The event's unique identifier. + * + * @var string|int + */ + public $eventID; + /** * Event generated output. * @@ -140,6 +147,13 @@ class Event implements PingableInterface */ protected $cwd; + /** + * Event source file. + * + * @var string|null + */ + protected $sourceFile; + /** * Position of cron fields. * @@ -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 $eventID + * + * @return $this + */ + public function eventID($eventID) + { + $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 + */ + public function getEventID() + { + if(empty($this->eventID)){ + return $this->eventID; + }else{ + return \md5($this->sourceFile . $this->description . $this->expression); + } + } + /** * Set the event's command. * From 4229ec0bae66911b6b85299383346b8b36313aea Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 18:15:15 +0200 Subject: [PATCH 02/15] EventID display --- .../Query/TaskInformation/TaskInformationHandler.php | 2 +- src/Console/Command/ScheduleListCommand.php | 6 +++++- src/Console/Command/ScheduleRunCommand.php | 2 +- src/Event.php | 2 +- src/Task/Loader.php | 9 ++++++++- src/Task/LoaderInterface.php | 2 +- 6 files changed, 17 insertions(+), 6 deletions(-) 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..c6d04e9 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 309efd8..c8a0f43 100644 --- a/src/Event.php +++ b/src/Event.php @@ -990,7 +990,7 @@ public function getTo(): \DateTime|string|null */ public function getEventID() { - if(empty($this->eventID)){ + if(!empty($this->eventID)){ return $this->eventID; }else{ return \md5($this->sourceFile . $this->description . $this->expression); 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; } From 69069e7985343f7382ead4c506091e710d6ebb31 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 18:41:19 +0200 Subject: [PATCH 03/15] Fix loader in test for EventID --- tests/TestCase/FakeLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 37db312cca2ee025d75ce8c02388698929f00360 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 18:44:46 +0200 Subject: [PATCH 04/15] Fixed getEventID according to PHPFixer --- src/Event.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Event.php b/src/Event.php index c8a0f43..6b1f81d 100644 --- a/src/Event.php +++ b/src/Event.php @@ -984,17 +984,17 @@ public function getTo(): \DateTime|string|null } /** - * Get the event ID + * Get the event ID. * * @return string */ public function getEventID() { - if(!empty($this->eventID)){ + if (!empty($this->eventID)) { return $this->eventID; - }else{ - return \md5($this->sourceFile . $this->description . $this->expression); } + + return \md5($this->sourceFile . $this->description . $this->expression); } /** From 8d78a989def224de1d4fada0d36978606f5835ae Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 18:49:52 +0200 Subject: [PATCH 05/15] Typo --- src/Console/Command/ScheduleListCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Command/ScheduleListCommand.php b/src/Console/Command/ScheduleListCommand.php index c6d04e9..0390d99 100644 --- a/src/Console/Command/ScheduleListCommand.php +++ b/src/Console/Command/ScheduleListCommand.php @@ -150,7 +150,7 @@ private function resolveFormat(InputInterface $input): string * array{ * number: int, * task: string, - * eventID: int|string, * + * eventID: int|string, * expression: string, * command: string, * }, From 2ab9b47c8dce603e3ca946ca50618007d73130eb Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Tue, 1 Apr 2025 19:02:03 +0200 Subject: [PATCH 06/15] Testo on EventID setter --- src/Event.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index 6b1f81d..5174bb1 100644 --- a/src/Event.php +++ b/src/Event.php @@ -902,6 +902,13 @@ public function description($description) */ public function eventID($eventID) { + if (empty($eventID)) { + throw new CrunzException('Event ID cannot be empty.'); + } + if (!\is_string($eventID) && !\is_int($eventID)) { + throw new CrunzException('Event ID must be a string or an integer.'); + } + $this->eventID = $eventID; return $this; @@ -990,7 +997,7 @@ public function getTo(): \DateTime|string|null */ public function getEventID() { - if (!empty($this->eventID)) { + if (!empty($this->eventID) && \is_string($this->eventID)) { return $this->eventID; } From 068106dff0ecbd53f79516dbe42f1f3369268777 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 12:59:13 +0200 Subject: [PATCH 07/15] Fixed according to php-cs-fixer --- src/Event.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Event.php b/src/Event.php index 5174bb1..45299e7 100644 --- a/src/Event.php +++ b/src/Event.php @@ -902,7 +902,7 @@ public function description($description) */ public function eventID($eventID) { - if (empty($eventID)) { + if (!isset($eventID) || $eventID === null || $eventID === '') { throw new CrunzException('Event ID cannot be empty.'); } if (!\is_string($eventID) && !\is_int($eventID)) { @@ -997,7 +997,7 @@ public function getTo(): \DateTime|string|null */ public function getEventID() { - if (!empty($this->eventID) && \is_string($this->eventID)) { + if (isset($this->eventID) && $this->eventID !== null && $this->eventID !== '') { return $this->eventID; } From ca7dfda414c3a8520fdadf39e5dd5964439982be Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:01:01 +0200 Subject: [PATCH 08/15] Other fix --- src/Event.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Event.php b/src/Event.php index 45299e7..86c09cd 100644 --- a/src/Event.php +++ b/src/Event.php @@ -902,7 +902,7 @@ public function description($description) */ public function eventID($eventID) { - if (!isset($eventID) || $eventID === null || $eventID === '') { + if (!isset($eventID) || null === $eventID || '' === $eventID) { throw new CrunzException('Event ID cannot be empty.'); } if (!\is_string($eventID) && !\is_int($eventID)) { @@ -997,7 +997,7 @@ public function getTo(): \DateTime|string|null */ public function getEventID() { - if (isset($this->eventID) && $this->eventID !== null && $this->eventID !== '') { + if (isset($this->eventID) && null !== $this->eventID && '' !== $this->eventID) { return $this->eventID; } From 23429f049dde857e8c870a80b2b25fb7b902f498 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:06:14 +0200 Subject: [PATCH 09/15] Other php-cs-fixer suggestion --- src/Event.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Event.php b/src/Event.php index 86c09cd..2693641 100644 --- a/src/Event.php +++ b/src/Event.php @@ -900,15 +900,8 @@ public function description($description) * * @return $this */ - public function eventID($eventID) + public function eventID($eventID = null) { - if (!isset($eventID) || null === $eventID || '' === $eventID) { - throw new CrunzException('Event ID cannot be empty.'); - } - if (!\is_string($eventID) && !\is_int($eventID)) { - throw new CrunzException('Event ID must be a string or an integer.'); - } - $this->eventID = $eventID; return $this; From 2c90e0771b1ea1022472df8a0de522a5c40dc495 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:08:39 +0200 Subject: [PATCH 10/15] Other php-cs-fixer suggestion 2 --- src/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index 2693641..5ef80d1 100644 --- a/src/Event.php +++ b/src/Event.php @@ -896,7 +896,7 @@ public function description($description) /** * Set event ID. * - * @param string $eventID + * @param string|int|null $eventID * * @return $this */ From 0beb2e21842475cabfb67a93fec6b32f6fc74117 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:10:27 +0200 Subject: [PATCH 11/15] Property Crunz\Event::$eventID type fix --- src/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index 5ef80d1..c76db04 100644 --- a/src/Event.php +++ b/src/Event.php @@ -46,7 +46,7 @@ class Event implements PingableInterface /** * The human readable description of the event. * - * @var string|null + * @var string|null|null */ public $description; From 4cb9a31b8745665008b5586e32dff782ac3e72c9 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:12:33 +0200 Subject: [PATCH 12/15] Typo --- src/Event.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Event.php b/src/Event.php index c76db04..e4bdd60 100644 --- a/src/Event.php +++ b/src/Event.php @@ -46,16 +46,16 @@ class Event implements PingableInterface /** * The human readable description of the event. * - * @var string|null|null + * @var string|null */ public $description; /** * The event's unique identifier. * - * @var string|int + * @var string|int|null */ - public $eventID; + private $eventID; /** * Event generated output. From 21d5a7c28b6e659f96028b33ee4f00ec17cc9cea Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:16:36 +0200 Subject: [PATCH 13/15] Variable definition moved --- src/Event.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Event.php b/src/Event.php index e4bdd60..e36cfd2 100644 --- a/src/Event.php +++ b/src/Event.php @@ -50,13 +50,6 @@ class Event implements PingableInterface */ public $description; - /** - * The event's unique identifier. - * - * @var string|int|null - */ - private $eventID; - /** * Event generated output. * @@ -167,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. */ From 6324a101098b8ab5e6a7564748ece6c911fb87f7 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:19:41 +0200 Subject: [PATCH 14/15] getEventID return type fix --- src/Event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event.php b/src/Event.php index e36cfd2..a580a32 100644 --- a/src/Event.php +++ b/src/Event.php @@ -986,7 +986,7 @@ public function getTo(): \DateTime|string|null /** * Get the event ID. * - * @return string + * @return string|int */ public function getEventID() { From 1473dc772cca9c49810da7720a43e113c70b7306 Mon Sep 17 00:00:00 2001 From: Luca Tacconi Date: Fri, 4 Apr 2025 13:32:05 +0200 Subject: [PATCH 15/15] Test Fix --- .../Console/Command/ScheduleListCommandTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) 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,