|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- <?php
-
-
- namespace yii\console;
-
- use Yii;
- use yii\base\Action;
- use yii\base\InlineAction;
- use yii\base\InvalidRouteException;
- use yii\helpers\Console;
-
-
- class Controller extends \yii\base\Controller
- {
- const EXIT_CODE_NORMAL = 0;
- const EXIT_CODE_ERROR = 1;
-
-
-
- public $interactive = true;
-
-
- public $color;
-
-
- public $help;
-
-
-
- private $_passedOptions = [];
-
-
-
-
- public function isColorEnabled($stream = \STDOUT)
- {
- return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color;
- }
-
-
-
- public function runAction($id, $params = [])
- {
- if (!empty($params)) {
-
- $options = $this->options($id === '' ? $this->defaultAction : $id);
- if (isset($params['_aliases'])) {
- $optionAliases = $this->optionAliases();
- foreach ($params['_aliases'] as $name => $value) {
- if (array_key_exists($name, $optionAliases)) {
- $params[$optionAliases[$name]] = $value;
- } else {
- throw new Exception(Yii::t('yii', 'Unknown alias: -{name}', ['name' => $name]));
- }
- }
- unset($params['_aliases']);
- }
- foreach ($params as $name => $value) {
- if (in_array($name, $options, true)) {
- $default = $this->$name;
- if (is_array($default)) {
- $this->$name = preg_split('/\s*,\s*(?![^()]*\))/', $value);
- } elseif ($default !== null) {
- settype($value, gettype($default));
- $this->$name = $value;
- } else {
- $this->$name = $value;
- }
- $this->_passedOptions[] = $name;
- unset($params[$name]);
- } elseif (!is_int($name)) {
- throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name]));
- }
- }
- }
- if ($this->help) {
- $route = $this->id . '/' . $id;
- return Yii::$app->runAction('help', [$route]);
- }
- return parent::runAction($id, $params);
- }
-
-
-
- public function bindActionParams($action, $params)
- {
- if ($action instanceof InlineAction) {
- $method = new \ReflectionMethod($this, $action->actionMethod);
- } else {
- $method = new \ReflectionMethod($action, 'run');
- }
-
- $args = array_values($params);
-
- $missing = [];
- foreach ($method->getParameters() as $i => $param) {
- if ($param->isArray() && isset($args[$i])) {
- $args[$i] = preg_split('/\s*,\s*/', $args[$i]);
- }
- if (!isset($args[$i])) {
- if ($param->isDefaultValueAvailable()) {
- $args[$i] = $param->getDefaultValue();
- } else {
- $missing[] = $param->getName();
- }
- }
- }
-
- if (!empty($missing)) {
- throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', $missing)]));
- }
-
- return $args;
- }
-
-
-
- public function ansiFormat($string)
- {
- if ($this->isColorEnabled()) {
- $args = func_get_args();
- array_shift($args);
- $string = Console::ansiFormat($string, $args);
- }
- return $string;
- }
-
-
-
- public function stdout($string)
- {
- if ($this->isColorEnabled()) {
- $args = func_get_args();
- array_shift($args);
- $string = Console::ansiFormat($string, $args);
- }
- return Console::stdout($string);
- }
-
-
-
- public function stderr($string)
- {
- if ($this->isColorEnabled(\STDERR)) {
- $args = func_get_args();
- array_shift($args);
- $string = Console::ansiFormat($string, $args);
- }
- return fwrite(\STDERR, $string);
- }
-
-
-
- public function prompt($text, $options = [])
- {
- if ($this->interactive) {
- return Console::prompt($text, $options);
- } else {
- return isset($options['default']) ? $options['default'] : '';
- }
- }
-
-
-
- public function confirm($message, $default = false)
- {
- if ($this->interactive) {
- return Console::confirm($message, $default);
- } else {
- return true;
- }
- }
-
-
-
- public function select($prompt, $options = [])
- {
- return Console::select($prompt, $options);
- }
-
-
-
- public function options($actionID)
- {
-
- return ['color', 'interactive', 'help'];
- }
-
-
-
- public function optionAliases()
- {
- return [
- 'h' => 'help'
- ];
- }
-
-
-
- public function getOptionValues($actionID)
- {
-
- $properties = [];
- foreach ($this->options($this->action->id) as $property) {
- $properties[$property] = $this->$property;
- }
- return $properties;
- }
-
-
-
- public function getPassedOptions()
- {
- return $this->_passedOptions;
- }
-
-
-
- public function getPassedOptionValues()
- {
- $properties = [];
- foreach ($this->_passedOptions as $property) {
- $properties[$property] = $this->$property;
- }
- return $properties;
- }
-
-
-
- public function getHelpSummary()
- {
- return $this->parseDocCommentSummary(new \ReflectionClass($this));
- }
-
-
-
- public function getHelp()
- {
- return $this->parseDocCommentDetail(new \ReflectionClass($this));
- }
-
-
-
- public function getActionHelpSummary($action)
- {
- return $this->parseDocCommentSummary($this->getActionMethodReflection($action));
- }
-
-
-
- public function getActionHelp($action)
- {
- return $this->parseDocCommentDetail($this->getActionMethodReflection($action));
- }
-
-
-
- public function getActionArgsHelp($action)
- {
- $method = $this->getActionMethodReflection($action);
- $tags = $this->parseDocCommentTags($method);
- $params = isset($tags['param']) ? (array) $tags['param'] : [];
-
- $args = [];
-
-
- foreach ($method->getParameters() as $i => $reflection) {
- $name = $reflection->getName();
- $tag = isset($params[$i]) ? $params[$i] : '';
- if (preg_match('/^(\S+)\s+(\$\w+\s+)?(.*)/s', $tag, $matches)) {
- $type = $matches[1];
- $comment = $matches[3];
- } else {
- $type = null;
- $comment = $tag;
- }
- if ($reflection->isDefaultValueAvailable()) {
- $args[$name] = [
- 'required' => false,
- 'type' => $type,
- 'default' => $reflection->getDefaultValue(),
- 'comment' => $comment,
- ];
- } else {
- $args[$name] = [
- 'required' => true,
- 'type' => $type,
- 'default' => null,
- 'comment' => $comment,
- ];
- }
- }
- return $args;
- }
-
-
-
- public function getActionOptionsHelp($action)
- {
- $optionNames = $this->options($action->id);
- if (empty($optionNames)) {
- return [];
- }
-
- $class = new \ReflectionClass($this);
- $options = [];
- foreach ($class->getProperties() as $property) {
- $name = $property->getName();
- if (!in_array($name, $optionNames, true)) {
- continue;
- }
- $defaultValue = $property->getValue($this);
- $tags = $this->parseDocCommentTags($property);
- if (isset($tags['var']) || isset($tags['property'])) {
- $doc = isset($tags['var']) ? $tags['var'] : $tags['property'];
- if (is_array($doc)) {
- $doc = reset($doc);
- }
- if (preg_match('/^(\S+)(.*)/s', $doc, $matches)) {
- $type = $matches[1];
- $comment = $matches[2];
- } else {
- $type = null;
- $comment = $doc;
- }
- $options[$name] = [
- 'type' => $type,
- 'default' => $defaultValue,
- 'comment' => $comment,
- ];
- } else {
- $options[$name] = [
- 'type' => null,
- 'default' => $defaultValue,
- 'comment' => '',
- ];
- }
- }
- return $options;
- }
-
- private $_reflections = [];
-
-
-
- protected function getActionMethodReflection($action)
- {
- if (!isset($this->_reflections[$action->id])) {
- if ($action instanceof InlineAction) {
- $this->_reflections[$action->id] = new \ReflectionMethod($this, $action->actionMethod);
- } else {
- $this->_reflections[$action->id] = new \ReflectionMethod($action, 'run');
- }
- }
- return $this->_reflections[$action->id];
- }
-
-
-
- protected function parseDocCommentTags($reflection)
- {
- $comment = $reflection->getDocComment();
- $comment = "@description \n" . strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))), "\r", '');
- $parts = preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
- $tags = [];
- foreach ($parts as $part) {
- if (preg_match('/^(\w+)(.*)/ms', trim($part), $matches)) {
- $name = $matches[1];
- if (!isset($tags[$name])) {
- $tags[$name] = trim($matches[2]);
- } elseif (is_array($tags[$name])) {
- $tags[$name][] = trim($matches[2]);
- } else {
- $tags[$name] = [$tags[$name], trim($matches[2])];
- }
- }
- }
- return $tags;
- }
-
-
-
- protected function parseDocCommentSummary($reflection)
- {
- $docLines = preg_split('~\R~u', $reflection->getDocComment());
- if (isset($docLines[1])) {
- return trim($docLines[1], "\t *");
- }
- return '';
- }
-
-
-
- protected function parseDocCommentDetail($reflection)
- {
- $comment = strtr(trim(preg_replace('/^\s*\**( |\t)?/m', '', trim($reflection->getDocComment(), '/'))), "\r", '');
- if (preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
- $comment = trim(substr($comment, 0, $matches[0][1]));
- }
- if ($comment !== '') {
- return rtrim(Console::renderColoredString(Console::markdownToAnsi($comment)));
- }
- return '';
- }
- }
|