Base.php 5.4 KB

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