staticSiteBackupManager = $staticSiteBackupManager; } protected function configure(): void { $this ->addOption( // this is the name that users must type to pass this option (e.g. --iterations=5) 'number', // this is the optional shortcut of the option name, which usually is just a letter // (e.g. `i`, so users pass it as `-i`); use it for commonly used options // or options with long names null, // this is the type of option (e.g. requires a value, can be passed more than once, etc.) InputOption::VALUE_OPTIONAL, // the option description displayed when showing the command help 'Number of backup to restore', // the default value of the option (for those which allow to pass values) '' ) ->addOption( // this is the name that users must type to pass this option (e.g. --iterations=5) 'date', // this is the optional shortcut of the option name, which usually is just a letter // (e.g. `i`, so users pass it as `-i`); use it for commonly used options // or options with long names null, // this is the type of option (e.g. requires a value, can be passed more than once, etc.) InputOption::VALUE_OPTIONAL, // the option description displayed when showing the command help 'Date of backup to restore', // the default value of the option (for those which allow to pass values) '' ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); if($this->staticSiteBackupManager->staticSiteBuilder->isRunning()) { $io->error('Static site builder is running.'); return Command::FAILURE; } $date = $input->getOption('date'); $number = $input->getOption('number'); $backups = []; foreach (glob($this->staticSiteBackupManager->backupPath . '*') as $filename) { if (is_dir($filename)) { $pathinfo = pathinfo($filename); $backups[] = $pathinfo['basename']; } } if ($date != '' && in_array($date, $backups)) { if ($this->staticSiteBackupManager->restore($date)) { $io->success('Static site restore is complete.'); return Command::SUCCESS; } else { $io->error('Static site restore failed.'); return Command::FAILURE; } } elseif ($number != '' && isset($backups[(int)$number])) { $date = $backups[(int)$number]; if ($this->staticSiteBackupManager->restore($date)) { $io->success('Static site restore is complete.'); return Command::SUCCESS; } else { $io->error('Static site restore failed.'); return Command::FAILURE; } } elseif (count($backups) > 0) { $output->writeln('Available backups: '); foreach ($backups as $key => $value) { $output->writeln('[' .$key . '] : ' . $value); } $output->writeln('To restore, add --date Y-m-d_H-i-s'); return Command::SUCCESS; } else { $io->error('No backups to restore.'); return Command::FAILURE; } } }