/** * Utility method to format a context string * * @param string $description Context description * @param string[] $context Context array * @param string $type Context type * @param string $fgColor1 Description foreground color DEFAULT ConsoleColors::PURPLE_F * @param string $bgColor1 Description background color DEFAULT ConsoleColors::BLACK * @param string $fgColor2 Context foreground color DEFAULT ConsoleColors::YELLOW * @param string $bgColor2 Context background color DEFAULT ConsoleColors::BLACK * * @return string The formatted context string */ private static function formatContextShortcut(string $description, array $context, string $type, string $fgColor1 = ConsoleColors::PURPLE_F, string $bgColor1 = ConsoleColors::BLACK, string $fgColor2 = ConsoleColors::YELLOW, string $bgColor2 = ConsoleColors::BLACK) : string { $formattedString = ''; if (isset($context[$type])) { if ($type === 'args') { $contextString = static::formatArguments($context[$type]); } else { $contextString = $context[$type]; } $formattedString = "\t" . ConsoleColors::getColoredString($description, $fgColor1, $bgColor1) . "\t" . ConsoleColors::getColoredString($contextString, $fgColor2, $bgColor2) . PHP_EOL; } return $formattedString; }
/** * Process the command entered by the user and output the result in the console * * @param string $command The command passed by the user * @param bool $executed DEFAULT false, true if the command is already executed, else false */ protected function processCommand(string $command, bool $executed = false) { $exit = false; preg_match('/^[a-zA-Z ]*/', $command, $commandName); if (!$executed) { switch (rtrim($commandName[0])) { case 'exit': $exit = true; static::out(ConsoleColors::getColoredString(static::GOODBYE, ConsoleColors::LIGHT_RED_F, ConsoleColors::GREEN) . PHP_EOL); break; case 'last cmd': static::out('The last cmd was: ' . $this->getLastCommand() . PHP_EOL); break; case 'all cmd': static::out('Commands historic:' . static::tablePrettyPrint($this->commandsHistoric) . PHP_EOL); break; case 'help': static::out('List of all command' . PHP_EOL . static::tableAssociativePrettyPrint(static::$COMMANDS)); break; default: static::out('The command : "' . $command . '" is not recognized as a command, type help to display all the commands' . PHP_EOL); break; } } static::out(PHP_EOL); if ($command !== $this->getLastCommand()) { $this->commandsHistoric[] = $command; } if (!$exit) { $this->processCommand($this->userInput()); } }