1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 13:16:45 +02:00

feat(console): improve cache:get-multiple logic

This commit is contained in:
Awilum
2022-04-29 21:14:21 +03:00
parent fddeb9d82b
commit b0e8186498

View File

@@ -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;