From db16a32ac3bf851b0f16034c6fbf348f3bb30577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 8 Jul 2024 16:56:07 +0200 Subject: [PATCH 1/3] feat(occ): Add support for iterable in Base and use in in group:list and user:list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- core/Command/Base.php | 8 ++++---- core/Command/Group/ListCommand.php | 23 ++++++++--------------- core/Command/User/ListCommand.php | 20 ++++++++------------ 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/core/Command/Base.php b/core/Command/Base.php index d5d70a8db39dc..a19b3586f0a20 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -37,17 +37,17 @@ 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: - $output->writeln(json_encode($items)); + $output->writeln(json_encode(iterator_to_array($items))); break; case self::OUTPUT_FORMAT_JSON_PRETTY: - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); + $output->writeln(json_encode(iterator_to_array($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 bea6d08c76f0b..f4c531bbc8980 100644 --- a/core/Command/Group/ListCommand.php +++ b/core/Command/Group/ListCommand.php @@ -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); } } diff --git a/core/Command/User/ListCommand.php b/core/Command/User/ListCommand.php index bb7987595114b..3d592afd7ee78 100644 --- a/core/Command/User/ListCommand.php +++ b/core/Command/User/ListCommand.php @@ -69,18 +69,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** * @param IUser[] $users - * @param bool [$detailed=false] - * @return array + * @return \Generator */ - 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 @@ 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; + } } } From bb94da69a611d0ff225a48244ad1b22b653dde1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 8 Jul 2024 17:02:43 +0200 Subject: [PATCH 2/3] fix(occ): Fix compatibility with PHP<8.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit iterator_to_array cannot take an array parameter prior to 8.2 Signed-off-by: Côme Chilliet --- core/Command/Base.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/Command/Base.php b/core/Command/Base.php index a19b3586f0a20..0af5981e9422e 100644 --- a/core/Command/Base.php +++ b/core/Command/Base.php @@ -40,10 +40,12 @@ protected function configure() { protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void { switch ($input->getOption('output')) { case self::OUTPUT_FORMAT_JSON: - $output->writeln(json_encode(iterator_to_array($items))); + $items = (is_array($items) ? $items : iterator_to_array($items)); + $output->writeln(json_encode($items)); break; case self::OUTPUT_FORMAT_JSON_PRETTY: - $output->writeln(json_encode(iterator_to_array($items), JSON_PRETTY_PRINT)); + $items = (is_array($items) ? $items : iterator_to_array($items)); + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); break; default: foreach ($items as $key => $item) { From 1e2155993d0098b2c65388228e6bb9a692f0ac0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 6 Aug 2024 14:43:44 +0200 Subject: [PATCH 3/3] chore(tests): Adapt tests to the group:list change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- tests/Core/Command/Group/ListCommandTest.php | 60 +++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/tests/Core/Command/Group/ListCommandTest.php b/tests/Core/Command/Group/ListCommandTest.php index e4e91ce145806..73eb8866715a2 100644 --- a/tests/Core/Command/Group/ListCommandTest.php +++ b/tests/Core/Command/Group/ListCommandTest.php @@ -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]); @@ -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]);