关于Yii2 yii\web\Application 的疑问

  • index.php
$application = new yii\web\Application($config);
$application->run();
  • yii\base\Application::__construct()
public function __construct($config = [])
{
    Yii::$app = $this;
    static::setInstance($this);
    $this->state = self::STATE_BEGIN;

    $this->preInit($config);
    $this->registerErrorHandler($config);
    
    Component::__construct($config);

    var_dump($this);die; // 在这里输出 $this
}
  • 输出结果
object(yii\web\Application)#5 (33) {
    ......
    ["_modules":"yii\base\Module":private]=>
        array(3) {
            ["debug"]=> object(yii\debug\Module)#33 (28) {
                ......
            }
            ["gii"]=> object(yii\gii\Module)#113 (21) {
                ......
            }
            ...
        }
    ["_definitions":"yii\di\ServiceLocator":private]=>
        array(24) {
            ["errorHandler"]=> .....
            ["request"]=> ......
            ["log"]=> ......
            ......
        }
    ......
}

问题:object(yii\web\Application)_modules_definitions 中的属性是在哪里赋值的?

阅读 2.9k
1 个回答

首先,你的 $config 数组中一定包含以下元素:

$config = [
    //others
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module',
        ], 
        'gii'   => [
            'class' => 'yii\gii\Module',
        ];
    ],
    //others
]

这里说明一下继承关系:

class yii\base\Application extends yii\base\Module

class yii\base\Module extends yii\di\ServiceLocator

class yii\di\ServiceLocator extends yii\base\Component

class yii\base\Component extends yii\base\Object

  • yiibaseApplication::__construct() 方法注解
public function __construct($config = [])
{
    Yii::$app = $this;
    //将\yii\base\Application中的所有的属性和方法交给Yii::$app->loadedModules数组中
    static::setInstance($this);

    $this->state = self::STATE_BEGIN;

    //加载配置文件的框架信息 如:设置别名,设置框架路径等等最为重要的是给加载默认组件
    $this->preInit($config);

    //加载配置文件中的异常组件
    $this->registerErrorHandler($config);

    // 调用父类的 __construct。
    // 由于Component类并没有__construct函数
    // 这里实际调用的是 `yii\base\Object__construct($config)`
    Component::__construct($config);
}

上面方法中 Component::__construct($config) 会调用 yii\base\Object::__construct() 方法

  • yiibaseObject::__construct() 方法注解
public function __construct($config = [])
{
    if (!empty($config)) {
        // 将配置文件里面的所有配置信息赋值给Object。
        // 由于Object是大部分类的基类,
        // 实际上也就是有配置信息赋值给了yii\web\Application的对象
        Yii::configure($this, $config);
    }
    $this->init();
}

一、下面只是为了说明 'components' => [ 'log' => [...]] 从哪来,若不关心可以直接看 第二步。

  • 先看 $this->preInit($config);,即 yii\base\Application::preInit(&$config)
public function preInit(&$config)
{
    //others...
    
    // merge core components with custom components
    // 合并核心组件和自定义组件
    foreach ($this->coreComponents() as $id => $component) {
        if (!isset($config['components'][$id])) {
            // 若自定义组件中没有设置该核心组件配置信息,直接使用核心组件默认配置
            $config['components'][$id] = $component;
        } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
            // 若自定义组件有设置该核心组件配置信息,但是没有设置 'class'属性,则添加该class属性
            $config['components'][$id]['class'] = $component['class'];
        }
    }
}

/**
 * Returns the configuration of core application components.
 * 返回核心应用组件的配置
 * @see set()
 */
public function coreComponents()
{
    return [
        // 日志分配器组件
        'log' => ['class' => 'yii\log\Dispatcher'],
        
        //others...
    ];
}
  • 经过 $this->preInit($config);, 我们得到的 $config
$config = [
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module',
        ], 
        'gii'   => [
            'class' => 'yii\gii\Module',
        ];
    ],
    'components' => [
        'log'   => [
            'class' => 'yii\\log\\Dispatcher',
        ],
        
        //others...
    ]
    //others...
]


上面只是为了说明 'components' => [ 'log' => [...]] 从哪来

二、重点在这里

  • yii\base\Object::__construct($config = []) 中的 Yii::configure($this, $config);
public static function configure($object, $properties)
{
    // 只是遍历配置信息,赋值给当前对象
    foreach ($properties as $name => $value) {
        $object->$name = $value;
    }
    return $object;
}
  • 这里我们要配合 yii\base\Object::__set($name, $value)
/**
 * 为实例不存在的属性赋值时调用
 *
 * Do not call this method directly as it is a PHP magic method that
 * will be implicitly called when executing `$object->property = $value;`.
 * 这个是PHP的魔术方法,会在执行 `$object->property = $value;` 的时候自动调用。
 */
public function __set($name, $value)
{
    // setter函数的函数名
    // 由于php中方法名称不区分大小写,所以setproperty() 等价于 setProperty()
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // 调用setter函数
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        // 如果只有getter没有setter 则为只读属性
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

当前情景下的 $object 我们可以认为是 yii\base\Application 的对象 $app

  • 当遍历到:
$app->modules =  [
    'debug' => [
        'class' => 'yii\debug\Module',
    ], 
    'gii'   => [
        'class' => 'yii\gii\Module',
    ];
]

这里会调用 yii\base\Module::setModules($modules) 方法

public function setModules($modules)
{
    foreach ($modules as $id => $module) {
        $this->_modules[$id] = $module;
    }
}

这样便有了问题中的

["_modules":"yii\base\Module":private]=>
    array(3) {
        ["debug"]=> object(yii\debug\Module)#33 (28) {
            ......
        }
        ["gii"]=> object(yii\gii\Module)#113 (21) {
            ......
        }
        ...
    }
  • 同样的道理,当遍历到:
$app->components =  [
    'log'   => [
        'class' => 'yii\\log\\Dispatcher',
    ],
]
  • 这里会调用 yii\di\ServiceLocator::setComponents($components) 方法
public function setComponents($components)
{
    foreach ($components as $id => $component) {
        $this->set($id, $component);
    }
}

public function set($id, $definition)
{
    // others ...

    if (is_object($definition) || is_callable($definition, true)) {
        // an object, a class name, or a PHP callable
        $this->_definitions[$id] = $definition;
    } elseif (is_array($definition)) {
        // 定义如果是个数组,要确保数组中具有 class 元素
        // a configuration array
        if (isset($definition['class'])) {
            // 定义的过程,只是写入了 $_definitions 数组
            $this->_definitions[$id] = $definition;
        } else {
            throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
        }
    } else {
        throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
    }
}

这样便有了问题中的

["_definitions":"yii\di\ServiceLocator":private]=>
        array(24) {
            ["errorHandler"]=> .....
            ["request"]=> ......
            ["log"]=> ......
            ......
        }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进