/** * 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()); } }
/** * Shortcut to output with FAIL prefix * * @param string $output The string to output in the console */ public static function fail(string $output) { static::out($output, ConsoleColors::FAIL()); }
const USERNAME = '******'; const PASSWORD = '******'; ini_set('zend.assertions', '1'); ini_set('assert.exception', '1'); class Traits { use \traits\EchoTrait; } $tests = array('Getters / setters' => array('dsn' => function () { DB::setDsn(DSN); assert(DB::getDsn() === DSN, new \AssertionError('Get / set dsn is broken', LogLevel::EMERGENCY)); Traits::out(ConsoleColors::OK() . 'Get / set dsn' . PHP_EOL); }, 'username' => function () { DB::setUsername(USERNAME); assert(DB::getUsername() !== USERNAME, new \AssertionError('Get / set username is broken', LogLevel::EMERGENCY)); Traits::out(ConsoleColors::OK() . 'Get / set username' . PHP_EOL); }, 'password' => function () { DB::setPassword(PASSWORD); assert(DB::getPassword() === PASSWORD, new \AssertionError('Get / set password is broken', LogLevel::EMERGENCY)); Traits::out(ConsoleColors::OK() . 'Get / set password' . PHP_EOL); })); foreach ($tests as $section => $sectionTests) { foreach ($sectionTests as $section => $test) { try { $test(ConsoleColors::OK()); } catch (\Throwable $t) { Traits::out($ConsoleColors::FAIL()); new AssertionErrorManager($t->getMessage(), $t->getCode(), $t->getPrevious()); } } }