1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 13:16:45 +02:00

feat(console): add new commands has, delete, update and fetch` for tokens

This commit is contained in:
Awilum
2022-05-02 19:53:08 +03:00
parent aad73e713f
commit 37d54538da
6 changed files with 389 additions and 1 deletions

View File

@@ -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.',

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Console\Commands\Tokens;
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 function Thermage\div;
use function Thermage\renderToString;
class TokensDeleteCommand extends Command
{
protected function configure(): void
{
$this->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;
}
}
}

View File

@@ -0,0 +1,161 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Console\Commands\Tokens;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
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 Symfony\Component\Console\Helper\Table;
use function Thermage\div;
use function Thermage\span;
use function Thermage\renderToString;
class TokensFetchCommand extends Command
{
protected function configure(): void
{
$this->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;
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Console\Commands\Tokens;
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;
class TokensHasCommand extends Command
{
protected function configure(): void
{
$this->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;
}
}
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/**
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
* and with the full functionality of a traditional CMS!
*
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
*
* Licensed under The MIT License.
*
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice.
*/
namespace Flextype\Console\Commands\Tokens;
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 function Thermage\div;
use function Thermage\renderToString;
class TokensUpdateCommand extends Command
{
protected function configure(): void
{
$this->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;
}
}
}

View File

@@ -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();
}