base.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Core\Command;
  22. use Symfony\Component\Console\Command\Command;
  23. use Symfony\Component\Console\Input\InputInterface;
  24. use Symfony\Component\Console\Input\InputOption;
  25. use Symfony\Component\Console\Output\OutputInterface;
  26. class Base extends Command {
  27. const OUTPUT_FORMAT_PLAIN = 'plain';
  28. const OUTPUT_FORMAT_JSON = 'json';
  29. const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
  30. protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
  31. protected function configure() {
  32. $this
  33. ->addOption(
  34. 'output',
  35. null,
  36. InputOption::VALUE_OPTIONAL,
  37. 'Output format (plain, json or json_pretty, default is plain)',
  38. $this->defaultOutputFormat
  39. )
  40. ;
  41. }
  42. /**
  43. * @param InputInterface $input
  44. * @param OutputInterface $output
  45. * @param array $items
  46. * @param string $prefix
  47. */
  48. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') {
  49. switch ($input->getOption('output')) {
  50. case self::OUTPUT_FORMAT_JSON:
  51. $output->writeln(json_encode($items));
  52. break;
  53. case self::OUTPUT_FORMAT_JSON_PRETTY:
  54. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  55. break;
  56. default:
  57. foreach ($items as $key => $item) {
  58. if (is_array($item)) {
  59. $output->writeln($prefix . $key . ':');
  60. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  61. continue;
  62. }
  63. if (!is_int($key)) {
  64. $value = $this->valueToString($item);
  65. if (!is_null($value)) {
  66. $output->writeln($prefix . $key . ': ' . $value);
  67. } else {
  68. $output->writeln($prefix . $key);
  69. }
  70. } else {
  71. $output->writeln($prefix . $this->valueToString($item));
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. /**
  78. * @param InputInterface $input
  79. * @param OutputInterface $output
  80. * @param mixed $item
  81. */
  82. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  83. if (is_array($item)) {
  84. $this->writeArrayInOutputFormat($input, $output, $item, '');
  85. return;
  86. }
  87. switch ($input->getOption('output')) {
  88. case self::OUTPUT_FORMAT_JSON:
  89. $output->writeln(json_encode($item));
  90. break;
  91. case self::OUTPUT_FORMAT_JSON_PRETTY:
  92. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  93. break;
  94. default:
  95. $output->writeln($this->valueToString($item, false));
  96. break;
  97. }
  98. }
  99. protected function valueToString($value, $returnNull = true) {
  100. if ($value === false) {
  101. return 'false';
  102. } else if ($value === true) {
  103. return 'true';
  104. } else if ($value === null) {
  105. return ($returnNull) ? null : 'null';
  106. } else {
  107. return $value;
  108. }
  109. }
  110. }