@@ -18,144 +18,144 @@ |
||
| 18 | 18 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 19 | 19 | |
| 20 | 20 | class Show extends Base { |
| 21 | - public function __construct( |
|
| 22 | - private IManager $settingManager, |
|
| 23 | - private AuthorizedGroupService $authorizedGroupService, |
|
| 24 | - ) { |
|
| 25 | - parent::__construct(); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - protected function configure(): void { |
|
| 29 | - parent::configure(); |
|
| 30 | - $this |
|
| 31 | - ->setName('admin-delegation:show') |
|
| 32 | - ->setDescription('show delegated settings') |
|
| 33 | - ; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 37 | - $io = new SymfonyStyle($input, $output); |
|
| 38 | - $outputFormat = $input->getOption('output'); |
|
| 39 | - |
|
| 40 | - // Validate output format |
|
| 41 | - if (!$this->validateOutputFormat($outputFormat)) { |
|
| 42 | - $io->error("Invalid output format: {$outputFormat}. Valid formats are: plain, json, json_pretty"); |
|
| 43 | - return 1; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - // Collect delegation data |
|
| 47 | - $delegationData = $this->collectDelegationData(); |
|
| 48 | - |
|
| 49 | - // Handle empty results |
|
| 50 | - if (empty($delegationData)) { |
|
| 51 | - if ($outputFormat === self::OUTPUT_FORMAT_PLAIN) { |
|
| 52 | - $io->info('No delegated settings found.'); |
|
| 53 | - } else { |
|
| 54 | - $this->writeArrayInOutputFormat($input, $io, []); |
|
| 55 | - } |
|
| 56 | - return 0; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - // Output based on format |
|
| 60 | - switch ($outputFormat) { |
|
| 61 | - case self::OUTPUT_FORMAT_JSON: |
|
| 62 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 63 | - $this->writeArrayInOutputFormat($input, $io, $delegationData); |
|
| 64 | - break; |
|
| 65 | - default: |
|
| 66 | - $this->outputPlainFormat($io, $delegationData); |
|
| 67 | - break; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return 0; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Collect all delegation data in a structured format |
|
| 75 | - */ |
|
| 76 | - private function collectDelegationData(): array { |
|
| 77 | - $result = []; |
|
| 78 | - $sections = $this->settingManager->getAdminSections(); |
|
| 79 | - |
|
| 80 | - foreach ($sections as $sectionPriority) { |
|
| 81 | - foreach ($sectionPriority as $section) { |
|
| 82 | - $sectionSettings = $this->settingManager->getAdminSettings($section->getId()); |
|
| 83 | - $delegatedSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []); |
|
| 84 | - |
|
| 85 | - if (empty($delegatedSettings)) { |
|
| 86 | - continue; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $result[] = [ |
|
| 90 | - 'id' => $section->getID(), |
|
| 91 | - 'name' => $section->getName() ?: $section->getID(), |
|
| 92 | - 'settings' => $this->formatSettingsData($delegatedSettings) |
|
| 93 | - ]; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return $result; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Format settings data for consistent output |
|
| 102 | - */ |
|
| 103 | - private function formatSettingsData(array $settings): array { |
|
| 104 | - return array_map(function (IDelegatedSettings $setting) { |
|
| 105 | - $className = get_class($setting); |
|
| 106 | - $groups = array_map( |
|
| 107 | - static fn (AuthorizedGroup $group) => $group->getGroupId(), |
|
| 108 | - $this->authorizedGroupService->findExistingGroupsForClass($className) |
|
| 109 | - ); |
|
| 110 | - natsort($groups); |
|
| 111 | - |
|
| 112 | - return [ |
|
| 113 | - 'name' => $setting->getName() ?: 'Global', |
|
| 114 | - 'className' => $className, |
|
| 115 | - 'priority' => $setting->getPriority(), |
|
| 116 | - 'delegatedGroups' => $groups, |
|
| 117 | - ]; |
|
| 118 | - }, $settings); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Output data in plain table format |
|
| 123 | - */ |
|
| 124 | - private function outputPlainFormat(SymfonyStyle $io, array $data): void { |
|
| 125 | - $io->title('Current delegations'); |
|
| 126 | - $headers = ['Name', 'SettingId', 'Delegated to groups']; |
|
| 127 | - |
|
| 128 | - foreach ($data as $section) { |
|
| 129 | - $io->section('Section: ' . $section['id']); |
|
| 130 | - |
|
| 131 | - $tableData = array_map(static function (array $setting) { |
|
| 132 | - return [ |
|
| 133 | - $setting['name'], |
|
| 134 | - $setting['className'], |
|
| 135 | - implode(', ', $setting['delegatedGroups']), |
|
| 136 | - ]; |
|
| 137 | - }, $section['settings']); |
|
| 138 | - |
|
| 139 | - $io->table($headers, $tableData); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Validate the output format parameter |
|
| 145 | - */ |
|
| 146 | - private function validateOutputFormat(string $format): bool { |
|
| 147 | - return in_array($format, [ |
|
| 148 | - self::OUTPUT_FORMAT_PLAIN, |
|
| 149 | - self::OUTPUT_FORMAT_JSON, |
|
| 150 | - self::OUTPUT_FORMAT_JSON_PRETTY |
|
| 151 | - ], true); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @param IDelegatedSettings[] $settings |
|
| 156 | - * @param array $innerSection |
|
| 157 | - */ |
|
| 158 | - private function getDelegatedSettings(array $settings, array $innerSection): array { |
|
| 159 | - return array_merge($settings, array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings)); |
|
| 160 | - } |
|
| 21 | + public function __construct( |
|
| 22 | + private IManager $settingManager, |
|
| 23 | + private AuthorizedGroupService $authorizedGroupService, |
|
| 24 | + ) { |
|
| 25 | + parent::__construct(); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + protected function configure(): void { |
|
| 29 | + parent::configure(); |
|
| 30 | + $this |
|
| 31 | + ->setName('admin-delegation:show') |
|
| 32 | + ->setDescription('show delegated settings') |
|
| 33 | + ; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 37 | + $io = new SymfonyStyle($input, $output); |
|
| 38 | + $outputFormat = $input->getOption('output'); |
|
| 39 | + |
|
| 40 | + // Validate output format |
|
| 41 | + if (!$this->validateOutputFormat($outputFormat)) { |
|
| 42 | + $io->error("Invalid output format: {$outputFormat}. Valid formats are: plain, json, json_pretty"); |
|
| 43 | + return 1; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + // Collect delegation data |
|
| 47 | + $delegationData = $this->collectDelegationData(); |
|
| 48 | + |
|
| 49 | + // Handle empty results |
|
| 50 | + if (empty($delegationData)) { |
|
| 51 | + if ($outputFormat === self::OUTPUT_FORMAT_PLAIN) { |
|
| 52 | + $io->info('No delegated settings found.'); |
|
| 53 | + } else { |
|
| 54 | + $this->writeArrayInOutputFormat($input, $io, []); |
|
| 55 | + } |
|
| 56 | + return 0; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + // Output based on format |
|
| 60 | + switch ($outputFormat) { |
|
| 61 | + case self::OUTPUT_FORMAT_JSON: |
|
| 62 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 63 | + $this->writeArrayInOutputFormat($input, $io, $delegationData); |
|
| 64 | + break; |
|
| 65 | + default: |
|
| 66 | + $this->outputPlainFormat($io, $delegationData); |
|
| 67 | + break; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return 0; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Collect all delegation data in a structured format |
|
| 75 | + */ |
|
| 76 | + private function collectDelegationData(): array { |
|
| 77 | + $result = []; |
|
| 78 | + $sections = $this->settingManager->getAdminSections(); |
|
| 79 | + |
|
| 80 | + foreach ($sections as $sectionPriority) { |
|
| 81 | + foreach ($sectionPriority as $section) { |
|
| 82 | + $sectionSettings = $this->settingManager->getAdminSettings($section->getId()); |
|
| 83 | + $delegatedSettings = array_reduce($sectionSettings, [$this, 'getDelegatedSettings'], []); |
|
| 84 | + |
|
| 85 | + if (empty($delegatedSettings)) { |
|
| 86 | + continue; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $result[] = [ |
|
| 90 | + 'id' => $section->getID(), |
|
| 91 | + 'name' => $section->getName() ?: $section->getID(), |
|
| 92 | + 'settings' => $this->formatSettingsData($delegatedSettings) |
|
| 93 | + ]; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return $result; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Format settings data for consistent output |
|
| 102 | + */ |
|
| 103 | + private function formatSettingsData(array $settings): array { |
|
| 104 | + return array_map(function (IDelegatedSettings $setting) { |
|
| 105 | + $className = get_class($setting); |
|
| 106 | + $groups = array_map( |
|
| 107 | + static fn (AuthorizedGroup $group) => $group->getGroupId(), |
|
| 108 | + $this->authorizedGroupService->findExistingGroupsForClass($className) |
|
| 109 | + ); |
|
| 110 | + natsort($groups); |
|
| 111 | + |
|
| 112 | + return [ |
|
| 113 | + 'name' => $setting->getName() ?: 'Global', |
|
| 114 | + 'className' => $className, |
|
| 115 | + 'priority' => $setting->getPriority(), |
|
| 116 | + 'delegatedGroups' => $groups, |
|
| 117 | + ]; |
|
| 118 | + }, $settings); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Output data in plain table format |
|
| 123 | + */ |
|
| 124 | + private function outputPlainFormat(SymfonyStyle $io, array $data): void { |
|
| 125 | + $io->title('Current delegations'); |
|
| 126 | + $headers = ['Name', 'SettingId', 'Delegated to groups']; |
|
| 127 | + |
|
| 128 | + foreach ($data as $section) { |
|
| 129 | + $io->section('Section: ' . $section['id']); |
|
| 130 | + |
|
| 131 | + $tableData = array_map(static function (array $setting) { |
|
| 132 | + return [ |
|
| 133 | + $setting['name'], |
|
| 134 | + $setting['className'], |
|
| 135 | + implode(', ', $setting['delegatedGroups']), |
|
| 136 | + ]; |
|
| 137 | + }, $section['settings']); |
|
| 138 | + |
|
| 139 | + $io->table($headers, $tableData); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Validate the output format parameter |
|
| 145 | + */ |
|
| 146 | + private function validateOutputFormat(string $format): bool { |
|
| 147 | + return in_array($format, [ |
|
| 148 | + self::OUTPUT_FORMAT_PLAIN, |
|
| 149 | + self::OUTPUT_FORMAT_JSON, |
|
| 150 | + self::OUTPUT_FORMAT_JSON_PRETTY |
|
| 151 | + ], true); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @param IDelegatedSettings[] $settings |
|
| 156 | + * @param array $innerSection |
|
| 157 | + */ |
|
| 158 | + private function getDelegatedSettings(array $settings, array $innerSection): array { |
|
| 159 | + return array_merge($settings, array_filter($innerSection, fn (ISettings $setting) => $setting instanceof IDelegatedSettings)); |
|
| 160 | + } |
|
| 161 | 161 | } |