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.

60 lines
1.7 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, print or json, default is plain)',
  34. 'plain'
  35. )
  36. ;
  37. }
  38. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) {
  39. $outputFormat = $input->getOption('output');
  40. switch ($outputFormat) {
  41. case 'json':
  42. case 'print':
  43. if ($outputFormat === 'json') {
  44. $output->writeln(json_encode($items));
  45. } else {
  46. print_r($items);
  47. }
  48. break;
  49. default:
  50. foreach ($items as $key => $item) {
  51. $output->writeln(' - ' . (!is_int($key) ? $key . ': ' : '') . $item);
  52. }
  53. break;
  54. }
  55. }
  56. }