-
Notifications
You must be signed in to change notification settings - Fork 17
Added sorting options to ibexa:debug:config
#631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adriendupuis
wants to merge
7
commits into
main
Choose a base branch
from
enh-ibexa-debug-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d8ec98
DebugConfigResolverCommand.php: Add sort option
adriendupuis 9e3d91a
DebugConfigResolverCommand.php: Add sort option
adriendupuis 0fa0622
DebugConfigResolverCommand.php: Fix code style
adriendupuis 0b3ce1d
DebugConfigResolverCommand.php: Enhance error handling
adriendupuis 907d617
DebugConfigResolverCommand.php: Enhance error handling
adriendupuis 7202bcd
DebugConfigResolverCommand.php: PHP CS Fixer
adriendupuis 5ea5d4d
DebugConfigResolverCommand.php: better list item check
adriendupuis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| namespace Ibexa\Bundle\Core\Command; | ||
|
|
||
| use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; | ||
| use Ibexa\Core\Base\Exceptions\InvalidArgumentException; | ||
| use Ibexa\Core\MVC\Symfony\SiteAccess; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
|
|
@@ -69,6 +70,18 @@ public function configure(): void | |
| InputOption::VALUE_REQUIRED, | ||
| 'Set a different namespace than the default "ibexa.site_access.config" used by SiteAccess settings.' | ||
| ); | ||
| $this->addOption( | ||
| 'sort', | ||
| null, | ||
| InputOption::VALUE_REQUIRED, | ||
| 'Sort list of hashes by this key, ascending. For example: --sort position' | ||
| ); | ||
| $this->addOption( | ||
| 'reverse-sort', | ||
| null, | ||
| InputOption::VALUE_NONE, | ||
| 'Reverse the sorting to descending. For example: --sort priority --reverse-sort' | ||
| ); | ||
| $this->setHelp( | ||
| <<<EOM | ||
| Outputs a given config resolver parameter, more commonly known as a SiteAccess setting. | ||
|
|
@@ -93,8 +106,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
| $parameter = $input->getArgument('parameter'); | ||
| $namespace = $input->getOption('namespace'); | ||
| $scope = $input->getOption('scope'); | ||
| $sort = $input->getOption('sort'); | ||
| $parameterData = $this->configResolver->getParameter($parameter, $namespace, $scope); | ||
|
|
||
| if (null !== $sort && !empty($parameterData)) { | ||
| if (!is_array($parameterData)) { | ||
| throw new InvalidArgumentException('--sort', "'$parameter' isn't a list. Sort can be used only on a list."); | ||
| } | ||
| if (!array_is_list($parameterData)) { | ||
| throw new InvalidArgumentException('--sort', "'$parameter' is a hash but sort can be used only on a list (an array with numeral keys incremented from zero)."); | ||
| } | ||
| foreach ($parameterData as $item) { | ||
| if (!is_array($item) || array_is_list($item)) { | ||
| throw new InvalidArgumentException('--sort', "'$parameter' list items aren't all hashes. Sort can be used only on a list of hashes."); | ||
| } | ||
| if (!array_key_exists($sort, $item)) { | ||
| throw new InvalidArgumentException('--sort', "'$sort' property doesn't exist on each '$parameter' list item."); | ||
| } | ||
| if (!is_scalar($item[$sort])) { | ||
| throw new InvalidArgumentException('--sort', "'$sort' properties aren't always scalar and can't be sorted."); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of letting these input-validation exceptions bubble, you could catch them, print a styled error, and return Command::INVALID or use Command::FAILURE for runtime errors. |
||
| } | ||
| } | ||
| if ($input->getOption('reverse-sort')) { | ||
| usort($parameterData, static fn (array $a, array $b): int => $b[$sort] <=> $a[$sort]); | ||
| } else { | ||
| usort($parameterData, static fn (array $a, array $b): int => $a[$sort] <=> $b[$sort]); | ||
| } | ||
| } | ||
|
|
||
| // In case of json output return early with no newlines and only the parameter data | ||
| if ($input->getOption('json')) { | ||
| $output->write(json_encode($parameterData)); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For CLI argument validation you could use a
Symfony\Component\Console\Exception\InvalidArgumentException. It integrates better with Console error styling and exit codes.