diff --git a/src/flextype/core/Console/Commands/Tokens/TokensCreateCommand.php b/src/flextype/core/Console/Commands/Tokens/TokensCreateCommand.php index b34d708f..3d893b86 100644 --- a/src/flextype/core/Console/Commands/Tokens/TokensCreateCommand.php +++ b/src/flextype/core/Console/Commands/Tokens/TokensCreateCommand.php @@ -29,6 +29,7 @@ class TokensCreateCommand extends Command { $this->setName('tokens:create'); $this->setDescription('Create a new unique token.'); + $this->addArgument('data', InputArgument::OPTIONAL, 'Data to create for the token entry.'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -37,9 +38,21 @@ class TokensCreateCommand extends Command $access_token = generateToken(); $hashed_access_token = generateTokenHash($access_token); + $data = $input->getArgument('data'); + + if ($data) { + if (strings($data)->isJson()) { + $dataToSave = serializers()->json()->decode($data); + } else { + parse_str($data, $dataToSave); + } + } else { + $dataToSave = []; + } + ! entries()->has('tokens') and entries()->create('tokens', ['title' => 'Tokens']); - if (entries()->create('tokens/' . $token, ['hashed_access_token' => $hashed_access_token])) { + if (entries()->create('tokens/' . $token, array_merge($dataToSave, ['hashed_access_token' => $hashed_access_token]))) { $output->write( renderToString( div('Success: Token [b]' . $token . '[/b] with secret access token [b]' . $access_token . '[/b] created.', diff --git a/src/flextype/core/Console/Commands/Tokens/TokensDeleteCommand.php b/src/flextype/core/Console/Commands/Tokens/TokensDeleteCommand.php new file mode 100644 index 00000000..0818b706 --- /dev/null +++ b/src/flextype/core/Console/Commands/Tokens/TokensDeleteCommand.php @@ -0,0 +1,67 @@ +setName('tokens:delete'); + $this->setDescription('Delete token entry.'); + $this->addArgument('id', InputArgument::REQUIRED, 'Unique identifier of the token entry.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $id = $input->getArgument('id'); + + if (! entries()->has($id)) { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] doesn\'t exists.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + + if (entries()->delete($id)) { + $output->write( + renderToString( + div('Success: Token entry [b]' . $id . '[/b] deleted.', + 'bg-success px-2 py-1') + ) + ); + return Command::SUCCESS; + } else { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] wasn\'t deleted.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/Commands/Tokens/TokensFetchCommand.php b/src/flextype/core/Console/Commands/Tokens/TokensFetchCommand.php new file mode 100644 index 00000000..e33b5dd1 --- /dev/null +++ b/src/flextype/core/Console/Commands/Tokens/TokensFetchCommand.php @@ -0,0 +1,161 @@ +setName('tokens:fetch'); + $this->setDescription('Fetch token entry.'); + $this->addArgument('id', InputArgument::OPTIONAL, 'Unique identifier of the token entry.'); + $this->addArgument('options', InputArgument::OPTIONAL, 'Options array.'); + $this->addOption('collection', null, InputOption::VALUE_NONE, 'Set this flag to fetch tokens entries collection.'); + $this->addOption('find-depth-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens entries files depth of traversing from.'); + $this->addOption('find-depth-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens entries files depth of traversing to.'); + $this->addOption('find-date-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens entries files by a date range from.'); + $this->addOption('find-date-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens entries filesby a date range to.'); + $this->addOption('find-size-from', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens entries files by a size range from.'); + $this->addOption('find-size-to', null, InputOption::VALUE_OPTIONAL, 'Restrict the tokens 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 tokens entries files by content.'); + $this->addOption('find-not-contains', null, InputOption::VALUE_OPTIONAL, 'Find tokens entries files by content excludes files containing given pattern.'); + $this->addOption('find-path', null, InputOption::VALUE_OPTIONAL, 'Find tokens entries files and directories by path.'); + $this->addOption('find-sort-by-key', null, InputOption::VALUE_OPTIONAL, 'Sort the tokens 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 tokens 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 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 + { + $id = $input->getArgument('id') ? $input->getArgument('id') : ''; + $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')) { + $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-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'); + + if ($input->getOption('filter-where')) { + $filterWhere = $input->getOption('filter-where'); + + foreach ($filterWhere as $key => $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); + + if (count($data) > 0) { + if (isset($options['collection']) && $options['collection'] == true) { + foreach ($data->toArray() as $item) { + foreach(collection($item)->dot() as $key => $value) { + $innerDataString .= renderToString(span('[b][color=success]' . $key . '[/color][/b]: ' . $value) . PHP_EOL); + } + $innerData[] = $innerDataString; + $innerDataString = ''; + } + } else { + foreach(collection($data)->dot() as $key => $value) { + $innerDataString .= renderToString(span('[b][color=success]' . $key . '[/color][/b]: ' . $value) . PHP_EOL); + } + $innerData[] = $innerDataString; + $innerDataString = ''; + } + + foreach ($innerData as $item) { + $output->write( + renderToString( + div($item, 'px-2 border-square border-color-success') + ) + ); + } + + return Command::SUCCESS; + } else { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] doesn\'t exists.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/Commands/Tokens/TokensHasCommand.php b/src/flextype/core/Console/Commands/Tokens/TokensHasCommand.php new file mode 100644 index 00000000..56cb0e6b --- /dev/null +++ b/src/flextype/core/Console/Commands/Tokens/TokensHasCommand.php @@ -0,0 +1,58 @@ +setName('tokens:has'); + $this->setDescription('Check whether token entry exists.'); + $this->addArgument('id', InputArgument::REQUIRED, 'Unique identifier of the token entry.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $id = $input->getArgument('id'); + + if (entries()->has($id)) { + $output->write( + renderToString( + div('Success: Token entry [b]' . $id . '[/b] exists.', + 'bg-success px-2 py-1') + ) + ); + return Command::SUCCESS; + } else { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] doesn\'t exists.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/Commands/Tokens/TokensUpdateCommand.php b/src/flextype/core/Console/Commands/Tokens/TokensUpdateCommand.php new file mode 100644 index 00000000..34c8dc07 --- /dev/null +++ b/src/flextype/core/Console/Commands/Tokens/TokensUpdateCommand.php @@ -0,0 +1,79 @@ +setName('tokens:update'); + $this->setDescription('Update tokens entry.'); + $this->addArgument('id', InputArgument::REQUIRED, 'Unique identifier of the token entry.'); + $this->addArgument('data', InputArgument::OPTIONAL, 'Data to update for the token entry.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $id = $input->getArgument('id'); + $data = $input->getArgument('data'); + + if ($data) { + if (strings($data)->isJson()) { + $dataToSave = serializers()->json()->decode($data); + } else { + parse_str($data, $dataToSave); + } + } else { + $dataToSave = []; + } + + if (! entries()->has($id)) { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] doesn\'t exists.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + + if (entries()->update($id, $dataToSave)) { + $output->write( + renderToString( + div('Success: Token entry [b]' . $id . '[/b] updated.', + 'bg-success px-2 py-1') + ) + ); + return Command::SUCCESS; + } else { + $output->write( + renderToString( + div('Failure: Token entry [b]' . $id . '[/b] wasn\'t updated.', + 'bg-danger px-2 py-1') + ) + ); + return Command::FAILURE; + } + } +} \ No newline at end of file diff --git a/src/flextype/core/Console/FlextypeConsole.php b/src/flextype/core/Console/FlextypeConsole.php index f4a1aad3..7d84198a 100644 --- a/src/flextype/core/Console/FlextypeConsole.php +++ b/src/flextype/core/Console/FlextypeConsole.php @@ -39,6 +39,12 @@ use Flextype\Console\Commands\Tokens\TokensGenerateCommand; use Flextype\Console\Commands\Tokens\TokensGenerateHashCommand; use Flextype\Console\Commands\Tokens\TokensVerifyHashCommand; use Flextype\Console\Commands\Tokens\TokensCreateCommand; +use Flextype\Console\Commands\Tokens\TokensFetchCommand; +use Flextype\Console\Commands\Tokens\TokensUpdateCommand; +use Flextype\Console\Commands\Tokens\TokensDeleteCommand; +use Flextype\Console\Commands\Tokens\TokensCopyCommand; +use Flextype\Console\Commands\Tokens\TokensMoveCommand; +use Flextype\Console\Commands\Tokens\TokensHasCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -69,6 +75,10 @@ class FlextypeConsole extends ConsoleApplication console()->add(new TokensGenerateHashCommand()); console()->add(new TokensVerifyHashCommand()); console()->add(new TokensCreateCommand()); + console()->add(new TokensDeleteCommand()); + console()->add(new TokensUpdateCommand()); + console()->add(new TokensHasCommand()); + console()->add(new TokensFetchCommand()); parent::run(); }