/* Decoded by unphp.net */ createController($command); if ($result === false) { $name = $this->ansiFormat($command, Console::FG_YELLOW); throw new Exception("No help for unknown command "{$name}"."); } list($controller, $actionID) = $result; $actions = $this->getActions($controller); if ($actionID !== '' || count($actions) === 1 && $actions[0] === $controller->defaultAction) { $this->getSubCommandHelp($controller, $actionID); } else { $this->getCommandHelp($controller); } } else { $this->getDefaultHelp(); } } public function actionList() { foreach ($this->getCommandDescriptions() as $command => $description) { $result = Yii::$app->createController($command); list($controller, $actionID) = $result; $actions = $this->getActions($controller); $prefix = $controller->getUniqueId(); if ($controller->createAction($controller->defaultAction) !== null) { $this->stdout("{$prefix} "); } foreach ($actions as $action) { $this->stdout("{$prefix}/{$action} "); } } } public function actionListActionOptions($action) { $result = Yii::$app->createController($action); if ($result === false || !$result[0] instanceof Controller) { return; } list($controller, $actionID) = $result; $action = $controller->createAction($actionID); if ($action === null) { return; } foreach ($controller->getActionArgsHelp($action) as $argument => $help) { $description = preg_replace("~\R~", '', addcslashes($help["comment"], ":")) ?: $argument; $this->stdout($argument . ":" . $description . "\xa"); } $this->stdout(" "); foreach ($controller->getActionOptionsHelp($action) as $argument => $help) { $description = preg_replace("~\R~", '', addcslashes($help["comment"], ":")); $this->stdout("--" . $argument . ($description ? ":" . $description : '') . "\xa"); } } public function actionUsage($action) { $result = Yii::$app->createController($action); if ($result === false || !$result[0] instanceof Controller) { return; } list($controller, $actionID) = $result; $action = $controller->createAction($actionID); if ($action === null) { return; } $scriptName = $this->getScriptName(); if ($action->id === $controller->defaultAction) { $this->stdout($scriptName . " " . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW)); } else { $this->stdout($scriptName . " " . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW)); } foreach ($controller->getActionArgsHelp($action) as $name => $arg) { if ($arg["required"]) { $this->stdout(" <" . $name . ">", Console::FG_CYAN); } else { $this->stdout(" [" . $name . "]", Console::FG_CYAN); } } $this->stdout("\xa"); } public function getCommands() { $commands = $this->getModuleCommands(Yii::$app); sort($commands); return array_filter(array_unique($commands), function ($command) { $result = Yii::$app->createController($command); if ($result === false || !$result[0] instanceof Controller) { return false; } list($controller, $actionID) = $result; $actions = $this->getActions($controller); return $actions !== array(); }); } protected function getCommandDescriptions() { $descriptions = array(); foreach ($this->getCommands() as $command) { $result = Yii::$app->createController($command); list($controller, $actionID) = $result; $descriptions[$command] = $controller->getHelpSummary(); } return $descriptions; } public function getActions($controller) { $actions = array_keys($controller->actions()); $class = new \ReflectionClass($controller); foreach ($class->getMethods() as $method) { $name = $method->getName(); if ($name !== "actions" && $method->isPublic() && !$method->isStatic() && strncmp($name, "action", 6) === 0) { $actions[] = $this->camel2id(substr($name, 6)); } } sort($actions); return array_unique($actions); } protected function getModuleCommands($module) { $prefix = $module instanceof Application ? '' : $module->getUniqueId() . "/"; $commands = array(); foreach (array_keys($module->controllerMap) as $id) { $commands[] = $prefix . $id; } foreach ($module->getModules() as $id => $child) { if (($child = $module->getModule($id)) === null) { continue; } foreach ($this->getModuleCommands($child) as $command) { $commands[] = $command; } } $controllerPath = $module->getControllerPath(); if (is_dir($controllerPath)) { $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($controllerPath, \RecursiveDirectoryIterator::KEY_AS_PATHNAME)); $iterator = new \RegexIterator($iterator, "/.*Controller\.php$/", \RecursiveRegexIterator::GET_MATCH); foreach ($iterator as $matches) { $file = $matches[0]; $relativePath = str_replace($controllerPath, '', $file); $class = strtr($relativePath, array("/" => "\", ".php" => '')); $controllerClass = $module->controllerNamespace . $class; if ($this->validateControllerClass($controllerClass)) { $dir = ltrim(pathinfo($relativePath, PATHINFO_DIRNAME), "\/"); $command = Inflector::camel2id(substr(basename($file), 0, -14), "-", true); if (!empty($dir)) { $command = $dir . "/" . $command; } $commands[] = $prefix . $command; } } } return $commands; } protected function validateControllerClass($controllerClass) { if (class_exists($controllerClass)) { $class = new \ReflectionClass($controllerClass); return !$class->isAbstract() && $class->isSubclassOf("yii\console\Controller"); } return false; } protected function getDefaultHelp() { $commands = $this->getCommandDescriptions(); $this->stdout($this->getDefaultHelpHeader()); if (empty($commands)) { $this->stdout(" No commands are found.\xa\xa", Console::BOLD); return; } $this->stdout("\xaThe following commands are available:\xa ", Console::BOLD); $maxLength = 0; foreach ($commands as $command => $description) { $result = Yii::$app->createController($command); list($controller, $actionID) = $result; $actions = $this->getActions($controller); $prefix = $controller->getUniqueId(); foreach ($actions as $action) { $string = $prefix . "/" . $action; if ($action === $controller->defaultAction) { $string .= " (default)"; } $maxLength = max($maxLength, strlen($string)); } } foreach ($commands as $command => $description) { $result = Yii::$app->createController($command); list($controller, $actionID) = $result; $actions = $this->getActions($controller); $this->stdout("- " . $this->ansiFormat($command, Console::FG_YELLOW)); $this->stdout(str_repeat(" ", $maxLength + 4 - strlen($command))); $this->stdout(Console::wrapText($description, $maxLength + 4 + 2), Console::BOLD); $this->stdout(" "); $prefix = $controller->getUniqueId(); foreach ($actions as $action) { $string = " " . $prefix . "/" . $action; $this->stdout(" " . $this->ansiFormat($string, Console::FG_GREEN)); if ($action === $controller->defaultAction) { $string .= " (default)"; $this->stdout(" (default)", Console::FG_YELLOW); } $summary = $controller->getActionHelpSummary($controller->createAction($action)); if ($summary !== '') { $this->stdout(str_repeat(" ", $maxLength + 4 - strlen($string))); $this->stdout(Console::wrapText($summary, $maxLength + 4 + 2)); } $this->stdout("\xa"); } $this->stdout("\xa"); } $scriptName = $this->getScriptName(); $this->stdout(" To see the help of each command, enter:\xa", Console::BOLD); $this->stdout("\xa {$scriptName} " . $this->ansiFormat("help", Console::FG_YELLOW) . " " . $this->ansiFormat("", Console::FG_CYAN) . " \xa"); } protected function getCommandHelp($controller) { $controller->color = $this->color; $this->stdout("\xaDESCRIPTION ", Console::BOLD); $comment = $controller->getHelp(); if ($comment !== '') { $this->stdout(" {$comment}\xa "); } $actions = $this->getActions($controller); if (!empty($actions)) { $this->stdout(" SUB-COMMANDS ", Console::BOLD); $prefix = $controller->getUniqueId(); $maxlen = 5; foreach ($actions as $action) { $len = strlen($prefix . "/" . $action) + 2 + ($action === $controller->defaultAction ? 10 : 0); $maxlen = max($maxlen, $len); } foreach ($actions as $action) { $this->stdout("- " . $this->ansiFormat($prefix . "/" . $action, Console::FG_YELLOW)); $len = strlen($prefix . "/" . $action) + 2; if ($action === $controller->defaultAction) { $this->stdout(" (default)", Console::FG_GREEN); $len += 10; } $summary = $controller->getActionHelpSummary($controller->createAction($action)); if ($summary !== '') { $this->stdout(str_repeat(" ", $maxlen - $len + 2) . Console::wrapText($summary, $maxlen + 2)); } $this->stdout(" "); } $scriptName = $this->getScriptName(); $this->stdout(" To see the detailed information about individual sub-commands, enter: "); $this->stdout(" {$scriptName} " . $this->ansiFormat("help", Console::FG_YELLOW) . " " . $this->ansiFormat("", Console::FG_CYAN) . "\xa "); } } protected function getSubCommandHelp($controller, $actionID) { $action = $controller->createAction($actionID); if ($action === null) { $name = $this->ansiFormat(rtrim($controller->getUniqueId() . "/" . $actionID, "/"), Console::FG_YELLOW); throw new Exception("No help for unknown sub-command "{$name}"."); } $description = $controller->getActionHelp($action); if ($description !== '') { $this->stdout("\xaDESCRIPTION ", Console::BOLD); $this->stdout(" {$description}\xa "); } $this->stdout("\xaUSAGE ", Console::BOLD); $scriptName = $this->getScriptName(); if ($action->id === $controller->defaultAction) { $this->stdout($scriptName . " " . $this->ansiFormat($controller->getUniqueId(), Console::FG_YELLOW)); } else { $this->stdout($scriptName . " " . $this->ansiFormat($action->getUniqueId(), Console::FG_YELLOW)); } $args = $controller->getActionArgsHelp($action); foreach ($args as $name => $arg) { if ($arg["required"]) { $this->stdout(" <" . $name . ">", Console::FG_CYAN); } else { $this->stdout(" [" . $name . "]", Console::FG_CYAN); } } $options = $controller->getActionOptionsHelp($action); $options[\yii\console\Application::OPTION_APPCONFIG] = array("type" => "string", "default" => null, "comment" => "custom application configuration file path.\xaIf not set, default application configuration is used."); ksort($options); if (!empty($options)) { $this->stdout(" [...options...]", Console::FG_RED); } $this->stdout("\xa "); if (!empty($args)) { foreach ($args as $name => $arg) { $this->stdout($this->formatOptionHelp("- " . $this->ansiFormat($name, Console::FG_CYAN), $arg["required"], $arg["type"], $arg["default"], $arg["comment"]) . " "); } } if (!empty($options)) { $this->stdout("\xaOPTIONS ", Console::BOLD); foreach ($options as $name => $option) { $this->stdout($this->formatOptionHelp($this->ansiFormat("--" . $name . $this->formatOptionAliases($controller, $name), Console::FG_RED, empty($option["required"]) ? Console::FG_RED : Console::BOLD), !empty($option["required"]), $option["type"], $option["default"], $option["comment"]) . " \xa"); } } } protected function formatOptionHelp($name, $required, $type, $defaultValue, $comment) { $comment = trim((string) $comment); $type = trim((string) $type); if (strncmp($type, "bool", 4) === 0) { $type = "boolean, 0 or 1"; } if ($defaultValue !== null && !is_array($defaultValue)) { if ($type === null) { $type = gettype($defaultValue); } if (is_bool($defaultValue)) { $defaultValue = (int) $defaultValue; } if (is_string($defaultValue)) { $defaultValue = "'" . $defaultValue . "'"; } else { $defaultValue = var_export($defaultValue, true); } $doc = "{$type} (defaults to {$defaultValue})"; } else { $doc = $type; } if ($doc === '') { $doc = $comment; } elseif ($comment !== '') { $doc .= " " . preg_replace("/^/m", " ", $comment); } $name = $required ? "{$name} (required)" : $name; return $doc === '' ? $name : "{$name}: {$doc}"; } protected function formatOptionAliases($controller, $option) { foreach ($controller->optionAliases() as $name => $value) { if (Inflector::camel2id($value, "-", true) === $option) { return ", -" . $name; } } return ''; } protected function getScriptName() { return basename(Yii::$app->request->scriptFile); } protected function getDefaultHelpHeader() { return "\xaThis is Yii version " . \Yii::getVersion() . ".\xa"; } private function camel2id($name) { return mb_strtolower(trim(preg_replace("/\p{Lu}/u", "-\0", $name), "-"), "UTF-8"); } } ?>