From 141ddf4d494eafefda147a81c42afd962eac4030 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sun, 1 May 2022 15:44:40 +0300 Subject: [PATCH] feat(console): improve `entries:fetch` logic --- .../Commands/Entries/EntrieFetchCommand.php | 100 ++++++++++++------ 1 file changed, 67 insertions(+), 33 deletions(-) diff --git a/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php index c628dc00..740d9e5c 100644 --- a/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php +++ b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php @@ -23,6 +23,9 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Helper\Table; +use function Thermage\div; +use function Thermage\span; +use function Thermage\renderToString; class EntriesFetchCommand extends Command { @@ -33,34 +36,40 @@ class EntriesFetchCommand extends Command $this->addArgument('id', InputArgument::OPTIONAL, 'Unique identifier of the entry.'); $this->addArgument('options', InputArgument::OPTIONAL, 'Options array.'); $this->addOption('collection', null, InputOption::VALUE_NONE, 'Set this flag to fetch entries collection.'); - $this->addOption('return', null, InputOption::VALUE_OPTIONAL, 'Return items. Valid values: all, first, last, next, random, shuffle.'); - $this->addOption('find-depth-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the depth of traversing from.'); - $this->addOption('find-depth-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the depth of traversing to.'); - $this->addOption('find-date-from', null, InputOption::VALUE_OPTIONAL, 'Restrict by a date range from.'); - $this->addOption('find-date-to', null, InputOption::VALUE_OPTIONAL, 'Restrict by a date range to.'); - $this->addOption('find-size-from', null, InputOption::VALUE_OPTIONAL, 'Restrict by a size range from.'); - $this->addOption('find-size-to', null, InputOption::VALUE_OPTIONAL, 'Restrict by a size range to.'); + $this->addOption('find-depth-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries files depth of traversing from.'); + $this->addOption('find-depth-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries files depth of traversing to.'); + $this->addOption('find-date-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries files by a date range from.'); + $this->addOption('find-date-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries filesby a date range to.'); + $this->addOption('find-size-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries files by a size range from.'); + $this->addOption('find-size-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the entries files by a size range to.'); $this->addOption('find-exclude', null, InputOption::VALUE_OPTIONAL, 'Exclude directories from matching.'); - $this->addOption('find-contains', null, InputOption::VALUE_OPTIONAL, 'Find files by content.'); - $this->addOption('find-not-contains', null, InputOption::VALUE_OPTIONAL, 'Find files by content excludes files containing given pattern.'); - $this->addOption('find-path', null, InputOption::VALUE_OPTIONAL, 'Find files and directories by path.'); - $this->addOption('find-sort-by', null, InputOption::VALUE_OPTIONAL, 'Sort the files and directories by the last accessed, changed or modified time. Values: atime, mtime, ctime.'); + $this->addOption('find-contains', null, InputOption::VALUE_OPTIONAL, 'Find entries files by content.'); + $this->addOption('find-not-contains', null, InputOption::VALUE_OPTIONAL, 'Find entries files by content excludes files containing given pattern.'); + $this->addOption('find-path', null, InputOption::VALUE_OPTIONAL, 'Find entries files and directories by path.'); + $this->addOption('find-sort-by-key', null, InputOption::VALUE_OPTIONAL, 'Sort the entries files and directories by the last accessed, changed or modified time. Values: atime, mtime, ctime.'); + $this->addOption('find-sort-by-direction', null, InputOption::VALUE_OPTIONAL, 'Sort the entries files and directories by direction. Order direction: DESC (descending) or ASC (ascending)'); $this->addOption('filter-return', null, InputOption::VALUE_OPTIONAL, 'Return items. Valid values: all, first, last, next, random, shuffle'); - $this->addOption('filter-group-by', null, InputOption::VALUE_OPTIONAL, 'Group by key.'); - $this->addOption('filter-offset', null, InputOption::VALUE_OPTIONAL, 'Extract a slice of the current array with specific offset.'); - $this->addOption('filter-limit', null, InputOption::VALUE_OPTIONAL, 'Extract a slice of the current array with offset 0 and specific length.'); - $this->addOption('filter-sort-by-key', null, InputOption::VALUE_OPTIONAL, 'Sort by key.'); - $this->addOption('filter-sort-by-direction', null, InputOption::VALUE_OPTIONAL, 'Sort by direction. Order direction: DESC (descending) or ASC (ascending)'); - $this->addOption('filter-where', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Filters the array items by a given condition.'); + $this->addOption('filter-group-by', null, InputOption::VALUE_OPTIONAL, 'Group array collection by key.'); + $this->addOption('filter-offset', null, InputOption::VALUE_OPTIONAL, 'Extract a slice of the current array collection with specific offset.'); + $this->addOption('filter-limit', null, InputOption::VALUE_OPTIONAL, 'Extract a slice of the current array collection with offset 0 and specific length.'); + $this->addOption('filter-sort-by-key', null, InputOption::VALUE_OPTIONAL, 'Sort array collection by key.'); + $this->addOption('filter-sort-by-direction', null, InputOption::VALUE_OPTIONAL, 'Sort array collection by direction. Order direction: DESC (descending) or ASC (ascending)'); + $this->addOption('filter-where', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Filters the array collection fields by a given condition.'); } protected function execute(InputInterface $input, OutputInterface $output): int { - $io = new SymfonyStyle($input, $output); - $id = $input->getArgument('id') ? $input->getArgument('id') : ''; - $options = $input->getArgument('options') ? serializers()->json()->decode($input->getArgument('options')) : []; - + $options = []; + + if ($input->getArgument('options')) { + if (strings($input->getArgument('options'))->isJson()) { + $options = serializers()->json()->decode($input->getArgument('options')); + } else { + parse_str($input->getArgument('options'), $options); + } + } + $input->getOption('collection') and $options['collection'] = true; if ($input->getOption('find-depth-from') || $input->getOption('find-depth-to')) { @@ -85,8 +94,8 @@ class EntriesFetchCommand extends Command $input->getOption('find-contains') and $options['find']['contains'] = $input->getOption('find-contains'); $input->getOption('find-not-contains') and $options['find']['not_contains'] = $input->getOption('find-not-contains'); $input->getOption('find-path') and $options['find']['path'] = $input->getOption('find-path'); - $input->getOption('find-sort-by-key') and $options['find']['sort_by']['key'] = $input->getOption('find-sort-by'); - $input->getOption('find-sort-by-direction') and $options['find']['sort_by']['direction'] = $input->getOption('find-sort-by'); + $input->getOption('find-sort-by-key') and $options['find']['sort_by']['key'] = $input->getOption('find-sort-by-key'); + $input->getOption('find-sort-by-direction') and $options['find']['sort_by']['direction'] = $input->getOption('find-sort-by-direction'); $input->getOption('filter-group-by') and $options['filter']['group_by'] = $input->getOption('filter-group-by'); $input->getOption('filter-return') and $options['filter']['return'] = $input->getOption('filter-return'); @@ -95,32 +104,57 @@ class EntriesFetchCommand extends Command $filterWhere = $input->getOption('filter-where'); foreach ($filterWhere as $key => $value) { - $where[] = serializers()->json()->decode($value); + + if (strings($value)->isJson()) { + $whereValues = serializers()->json()->decode($value); + } else { + parse_str($value, $whereValues); + } + + $where[] = $whereValues; } $options['filter']['where'] = $where; } + + $innerData = []; + $innerDataString = ''; + + $data = entries()->fetch($id, $options); - $output->writeln(''); - - if ($data = entries()->fetch($id, $options)) { + if (count($data) > 0) { if (isset($options['collection']) && $options['collection'] == true) { foreach ($data->toArray() as $item) { foreach(collection($item)->dot() as $key => $value) { - $output->writeln(''.$key.': ' . $value); + $innerDataString .= renderToString(span('[b][color=success]' . $key . '[/color][/b]: ' . $value) . PHP_EOL); } - $output->writeln(''); - } + $innerData[] = $innerDataString; + $innerDataString = ''; + } } else { foreach(collection($data)->dot() as $key => $value) { - $output->writeln(''.$key.': ' . $value); + $innerDataString .= renderToString(span('[b][color=success]' . $key . '[/color][/b]: ' . $value) . PHP_EOL); } - $output->writeln(''); + $innerData[] = $innerDataString; + $innerDataString = ''; + } + + foreach ($innerData as $item) { + $output->write( + renderToString( + div($item, 'px-2 border-square border-color-success') + ) + ); } return Command::SUCCESS; } else { - $io->error('Entry ' . $id . ' doesn\'t exists'); + $output->write( + renderToString( + div('Failure: Entry [b]' . $id . '[/b] doesn\'t exists.', + 'bg-danger px-2 py-1') + ) + ); return Command::FAILURE; } }