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;
                    // 如果该属性的默认值是数组,就根据逗号和空格(包含" ", \r, \t, \n, \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 [[\yii\helpers\Console]].
     * 你可以通过使用常量定义在其他参数[[\yii\helpers\Console]].
     * 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;
    }
时间: 2024-10-11 04:38:04

yii2框架随笔20的相关文章

yii2框架随笔21

今天来看一下BaseYii.php <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii; use yii\base\InvalidConfigException; use yii\base\InvalidParamExceptio

yii2框架随笔4

接下来我们继续了解Component.php 目录为:vendor/yiisoft/yii2/base/Component.php (接上次的代码) /** * Sets the value of a component property. *设置一个组件属性的值. * This method will check in the following order and act accordingly: *这种方法将检查以下顺序并采取相应的行动: * - a property defined by

yii2框架随笔30

今天来看console/Application.php <?php /** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ namespace yii\console; use Yii; use yii\base\InvalidRouteException; /** * A

yii2框架随笔23

今天继续阅读BaseYii.php <?php /** * Class autoload loader. * 自动装载类加载程序. * This method is invoked automatically when PHP sees an unknown class. * PHP用该方法自动调用一个未知类. * The method will attempt to include the class file according to the following procedure: * 方

yii2框架随笔22

今天继续来看BaseYii.php <?php /** * Returns the root alias part of a given alias. * 返回根别名的一部分,一个给定的别名. * A root alias is an alias that has been registered via [[setAlias()]] previously. * 根别名是已经注册别名通过[[setAlias()]]. * If a given alias matches multiple root

yii2框架随笔3

今天开始阅读vendor/yiisoft/yii2/base/Action.php <?php namespace yii\base;//命名空间 use Yii;//加载Yii文件夹下的Yii.php /** * Action is the base class for all controller action classes. * * Action provides a way to reuse action method code. An action method in an Acti

yii2框架随笔36

今天来看一下vendor/yiisoft/yii2/base/Event.php <?php namespace yii\base; //事件是所有事件类的基类.它封装了参数与事件相关联. //如果一个事件处理程序集[[进行]]是真的,其余的,uninvoked处理程序将不再被称为处理事件. //另外,添加一个事件处理程序时,额外的数据可能被传递和可以通过[[数据]]属性调用事件处理程序时. class Event extends Object { /** * @var string the e

yii2框架随笔37

vendor/yiisoft/yii2/base/Event.php <?php namespace yii\base; //事件是所有事件类的基类.它封装了参数与事件相关联. //如果一个事件处理程序集[[进行]]是真的,其余的,uninvoked处理程序将不再被称为处理事件. //另外,添加一个事件处理程序时,额外的数据可能被传递和可以通过[[数据]]属性调用事件处理程序时. class Event extends Object { /** * @var string the event n

yii2框架随笔35

今天来看vendor/yiisoft/yii2/base/Event.php <?php namespace yii\base; //事件是所有事件类的基类.它封装了参数与事件相关联. //如果一个事件处理程序集[[进行]]是真的,其余的,uninvoked处理程序将不再被称为处理事件. //另外,添加一个事件处理程序时,额外的数据可能被传递和可以通过[[数据]]属性调用事件处理程序时. class Event extends Object { /** * @var string the eve