base.php 4.4 KB

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