Skip to content
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/bundle/Core/Command/DebugConfigResolverCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -93,8 +106,31 @@ 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.");
Copy link
Contributor

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.

}
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).");
}
for ($i = 0, $count = count($parameterData); $i < $count; ++$i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for ($i = 0, $count = count($parameterData); $i < $count; ++$i) {
foreach ($parameterData as $i => $data) {

if (!array_key_exists($sort, $parameterData[$i])) {
throw new InvalidArgumentException('--sort', "'$sort' property doesn't exist on each '$parameter' list item.");
}
if (!is_scalar($parameterData[$i][$sort])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_scalar() allows bool, int, float, string. Mixed types across items (e.g., "10" and 2, or "abc" and 1) will be coerced by <=>, often in surprising ways (non-numeric strings become 0 in numeric comparisons). You could enforce type consistency across the list for the $sort key, or detect the predominant type and normalize.

throw new InvalidArgumentException('--sort', "'$sort' properties aren't always scalar and can't be sorted.");
Copy link
Contributor

Choose a reason for hiding this comment

The 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));
Expand Down
Loading