Base.php 5.3 KB

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