Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Base.php 5.2KB

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