1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 05:07:41 +02:00

feat(console): improve cache console commands logic

This commit is contained in:
Awilum
2022-05-01 21:26:12 +03:00
parent fe1ca849ae
commit 14887b7904
5 changed files with 36 additions and 43 deletions

View File

@@ -37,20 +37,18 @@ class CacheDeleteCommand extends Command
$key = $input->getArgument('key');
if (cache()->delete($key)) {
$io->success('Cache item with key ' . $key . ' deleted.');
$output->write(
renderToString(
div('Success: Cache item with key ' . $key . ' created.',
div('Success: Cache item with key ' . $key . ' deleted.',
'bg-success px-2 py-1')
)
);
return Command::SUCCESS;
} else {
$io->error('Cache item with key ' . $key . ' wasn\'t deleted.');
$output->write(
renderToString(
div('Success: Cache item with key ' . $key . ' created.',
'bg-success px-2 py-1')
div('Failure: Cache item with key ' . $key . ' wasn\'t deleted.',
'bg-danger px-2 py-1')
)
);
return Command::FAILURE;

View File

@@ -20,7 +20,8 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Thermage\div;
use function Thermage\renderToString;
class CacheDeleteMultipleCommand extends Command
{
@@ -33,15 +34,27 @@ class CacheDeleteMultipleCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$keys = $input->getArgument('keys') ? serializers()->json()->decode($input->getArgument('keys')) : [];
if ($input->getArgument('keys')) {
$keys = collectionFromString($input->getArgument('keys'), ',')->map(fn($key) => trim($key))->toArray();
} else {
$keys = [];
}
if (cache()->deleteMultiple($keys)) {
$io->success('Cache items with keys ' . implode(', ', $keys) . ' deleted.');
$output->write(
renderToString(
div('Cache items with keys [b]' . implode('[/b], [b]', $keys) . '[/b] deleted.',
'bg-success px-2 py-1')
)
);
return Command::SUCCESS;
} else {
$io->error('Cache items with keys ' . implode(', ', $keys) . ' wasn\'t deleted.');
$output->write(
renderToString(
div('Cache items with keys [b]' . implode('[/b], [b]', $keys) . '[/b] wasn\'t deleted.',
'bg-danger px-2 py-1')
)
);
return Command::FAILURE;
}
}

View File

@@ -19,8 +19,9 @@ 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 function Thermage\div;
use function Thermage\renderToString;
class CacheGetCommand extends Command
{
@@ -37,7 +38,11 @@ class CacheGetCommand extends Command
$key = $input->getArgument('key');
$default = $input->getArgument('default') ?? null;
$output->writeln(cache()->get($key, $default));
$output->write(
renderToString(
div('[b][color=success]Key:[/color][/b] ' . $key . "\n" . '[b][color=success]Value:[/color][/b] ' . cache()->get($key, $default), 'px-2 border-square border-color-success')
)
);
return Command::SUCCESS;
}

View File

@@ -32,7 +32,6 @@ class CacheGetMultipleCommand extends Command
$this->setDescription('Get multiple items.');
$this->addArgument('keys', InputArgument::REQUIRED, 'Keys.');
$this->addArgument('default', InputArgument::OPTIONAL, 'Default.');
$this->addOption('output', null, InputOption::VALUE_REQUIRED, 'Set this flag to set result output style: pretty(default), json.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
@@ -42,35 +41,14 @@ class CacheGetMultipleCommand extends Command
$data = cache()->getMultiple($keys, $default);
$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('output')) {
switch ($input->getOption('output')) {
case 'json':
$output->write(
renderToString(
div(json_encode($data))
)
);
break;
case 'pretty':
default:
$prettyPrint($data);
break;
}
} else {
$prettyPrint($data);
foreach ($data as $key => $value) {
$output->write(
renderToString(
div('[b][color=success]Key:[/color][/b] ' . $key . "\n" . '[b][color=success]Value:[/color][/b] ' . $value, 'px-2 border-square border-color-success')
)
);
}
return Command::SUCCESS;
}
}

View File

@@ -20,7 +20,6 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function Thermage\div;
use function Thermage\renderToString;