diff --git a/Command/ListCommand.php b/Command/ListCommand.php index 8af266aa..b4eb5a6c 100644 --- a/Command/ListCommand.php +++ b/Command/ListCommand.php @@ -23,7 +23,9 @@ class ListCommand extends Command { private ObjectManager $em; - public function __construct(ManagerRegistry $managerRegistry, private readonly DateTimeFormatter $dateTimeFormatter, string $managerName) + public function __construct(ManagerRegistry $managerRegistry, + private readonly DateTimeFormatter|null $dateTimeFormatter, + string $managerName) { $this->em = $managerRegistry->getManager($managerName); parent::__construct(); @@ -38,6 +40,17 @@ protected function configure(): void ->setHelp('This class is for listing all active commands.'); } + // https://github.com/Dukecity/CommandSchedulerBundle/issues/99 + private function formatDiff(\DateTime $dateTime): string + { + if($this->dateTimeFormatter) + { + return $this->dateTimeFormatter->formatDiff($dateTime); + } + + return $dateTime->format('Y-m-d H:i'); + } + /** * @throws \Exception */ @@ -64,11 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int }; if($nextRunDate = $command->getNextRunDate()) - {$nextRunDateText = $this->dateTimeFormatter->formatDiff($nextRunDate);} + {$nextRunDateText = $this->formatDiff($nextRunDate);} else {$nextRunDateText = "";} if($lastRunDate = $command->getLastExecution()) - {$lastRunDateText = $this->dateTimeFormatter->formatDiff($lastRunDate);} + {$lastRunDateText = $this->formatDiff($lastRunDate);} else {$lastRunDateText = "";} $table->addRow([ diff --git a/Command/MonitorCommand.php b/Command/MonitorCommand.php index fa8b6afe..1be3da03 100644 --- a/Command/MonitorCommand.php +++ b/Command/MonitorCommand.php @@ -37,7 +37,7 @@ class MonitorCommand extends Command public function __construct( private EventDispatcherInterface $eventDispatcher, ManagerRegistry $managerRegistry, - private readonly DateTimeFormatter $dateTimeFormatter, + private readonly DateTimeFormatter|null $dateTimeFormatter, string $managerName, private int | bool $lockTimeout, private array $receiver, @@ -103,6 +103,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::SUCCESS; } + // https://github.com/Dukecity/CommandSchedulerBundle/issues/99 + private function formatDiff(\DateTime $dateTime): string + { + if($this->dateTimeFormatter) + { + return $this->dateTimeFormatter->formatDiff($dateTime); + } + + return ''; + } + /** * Print a table of locked Commands to console. * @@ -133,7 +144,7 @@ private function dump(OutputInterface $output, array $failedCommands): void if($lastRunDate) { $lastRunDateText = $lastRunDate->format('Y-m-d H:i').' (' - .$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')'; + .$this->formatDiff($command->getLastExecution()).')'; } else { $lastRunDateText = ''; @@ -143,7 +154,7 @@ private function dump(OutputInterface $output, array $failedCommands): void if($nextRunDate) { $nextRunDateText = $nextRunDate->format('Y-m-d H:i').' (' - .$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')'; + .$this->formatDiff($command->getLastExecution()).')'; } else { $nextRunDateText = ''; diff --git a/Resources/config/services.php b/Resources/config/services.php index 50fbc0d3..2599ce9a 100644 --- a/Resources/config/services.php +++ b/Resources/config/services.php @@ -101,7 +101,7 @@ [ service('event_dispatcher'), service('doctrine'), - service('time.datetime_formatter'), + service('time.datetime_formatter')->nullOnInvalid(), '%dukecity_command_scheduler.doctrine_manager%', '%dukecity_command_scheduler.lock_timeout%', '%dukecity_command_scheduler.monitor_mail%', @@ -115,7 +115,7 @@ ->args( [ service('doctrine'), - service('time.datetime_formatter'), + service('time.datetime_formatter')->nullOnInvalid(), '%dukecity_command_scheduler.doctrine_manager%', ] )