A question about yii2 framework

新手上路,请多包涵

when I read the codes of yiicms,  I found a problem that puzzles me all the time. The problem is discribed aretroactively s follow.

$app = new yii\web\Application($config);

yiibaseapplication::__construct($config);

Component::__construct($config);

  1. add the debug statement that var_dump($this) later.  
  2. found as follow.

public "bootstrap" =>
      array (size=3)
          0=>string  "log"
          1=>string "debug"
           2=>string "gii"
private "_modules" (yiibasemodule)=>
       array (size=3)
            'backend'  =>....
            'debug' =>...
            'gii'=>...
private  "_definitions"(yiidiservicelocator)=>
        array  (size=7)
           ...
           'log'=>...
           ...

  1. wonder when and where the private attributes _modules and _definitions  are inserted by values 'debug'  'gii'  and 'log'?
阅读 2.1k
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;
    }
}

这样便有了问题中的

private "_modules" (yiibasemodule)=>
       array (size=3)
            'backend'  =>....
            'debug' =>...
            'gii'=>...
  • 同样的道理,当遍历到:
$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));
    }
}

这样便有了问题中的

private  "_definitions"(yiidiservicelocator)=>
        array  (size=7)
           ...
           'log'=>...
           ...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进