From f8f9d9f9c9e5d8f8e9835321dec3f26312fa1632 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 21 Feb 2020 17:16:14 +0300 Subject: [PATCH] add jsonformat option to databox:list --- .../Command/Databox/ListDataboxCommand.php | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php b/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php index 49775437d5..92a30435fe 100644 --- a/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php +++ b/lib/Alchemy/Phrasea/Command/Databox/ListDataboxCommand.php @@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Command\Databox; use Alchemy\Phrasea\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class ListDataboxCommand extends Command @@ -25,6 +26,7 @@ class ListDataboxCommand extends Command parent::__construct('databox:list'); $this->setDescription('List all databox in Phraseanet') + ->addOption('jsonformat', null, InputOption::VALUE_NONE, 'Output in json format') ->setHelp(''); return $this; @@ -33,15 +35,23 @@ class ListDataboxCommand extends Command protected function doExecute(InputInterface $input, OutputInterface $output) { try { + $jsonformat = $input->getOption('jsonformat'); $databoxes = array_map(function (\databox $databox) { return $this->listDatabox($databox); }, $this->container->getApplicationBox()->get_databoxes()); - $table = $this->getHelperSet()->get('table'); - $table - ->setHeaders(['id', 'name', 'alias']) - ->setRows($databoxes) - ->render($output); + if ($jsonformat) { + foreach ($databoxes as $databox) { + $databoxList[] = array_combine(['id', 'name', 'alias'], $databox); + } + echo json_encode($databoxList); + } else { + $table = $this->getHelperSet()->get('table'); + $table + ->setHeaders(['id', 'name', 'alias']) + ->setRows($databoxes) + ->render($output); + } } catch (\Exception $e) { $output->writeln('Listing databox failed : '.$e->getMessage().'');