Base.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 OC\Core\Command\User\ListCommand;
  25. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  26. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Input\InputOption;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class Base extends Command implements CompletionAwareInterface {
  32. const OUTPUT_FORMAT_PLAIN = 'plain';
  33. const OUTPUT_FORMAT_JSON = 'json';
  34. const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
  35. protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
  36. /** @var boolean */
  37. private $php_pcntl_signal = false;
  38. /** @var boolean */
  39. private $interrupted = false;
  40. protected function configure() {
  41. $this
  42. ->addOption(
  43. 'output',
  44. null,
  45. InputOption::VALUE_OPTIONAL,
  46. 'Output format (plain, json or json_pretty, default is plain)',
  47. $this->defaultOutputFormat
  48. )
  49. ;
  50. }
  51. /**
  52. * @param InputInterface $input
  53. * @param OutputInterface $output
  54. * @param array $items
  55. * @param string $prefix
  56. */
  57. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') {
  58. switch ($input->getOption('output')) {
  59. case self::OUTPUT_FORMAT_JSON:
  60. $output->writeln(json_encode($items));
  61. break;
  62. case self::OUTPUT_FORMAT_JSON_PRETTY:
  63. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  64. break;
  65. default:
  66. foreach ($items as $key => $item) {
  67. if (is_array($item)) {
  68. $output->writeln($prefix . $key . ':');
  69. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  70. continue;
  71. }
  72. if (!is_int($key) || ListCommand::class === get_class($this)) {
  73. $value = $this->valueToString($item);
  74. if (!is_null($value)) {
  75. $output->writeln($prefix . $key . ': ' . $value);
  76. } else {
  77. $output->writeln($prefix . $key);
  78. }
  79. } else {
  80. $output->writeln($prefix . $this->valueToString($item));
  81. }
  82. }
  83. break;
  84. }
  85. }
  86. /**
  87. * @param InputInterface $input
  88. * @param OutputInterface $output
  89. * @param mixed $item
  90. */
  91. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  92. if (is_array($item)) {
  93. $this->writeArrayInOutputFormat($input, $output, $item, '');
  94. return;
  95. }
  96. switch ($input->getOption('output')) {
  97. case self::OUTPUT_FORMAT_JSON:
  98. $output->writeln(json_encode($item));
  99. break;
  100. case self::OUTPUT_FORMAT_JSON_PRETTY:
  101. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  102. break;
  103. default:
  104. $output->writeln($this->valueToString($item, false));
  105. break;
  106. }
  107. }
  108. protected function valueToString($value, $returnNull = true) {
  109. if ($value === false) {
  110. return 'false';
  111. } else if ($value === true) {
  112. return 'true';
  113. } else if ($value === null) {
  114. return $returnNull ? null : 'null';
  115. } else {
  116. return $value;
  117. }
  118. }
  119. /**
  120. * Throw InterruptedException when interrupted by user
  121. *
  122. * @throws InterruptedException
  123. */
  124. protected function abortIfInterrupted() {
  125. if ($this->php_pcntl_signal === false) {
  126. return;
  127. }
  128. pcntl_signal_dispatch();
  129. if ($this->interrupted === true) {
  130. throw new InterruptedException('Command interrupted by user');
  131. }
  132. }
  133. /**
  134. * Changes the status of the command to "interrupted" if ctrl-c has been pressed
  135. *
  136. * Gives a chance to the command to properly terminate what it's doing
  137. */
  138. protected function cancelOperation() {
  139. $this->interrupted = true;
  140. }
  141. public function run(InputInterface $input, OutputInterface $output) {
  142. // check if the php pcntl_signal functions are accessible
  143. $this->php_pcntl_signal = function_exists('pcntl_signal');
  144. if ($this->php_pcntl_signal) {
  145. // Collect interrupts and notify the running command
  146. pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
  147. pcntl_signal(SIGINT, [$this, 'cancelOperation']);
  148. }
  149. return parent::run($input, $output);
  150. }
  151. /**
  152. * @param string $optionName
  153. * @param CompletionContext $context
  154. * @return string[]
  155. */
  156. public function completeOptionValues($optionName, CompletionContext $context) {
  157. if ($optionName === 'output') {
  158. return ['plain', 'json', 'json_pretty'];
  159. }
  160. return [];
  161. }
  162. /**
  163. * @param string $argumentName
  164. * @param CompletionContext $context
  165. * @return string[]
  166. */
  167. public function completeArgumentValues($argumentName, CompletionContext $context) {
  168. return [];
  169. }
  170. }