ConsoleOutput.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017, ownCloud GmbH
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Migration;
  23. use OCP\Migration\IOutput;
  24. use Symfony\Component\Console\Helper\ProgressBar;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. /**
  27. * Class SimpleOutput
  28. *
  29. * Just a simple IOutput implementation with writes messages to the log file.
  30. * Alternative implementations will write to the console or to the web ui (web update case)
  31. *
  32. * @package OC\Migration
  33. */
  34. class ConsoleOutput implements IOutput {
  35. private ?ProgressBar $progressBar = null;
  36. public function __construct(
  37. private OutputInterface $output,
  38. ) {
  39. }
  40. public function debug(string $message): void {
  41. $this->output->writeln($message, OutputInterface::VERBOSITY_VERBOSE);
  42. }
  43. /**
  44. * @param string $message
  45. */
  46. public function info($message): void {
  47. $this->output->writeln("<info>$message</info>");
  48. }
  49. /**
  50. * @param string $message
  51. */
  52. public function warning($message): void {
  53. $this->output->writeln("<comment>$message</comment>");
  54. }
  55. /**
  56. * @param int $max
  57. */
  58. public function startProgress($max = 0): void {
  59. if (!is_null($this->progressBar)) {
  60. $this->progressBar->finish();
  61. }
  62. $this->progressBar = new ProgressBar($this->output);
  63. $this->progressBar->start($max);
  64. }
  65. /**
  66. * @param int $step
  67. * @param string $description
  68. */
  69. public function advance($step = 1, $description = ''): void {
  70. if (is_null($this->progressBar)) {
  71. $this->progressBar = new ProgressBar($this->output);
  72. $this->progressBar->start();
  73. }
  74. $this->progressBar->advance($step);
  75. if (!is_null($description)) {
  76. $this->output->write(" $description");
  77. }
  78. }
  79. public function finishProgress(): void {
  80. if (is_null($this->progressBar)) {
  81. return;
  82. }
  83. $this->progressBar->finish();
  84. }
  85. }