/** * Display the application version * * @param Console $console * @return int */ public function showVersion(Console $console) { $console->writeLine($console->colorize($this->name . ',', Color::GREEN) . ' version ' . $console->colorize($this->version, Color::BLUE)); $console->writeLine(''); return 0; }
/** * Build Console usage information by querying currently loaded modules. * * @param ConsoleAdapter $console * @param string $scriptName * @param ModuleManagerInterface $moduleManager * @return string * @throws RuntimeException */ protected function getConsoleUsage(ConsoleAdapter $console, $scriptName, ModuleManagerInterface $moduleManager = null) { /* * Loop through all loaded modules and collect usage info */ $usageInfo = array(); if ($moduleManager !== null) { foreach ($moduleManager->getLoadedModules(false) as $name => $module) { // Strict-type on ConsoleUsageProviderInterface, or duck-type // on the method it defines if (!$module instanceof ConsoleUsageProviderInterface && !method_exists($module, 'getConsoleUsage')) { continue; // this module does not provide usage info } // We prepend the usage by the module name (printed in red), so that each module is // clearly visible by the user $moduleName = sprintf("%s\n%s\n%s\n", str_repeat('-', $console->getWidth()), $name, str_repeat('-', $console->getWidth())); $moduleName = $console->colorize($moduleName, ColorInterface::RED); $usage = $module->getConsoleUsage($console); // Normalize what we got from the module or discard if (is_array($usage) && !empty($usage)) { array_unshift($usage, $moduleName); $usageInfo[$name] = $usage; } elseif (is_string($usage) && $usage != '') { $usageInfo[$name] = array($moduleName, $usage); } } } /* * Handle an application with no usage information */ if (!count($usageInfo)) { // TODO: implement fetching available console routes from router return ''; } /* * Transform arrays in usage info into columns, otherwise join everything together */ $result = ''; $table = false; $tableCols = 0; $tableType = 0; foreach ($usageInfo as $moduleName => $usage) { if (!is_string($usage) && !is_array($usage)) { throw new RuntimeException(sprintf('Cannot understand usage info for module "%s"', $moduleName)); } if (is_string($usage)) { // It's a plain string - output as is $result .= $usage . "\n"; continue; } // It's an array, analyze it foreach ($usage as $a => $b) { /* * 'invocation method' => 'explanation' */ if (is_string($a) && is_string($b)) { if (($tableCols !== 2 || $tableType != 1) && $table !== false) { // render last table $result .= $this->renderTable($table, $tableCols, $console->getWidth()); $table = false; // add extra newline for clarity $result .= "\n"; } // Colorize the command $a = $console->colorize($scriptName . ' ' . $a, ColorInterface::GREEN); $tableCols = 2; $tableType = 1; $table[] = array($a, $b); continue; } /* * array('--param', '--explanation') */ if (is_array($b)) { if ((count($b) != $tableCols || $tableType != 2) && $table !== false) { // render last table $result .= $this->renderTable($table, $tableCols, $console->getWidth()); $table = false; // add extra newline for clarity $result .= "\n"; } $tableCols = count($b); $tableType = 2; $table[] = $b; continue; } /* * 'A single line of text' */ if ($table !== false) { // render last table $result .= $this->renderTable($table, $tableCols, $console->getWidth()); $table = false; // add extra newline for clarity $result .= "\n"; } $tableType = 0; $result .= $b . "\n"; } } // Finish last table if ($table !== false) { $result .= $this->renderTable($table, $tableCols, $console->getWidth()); } return $result; }
/** * Prepare a string that will be rendered in color. * * @param string $string * @param null|int $color Foreground color * @param null|int $bgColor Background color * * @return string */ public function colorize($string, $color = null, $bgColor = null) { return $this->adapter->colorize($string, $color, $bgColor); }