From b0e81864986875f77cb47ac30e32ed3742138423 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 29 Apr 2022 21:14:21 +0300 Subject: [PATCH] feat(console): improve `cache:get-multiple` logic --- .../Cache/CacheGetMultipleCommand.php | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/flextype/core/Console/Commands/Cache/CacheGetMultipleCommand.php b/src/flextype/core/Console/Commands/Cache/CacheGetMultipleCommand.php index d66a921c..f2647766 100644 --- a/src/flextype/core/Console/Commands/Cache/CacheGetMultipleCommand.php +++ b/src/flextype/core/Console/Commands/Cache/CacheGetMultipleCommand.php @@ -19,8 +19,10 @@ namespace Flextype\Console\Commands\Cache; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use function Thermage\div; +use function Thermage\renderToString; class CacheGetMultipleCommand extends Command { @@ -30,17 +32,43 @@ class CacheGetMultipleCommand extends Command $this->setDescription('Get multiple items.'); $this->addArgument('keys', InputArgument::REQUIRED, 'Keys.'); $this->addArgument('default', InputArgument::OPTIONAL, 'Default.'); + $this->addOption('template', null, InputOption::VALUE_REQUIRED, 'Set this flag to set result print style: pretty(default), json.'); } protected function execute(InputInterface $input, OutputInterface $output): int { - $keys = $input->getArgument('keys') ? serializers()->json()->decode($input->getArgument('keys')) : []; + $keys = $input->getArgument('keys') ? collectionFromString($input->getArgument('keys'), ',') : []; $default = $input->getArgument('default') ?? null; $data = cache()->getMultiple($keys, $default); - foreach ($data as $key => $value) { - $output->writeln($value); + $prettyPrint = function ($data) use ($output) { + foreach ($data as $key => $value) { + $output->write( + renderToString( + div('[b]Key:[/b] ' . $key . "\n" . '[b]Value:[/b] ' . $value, 'px-2 border-square') + ) + ); + } + }; + + if ($input->getOption('template')) { + switch ($input->getOption('template')) { + case 'json': + $output->write( + renderToString( + div(json_encode($data)) + ) + ); + break; + + case 'pretty': + default: + $prettyPrint($data); + break; + } + } else { + $prettyPrint($data); } return Command::SUCCESS;