From 8f86b6169dff439d2949f719a606ddd9e20f636f Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 1 May 2022 16:27:41 +0300 Subject: [PATCH] feat(console): improve `entries:create` logic --- .../Commands/Entries/EntriesCreateCommand.php | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/flextype/core/Console/Commands/Entries/EntriesCreateCommand.php b/src/flextype/core/Console/Commands/Entries/EntriesCreateCommand.php index f9996741..0d553c09 100644 --- a/src/flextype/core/Console/Commands/Entries/EntriesCreateCommand.php +++ b/src/flextype/core/Console/Commands/Entries/EntriesCreateCommand.php @@ -19,8 +19,9 @@ namespace Flextype\Console\Commands\Entries; 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 EntriesCreateCommand extends Command { @@ -34,15 +35,34 @@ class EntriesCreateCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); + $id = $input->getArgument('id'); + $data = $input->getArgument('data'); - $data = $input->getArgument('data') ? serializers()->json()->decode($input->getArgument('data')) : []; + if ($data) { + if (strings($data)->isJson()) { + $dataToSave = serializers()->json()->decode($data); + } else { + parse_str($data, $dataToSave); + } + } else { + $dataToSave = []; + } - if (entries()->create($input->getArgument('id'), $data)) { - $io->success('Entry ' . $input->getArgument('id') . ' created.'); + if (entries()->create($id, $dataToSave)) { + $output->write( + renderToString( + div('Success: Entry [b]' . $id . '[/b] created.', + 'bg-success px-2 py-1') + ) + ); return Command::SUCCESS; } else { - $io->error('Entry ' . $input->getArgument('id') . ' wasn\'t created.'); + $output->write( + renderToString( + div('Failure: Entry [b]' . $id . '[/b] wasn\'t created.', + 'bg-danger px-2 py-1') + ) + ); return Command::FAILURE; } }