From e9d777ab248f95135cf52a09377a11491b4a9e3a Mon Sep 17 00:00:00 2001 From: tracer Date: Tue, 27 Sep 2022 19:38:01 +0200 Subject: [PATCH] initial commit --- .../Commands/CommandGroupContainer.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/Controller/Commands/CommandGroupContainer.php diff --git a/src/Controller/Commands/CommandGroupContainer.php b/src/Controller/Commands/CommandGroupContainer.php new file mode 100644 index 0000000..a96b40a --- /dev/null +++ b/src/Controller/Commands/CommandGroupContainer.php @@ -0,0 +1,73 @@ +commandGroups[] = $commandGroup; + + return $this; + } + + /** + * @return void + */ + public function printCommands(): void + { + $longestCommandLength = $this->getLongestCommandLength(); + foreach ($this->commandGroups as $commandGroup) { + $commandGroup->printCommands($longestCommandLength); + } + } + + + /** + * @return int + */ + public function getLongestCommandLength(): int + { + $longest = 0; + + foreach ($this->commandGroups as $group) { + $len = strlen(string: $group->getName()); + if ($len > $longest) { + $longest = $len; + } + } + + return $longest; + } + + + /** + * @param string $command + * @return ?CommandGroup + */ + private function findGroupByName(string $command): ?CommandGroup + { + foreach ($this->commandGroups as $group) { + if ($group->getName() === $command) { + return $group; + } + } + return null; + } + + + public function run(string $command, string $subcommand): void + { + if ($group = $this->findGroupByName(command: $command)) { + $group->exec(subcommand: $subcommand); + } else { + echo COLOR_DEFAULT . 'Unknown command ' . COLOR_YELLOW . $command . COLOR_DEFAULT . '.' . PHP_EOL; + exit(1); + } + } +} \ No newline at end of file