Skip to content
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

feat(occ): Add support for iterable in Base and use it in group:list and user:list #46356

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions core/Command/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ protected function configure() {
;
}

protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - '): void {
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void {
switch ($input->getOption('output')) {
case self::OUTPUT_FORMAT_JSON:
$items = (is_array($items) ? $items : iterator_to_array($items));
come-nc marked this conversation as resolved.
Show resolved Hide resolved
$output->writeln(json_encode($items));
break;
case self::OUTPUT_FORMAT_JSON_PRETTY:
$items = (is_array($items) ? $items : iterator_to_array($items));
$output->writeln(json_encode($items, JSON_PRETTY_PRINT));
break;
default:
foreach ($items as $key => $item) {
if (is_array($item)) {
if (is_iterable($item)) {
$output->writeln($prefix . $key . ':');
$this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
continue;
Expand Down
23 changes: 8 additions & 15 deletions core/Command/Group/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,18 @@ public function usersForGroup(IGroup $group) {

/**
* @param IGroup[] $groups
* @return array
*/
private function formatGroups(array $groups, bool $addInfo = false) {
$keys = array_map(function (IGroup $group) {
return $group->getGID();
}, $groups);

if ($addInfo) {
$values = array_map(function (IGroup $group) {
return [
private function formatGroups(array $groups, bool $addInfo = false): \Generator {
foreach ($groups as $group) {
if ($addInfo) {
$value = [
'backends' => $group->getBackendNames(),
'users' => $this->usersForGroup($group),
];
}, $groups);
} else {
$values = array_map(function (IGroup $group) {
return $this->usersForGroup($group);
}, $groups);
} else {
$value = $this->usersForGroup($group);
}
yield $group->getGID() => $value;
}
return array_combine($keys, $values);
}
}
20 changes: 8 additions & 12 deletions core/Command/User/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* @param IUser[] $users
* @param bool [$detailed=false]
* @return array
* @return \Generator<string,string|array>
*/
private function formatUsers(array $users, bool $detailed = false) {
$keys = array_map(function (IUser $user) {
return $user->getUID();
}, $users);

$values = array_map(function (IUser $user) use ($detailed) {
private function formatUsers(array $users, bool $detailed = false): \Generator {
foreach ($users as $user) {
if ($detailed) {
$groups = $this->groupManager->getUserGroupIds($user);
return [
$value = [
'user_id' => $user->getUID(),
'display_name' => $user->getDisplayName(),
'email' => (string)$user->getSystemEMailAddress(),
Expand All @@ -92,9 +87,10 @@ private function formatUsers(array $users, bool $detailed = false) {
'user_directory' => $user->getHome(),
'backend' => $user->getBackendClassName()
];
} else {
$value = $user->getDisplayName();
}
return $user->getDisplayName();
}, $users);
return array_combine($keys, $values);
yield $user->getUID() => $value;
}
}
}
60 changes: 32 additions & 28 deletions tests/Core/Command/Group/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ public function testExecute() {
->with(
$this->equalTo($this->input),
$this->equalTo($this->output),
[
'group1' => [
'user1',
'user2',
],
'group2' => [
],
'group3' => [
'user1',
'user3',
$this->callback(
fn ($iterator) => iterator_to_array($iterator) === [
'group1' => [
'user1',
'user2',
],
'group2' => [
],
'group3' => [
'user1',
'user3',
]
]
]
)
);

$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
Expand Down Expand Up @@ -166,26 +168,28 @@ public function testInfo() {
->with(
$this->equalTo($this->input),
$this->equalTo($this->output),
[
'group1' => [
'backends' => ['Database'],
'users' => [
'user1',
'user2',
$this->callback(
fn ($iterator) => iterator_to_array($iterator) === [
'group1' => [
'backends' => ['Database'],
'users' => [
'user1',
'user2',
],
],
],
'group2' => [
'backends' => ['Database'],
'users' => [],
],
'group3' => [
'backends' => ['LDAP'],
'users' => [
'user1',
'user3',
'group2' => [
'backends' => ['Database'],
'users' => [],
],
'group3' => [
'backends' => ['LDAP'],
'users' => [
'user1',
'user3',
],
]
]
]
)
);

$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
Expand Down
Loading