yii2框架随笔20

今天继续来看console/Controller.php

<?php
/**
     * Returns a value indicating whether ANSI color is enabled.
     *返回一个值指示是否启用ANSI颜色
     * ANSI color is enabled only if [[color]] is set true or is not set
     * and the terminal supports ANSI color.
     *ANSI颜色只有在启用[[color]]是真正的或没有设置

     *和终端支持ANSI的颜色。
     * @param resource $stream the stream to check.
     * @return boolean Whether to enable ANSI style in output.
     */
    public function isColorEnabled($stream = STDOUT)
    {
        return $this->color === null ? Console::streamSupportsAnsiColors($stream) : $this->color;
    }
    /**
     * Runs an action with the specified action ID and parameters.
     * 运行一个动作与ID指定的操作和参数。
     * If the action ID is empty, the method will use [[defaultAction]].
     * 如果行动ID为空,该方法将使用[[defaultAction]]。
     * @param string $id the ID of the action to be executed.
     * @param array $params the parameters (name-value pairs) to be passed to the action.
     * @return integer the status of the action execution. 0 means normal, other values mean abnormal.
     * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
     * @throws Exception if there are unknown options or missing arguments
     * @see createAction
     */
    public function runAction($id, $params = [])
    {
        if (!empty($params)) {
            // populate options here so that they are available in beforeAction().
            // 获取允许的参数,可以在beforeAction中拿到
            $options = $this->options($id);
            foreach ($params as $name => $value) {
                if (in_array($name, $options, true)) {
                    // 如果参数在允许的options,才会将值赋给controller的属性
                    $default = $this->$name;
                    // 如果该属性的默认值是数组,就根据逗号和空格(包含" ", 
, 	, 
, f)的组合分隔value值
                    $this->$name = is_array($default) ? preg_split('/s*,s*/', $value) : $value;
                    unset($params[$name]);
                } elseif (!is_int($name)) {
                    throw new Exception(Yii::t('yii', 'Unknown option: --{name}', ['name' => $name]));
                }
            }
        }
        return parent::runAction($id, $params);
    }
    /**
     * Binds the parameters to the action.
     * 将参数绑定到行动.
     * This method is invoked by [[Action]] when it begins to run with the given parameters.
     * 由[[Action]]调用该方法时开始使用给定的运行参数。
     * This method will first bind the parameters with the [[options()|options]]
     * 该方法首先将绑定的参数与[[options()|options]]
     * available to the action. It then validates the given arguments.
     * 可用的行动。然后验证给定的参数。
     * @param Action $action the action to be bound with parameters
     * @param array $params the parameters to be bound to the action
     * @return array the valid parameters that the action can run with.
     * @throws Exception if there are unknown options or missing arguments
     */
    public function bindActionParams($action, $params)
    {
        if ($action instanceof InlineAction) {
            // 如果action是InlineAction的实例,就new一个controller上actionMethod的ReflectionMethod实例
            $method = new ReflectionMethod($this, $action->actionMethod);
        } else {
            // 否则,就new一个action上run方法的ReflectionMethod实例
            $method = new ReflectionMethod($action, 'run');
        }
        // 取参数的值
        $args = array_values($params);
        $missing = [];
        foreach ($method->getParameters() as $i => $param) {
            if ($param->isArray() && isset($args[$i])) {
                // $param是数组,并且存在$args[$i],就将$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;
    }
    /**
     * Formats a string with ANSI codes
     * 与ANSI编码格式字符串
     * You may pass additional parameters using the constants defined in [[yiihelpersConsole]].
     * 你可以通过使用常量定义在其他参数[[yiihelpersConsole]].
     * Example:
     *
     * ~~~
     * echo $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
     * ~~~
     *
     * @param string $string the string to be formatted
     * @return string
     */
    public function ansiFormat($string)
    {
        if ($this->isColorEnabled()) {//如果为真,执行$string赋值操作。
            $args = func_get_args();
            array_shift($args);
            $string = Console::ansiFormat($string, $args);
        }
        return $string;
    }
原文地址:https://www.cnblogs.com/taokai/p/5452374.html