ConsoleOutput.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2015 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Migration;
  8. use OCP\Migration\IOutput;
  9. use Symfony\Component\Console\Helper\ProgressBar;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. /**
  12. * Class SimpleOutput
  13. *
  14. * Just a simple IOutput implementation with writes messages to the log file.
  15. * Alternative implementations will write to the console or to the web ui (web update case)
  16. *
  17. * @package OC\Migration
  18. */
  19. class ConsoleOutput implements IOutput {
  20. private ?ProgressBar $progressBar = null;
  21. public function __construct(
  22. private OutputInterface $output,
  23. ) {
  24. }
  25. public function debug(string $message): void {
  26. $this->output->writeln($message, OutputInterface::VERBOSITY_VERBOSE);
  27. }
  28. /**
  29. * @param string $message
  30. */
  31. public function info($message): void {
  32. $this->output->writeln("<info>$message</info>");
  33. }
  34. /**
  35. * @param string $message
  36. */
  37. public function warning($message): void {
  38. $this->output->writeln("<comment>$message</comment>");
  39. }
  40. /**
  41. * @param int $max
  42. */
  43. public function startProgress($max = 0): void {
  44. if (!is_null($this->progressBar)) {
  45. $this->progressBar->finish();
  46. }
  47. $this->progressBar = new ProgressBar($this->output);
  48. $this->progressBar->start($max);
  49. }
  50. /**
  51. * @param int $step
  52. * @param string $description
  53. */
  54. public function advance($step = 1, $description = ''): void {
  55. if (is_null($this->progressBar)) {
  56. $this->progressBar = new ProgressBar($this->output);
  57. $this->progressBar->start();
  58. }
  59. $this->progressBar->advance($step);
  60. if (!is_null($description)) {
  61. $this->output->write(" $description");
  62. }
  63. }
  64. public function finishProgress(): void {
  65. if (is_null($this->progressBar)) {
  66. return;
  67. }
  68. $this->progressBar->finish();
  69. }
  70. }