You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

  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. protected function configure() {
  28. $this
  29. ->addOption(
  30. 'output',
  31. null,
  32. InputOption::VALUE_OPTIONAL,
  33. 'Output format (plain, json or json_pretty, default is plain)',
  34. 'plain'
  35. )
  36. ;
  37. }
  38. /**
  39. * @param InputInterface $input
  40. * @param OutputInterface $output
  41. * @param array $items
  42. */
  43. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) {
  44. switch ($input->getOption('output')) {
  45. case 'json':
  46. $output->writeln(json_encode($items));
  47. break;
  48. case 'json_pretty':
  49. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  50. break;
  51. default:
  52. foreach ($items as $key => $item) {
  53. if (!is_int($key)) {
  54. $value = $this->valueToString($item);
  55. if (!is_null($value)) {
  56. $output->writeln(' - ' . $key . ': ' . $value);
  57. } else {
  58. $output->writeln(' - ' . $key);
  59. }
  60. } else {
  61. $output->writeln(' - ' . $this->valueToString($item));
  62. }
  63. }
  64. break;
  65. }
  66. }
  67. protected function valueToString($value) {
  68. if ($value === false) {
  69. return 'false';
  70. } else if ($value === true) {
  71. return 'true';
  72. } else if ($value === null) {
  73. null;
  74. } else {
  75. return $value;
  76. }
  77. }
  78. }