diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2024-08-06 17:34:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-06 17:34:04 +0200 |
commit | 2f3dfbb8774a0fac84d6f4d3cdd5bd40585a8e7e (patch) | |
tree | 8b7b90d399f5cc5de74b9a73dc2d87e03c679552 /core | |
parent | 1e2bf36512cd662d0a226bb9cb5cd24945cc0077 (diff) | |
parent | 1e2155993d0098b2c65388228e6bb9a692f0ac0c (diff) | |
download | nextcloud-server-2f3dfbb8774a0fac84d6f4d3cdd5bd40585a8e7e.tar.gz nextcloud-server-2f3dfbb8774a0fac84d6f4d3cdd5bd40585a8e7e.zip |
Merge pull request #46356 from nextcloud/enh/use-generator-for-group-list
feat(occ): Add support for iterable in Base and use it in group:list and user:list
Diffstat (limited to 'core')
-rw-r--r-- | core/Command/Base.php | 6 | ||||
-rw-r--r-- | core/Command/Group/ListCommand.php | 23 | ||||
-rw-r--r-- | core/Command/User/ListCommand.php | 20 |
3 files changed, 20 insertions, 29 deletions
diff --git a/core/Command/Base.php b/core/Command/Base.php index d5d70a8db39..0af5981e942 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -37,17 +37,19 @@ class Base extends Command implements CompletionAwareInterface { ; } - 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)); $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; diff --git a/core/Command/Group/ListCommand.php b/core/Command/Group/ListCommand.php index bea6d08c76f..f4c531bbc89 100644 --- a/core/Command/Group/ListCommand.php +++ b/core/Command/Group/ListCommand.php @@ -68,25 +68,18 @@ class ListCommand extends Base { /** * @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); } } diff --git a/core/Command/User/ListCommand.php b/core/Command/User/ListCommand.php index bb798759511..3d592afd7ee 100644 --- a/core/Command/User/ListCommand.php +++ b/core/Command/User/ListCommand.php @@ -69,18 +69,13 @@ class ListCommand extends Base { /** * @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(), @@ -92,9 +87,10 @@ class ListCommand extends Base { 'user_directory' => $user->getHome(), 'backend' => $user->getBackendClassName() ]; + } else { + $value = $user->getDisplayName(); } - return $user->getDisplayName(); - }, $users); - return array_combine($keys, $values); + yield $user->getUID() => $value; + } } } |