Base.php 5.1 KB

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