ConsoleOutput.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /** @var OutputInterface */
  36. private $output;
  37. /** @var ProgressBar */
  38. private $progressBar;
  39. public function __construct(OutputInterface $output) {
  40. $this->output = $output;
  41. }
  42. public function debug(string $message): void {
  43. $this->output->writeln($message, OutputInterface::VERBOSITY_VERBOSE);
  44. }
  45. /**
  46. * @param string $message
  47. */
  48. public function info($message) {
  49. $this->output->writeln("<info>$message</info>");
  50. }
  51. /**
  52. * @param string $message
  53. */
  54. public function warning($message) {
  55. $this->output->writeln("<comment>$message</comment>");
  56. }
  57. /**
  58. * @param int $max
  59. */
  60. public function startProgress($max = 0) {
  61. if (!is_null($this->progressBar)) {
  62. $this->progressBar->finish();
  63. }
  64. $this->progressBar = new ProgressBar($this->output);
  65. $this->progressBar->start($max);
  66. }
  67. /**
  68. * @param int $step
  69. * @param string $description
  70. */
  71. public function advance($step = 1, $description = '') {
  72. if (is_null($this->progressBar)) {
  73. $this->progressBar = new ProgressBar($this->output);
  74. $this->progressBar->start();
  75. }
  76. $this->progressBar->advance($step);
  77. if (!is_null($description)) {
  78. $this->output->write(" $description");
  79. }
  80. }
  81. public function finishProgress() {
  82. if (is_null($this->progressBar)) {
  83. return;
  84. }
  85. $this->progressBar->finish();
  86. }
  87. }