Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command;
  24. use Symfony\Component\Console\Command\Command;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Input\InputOption;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class Base extends Command {
  29. const OUTPUT_FORMAT_PLAIN = 'plain';
  30. const OUTPUT_FORMAT_JSON = 'json';
  31. const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
  32. protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
  33. /** @var boolean */
  34. private $php_pcntl_signal = false;
  35. /** @var boolean */
  36. private $interrupted = false;
  37. protected function configure() {
  38. $this
  39. ->addOption(
  40. 'output',
  41. null,
  42. InputOption::VALUE_OPTIONAL,
  43. 'Output format (plain, json or json_pretty, default is plain)',
  44. $this->defaultOutputFormat
  45. )
  46. ;
  47. }
  48. /**
  49. * @param InputInterface $input
  50. * @param OutputInterface $output
  51. * @param array $items
  52. * @param string $prefix
  53. */
  54. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') {
  55. switch ($input->getOption('output')) {
  56. case self::OUTPUT_FORMAT_JSON:
  57. $output->writeln(json_encode($items));
  58. break;
  59. case self::OUTPUT_FORMAT_JSON_PRETTY:
  60. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  61. break;
  62. default:
  63. foreach ($items as $key => $item) {
  64. if (is_array($item)) {
  65. $output->writeln($prefix . $key . ':');
  66. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  67. continue;
  68. }
  69. if (!is_int($key)) {
  70. $value = $this->valueToString($item);
  71. if (!is_null($value)) {
  72. $output->writeln($prefix . $key . ': ' . $value);
  73. } else {
  74. $output->writeln($prefix . $key);
  75. }
  76. } else {
  77. $output->writeln($prefix . $this->valueToString($item));
  78. }
  79. }
  80. break;
  81. }
  82. }
  83. /**
  84. * @param InputInterface $input
  85. * @param OutputInterface $output
  86. * @param mixed $item
  87. */
  88. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  89. if (is_array($item)) {
  90. $this->writeArrayInOutputFormat($input, $output, $item, '');
  91. return;
  92. }
  93. switch ($input->getOption('output')) {
  94. case self::OUTPUT_FORMAT_JSON:
  95. $output->writeln(json_encode($item));
  96. break;
  97. case self::OUTPUT_FORMAT_JSON_PRETTY:
  98. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  99. break;
  100. default:
  101. $output->writeln($this->valueToString($item, false));
  102. break;
  103. }
  104. }
  105. protected function valueToString($value, $returnNull = true) {
  106. if ($value === false) {
  107. return 'false';
  108. } else if ($value === true) {
  109. return 'true';
  110. } else if ($value === null) {
  111. return ($returnNull) ? null : 'null';
  112. } else {
  113. return $value;
  114. }
  115. }
  116. /**
  117. * @return bool
  118. */
  119. protected function hasBeenInterrupted() {
  120. // return always false if pcntl_signal functions are not accessible
  121. if ($this->php_pcntl_signal) {
  122. pcntl_signal_dispatch();
  123. return $this->interrupted;
  124. } else {
  125. return false;
  126. }
  127. }
  128. /**
  129. * Changes the status of the command to "interrupted" if ctrl-c has been pressed
  130. *
  131. * Gives a chance to the command to properly terminate what it's doing
  132. */
  133. protected function cancelOperation() {
  134. $this->interrupted = true;
  135. }
  136. public function run(InputInterface $input, OutputInterface $output) {
  137. // check if the php pcntl_signal functions are accessible
  138. $this->php_pcntl_signal = function_exists('pcntl_signal');
  139. if ($this->php_pcntl_signal) {
  140. // Collect interrupts and notify the running command
  141. pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
  142. pcntl_signal(SIGINT, [$this, 'cancelOperation']);
  143. }
  144. return parent::run($input, $output);
  145. }
  146. }