diff --git a/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php new file mode 100644 index 00000000..dace162b --- /dev/null +++ b/src/flextype/core/Console/Commands/Entries/EntrieFetchCommand.php @@ -0,0 +1,116 @@ +setName('entries:fetch'); + $this->setDescription('Fetch entry.'); + $this->addOption('id', null, InputOption::VALUE_REQUIRED, 'Unique identifier of the entry.'); + $this->addOption('options', null, InputOption::VALUE_OPTIONAL, 'Options array.'); + $this->addOption('collection', null, InputOption::VALUE_OPTIONAL, 'Set true to fetch entries collection.'); + $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-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('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.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $options = $input->getOption('options') ? serializers()->json()->decode($input->getOption('options')) : []; + + $input->getOption('collection') and $options['collection'] = strings($input->getOption('collection'))->toBoolean(); + + if ($input->getOption('find-depth-from') || $input->getOption('find-depth-to')) { + $options['find']['depth'] = []; + $input->getOption('find-depth-from') and array_push($options['find']['depth'], $input->getOption('find-depth-from')); + $input->getOption('find-depth-to') and array_push($options['find']['depth'], $input->getOption('find-depth-to')); + } + + if ($input->getOption('find-date-from') || $input->getOption('find-date-to')) { + $options['find']['date'] = []; + $input->getOption('find-date-from') and array_push($options['find']['date'], $input->getOption('find-date-from')); + $input->getOption('find-date-to') and array_push($options['find']['date'], $input->getOption('find-date-to')); + } + + if ($input->getOption('find-size-from') || $input->getOption('find-size-to')) { + $options['find']['size'] = []; + $input->getOption('find-size-from') and array_push($options['find']['size'], $input->getOption('find-size-from')); + $input->getOption('find-size-to') and array_push($options['find']['size'], $input->getOption('find-size-to')); + } + + $input->getOption('find-exclude') and $options['find']['exclude'] = $input->getOption('find-exclude'); + $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') and $options['find']['sort-by'] = $input->getOption('find-sort-by'); + + $input->getOption('filter-group-by') and $options['filter']['group_by'] = $input->getOption('filter-sort-by'); + $input->getOption('filter-return') and $options['filter']['return'] = $input->getOption('filter-sort-by'); + + if ($input->getOption('filter-where')) { + $filterWhere = $input->getOption('filter-where'); + + foreach ($filterWhere as $key => $value) { + $where[] = serializers()->json()->decode($value); + } + + $options['filter']['where'] = $where; + } + + $output->writeln(''); + + if ($data = entries()->fetch($input->getOption('id'), $options)) { + if (isset($options['collection']) && $options['collection'] == true) { + foreach ($data->toArray() as $item) { + foreach(arrays($item)->dot() as $key => $value) { + $output->writeln(''.$key.': ' . $value); + } + $output->writeln(''); + } + } else { + foreach(arrays($data)->dot() as $key => $value) { + $output->writeln(''.$key.': ' . $value); + } + $output->writeln(''); + } + + return Command::SUCCESS; + } else { + $io->error('Entry ' . $input->getOption('id') . ' doesn\'t exists'); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/FlextypeConsoleApplication.php b/src/flextype/core/Console/FlextypeConsoleApplication.php index 760f0be7..b63206f4 100644 --- a/src/flextype/core/Console/FlextypeConsoleApplication.php +++ b/src/flextype/core/Console/FlextypeConsoleApplication.php @@ -11,6 +11,7 @@ namespace Flextype\Console; use Symfony\Component\Console\Application as ConsoleApplication; use Flextype\Console\Commands\Entries\EntriesCreateCommand; +use Flextype\Console\Commands\Entries\EntriesFetchCommand; use Flextype\Console\Commands\Entries\EntriesUpdateCommand; use Flextype\Console\Commands\Entries\EntriesDeleteCommand; use Flextype\Console\Commands\Entries\EntriesCopyCommand; @@ -30,6 +31,7 @@ class FlextypeConsoleApplication extends ConsoleApplication console()->add(new EntriesCopyCommand()); console()->add(new EntriesMoveCommand()); console()->add(new EntriesHasCommand()); + console()->add(new EntriesFetchCommand()); parent::run(); }