BackupCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @package Grav\Console\Cli
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Cli;
  9. use Grav\Common\Backup\Backups;
  10. use Grav\Common\Grav;
  11. use Grav\Console\ConsoleCommand;
  12. use Symfony\Component\Console\Helper\ProgressBar;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Question\ChoiceQuestion;
  15. use Symfony\Component\Console\Style\SymfonyStyle;
  16. class BackupCommand extends ConsoleCommand
  17. {
  18. /** @var string $source */
  19. protected $source;
  20. /** @var ProgressBar $progress */
  21. protected $progress;
  22. protected function configure()
  23. {
  24. $this
  25. ->setName('backup')
  26. ->addArgument(
  27. 'id',
  28. InputArgument::OPTIONAL,
  29. 'The ID of the backup profile to perform without prompting'
  30. )
  31. ->setDescription('Creates a backup of the Grav instance')
  32. ->setHelp('The <info>backup</info> creates a zipped backup.');
  33. $this->source = getcwd();
  34. }
  35. protected function serve()
  36. {
  37. $this->progress = new ProgressBar($this->output);
  38. $this->progress->setFormat('Archiving <cyan>%current%</cyan> files [<green>%bar%</green>] <white>%percent:3s%%</white> %elapsed:6s% <yellow>%message%</yellow>');
  39. $this->progress->setBarWidth(100);
  40. Grav::instance()['config']->init();
  41. $io = new SymfonyStyle($this->input, $this->output);
  42. $io->title('Grav Backup');
  43. /** @var Backups $backups */
  44. $backups = Grav::instance()['backups'];
  45. $backups_list = $backups->getBackupProfiles();
  46. $backups_names = $backups->getBackupNames();
  47. $id = null;
  48. $inline_id = $this->input->getArgument('id');
  49. if (null !== $inline_id && is_numeric($inline_id)) {
  50. $id = $inline_id;
  51. }
  52. if (null === $id) {
  53. if (\count($backups_list) > 1) {
  54. $helper = $this->getHelper('question');
  55. $question = new ChoiceQuestion(
  56. 'Choose a backup?',
  57. $backups_names,
  58. 0
  59. );
  60. $question->setErrorMessage('Option %s is invalid.');
  61. $backup_name = $helper->ask($this->input, $this->output, $question);
  62. $id = array_search($backup_name, $backups_names, true);
  63. $io->newLine();
  64. $io->note('Selected backup: ' . $backup_name);
  65. } else {
  66. $id = 0;
  67. }
  68. }
  69. $backup = $backups->backup($id, [$this, 'outputProgress']);
  70. $io->newline(2);
  71. $io->success('Backup Successfully Created: ' . $backup);
  72. }
  73. /**
  74. * @param array $args
  75. */
  76. public function outputProgress($args)
  77. {
  78. switch ($args['type']) {
  79. case 'count':
  80. $steps = $args['steps'];
  81. $freq = (int)($steps > 100 ? round($steps / 100) : $steps);
  82. $this->progress->setMaxSteps($steps);
  83. $this->progress->setRedrawFrequency($freq);
  84. $this->progress->setMessage('Adding files...');
  85. break;
  86. case 'message':
  87. $this->progress->setMessage($args['message']);
  88. $this->progress->display();
  89. break;
  90. case 'progress':
  91. if (isset($args['complete']) && $args['complete']) {
  92. $this->progress->finish();
  93. } else {
  94. $this->progress->advance();
  95. }
  96. break;
  97. }
  98. }
  99. }