当前位置:首页 > > 充电吧
[导读]本博客要讲的是yii整个Application的创建过程,而不是运行过程哈,读者注意,没有run啊。 阅读前须知: web的Application          extends  yii\ba

本博客要讲的是yii整个Application的创建过程,而不是运行过程哈,读者注意,没有run啊。 阅读前须知: web的Application          extends  yiibaseApplication yiibaseApplication      extends  yiibasemodule yiibasemodule            extends  yiibaseservicelocator yiibaseservicelocator  extends  yiibasecomponent yiibasecomponent      extends  yiibasepbject 其中我最关心的 __get  __set函数是在component类中定义的
1.$config该变量是我们从配置文件加载出来的,一个配置数组的合集(其中的各种require文件就不说了啊) 2.Application的创建过程

public function __construct($config = [])
{

    Yii::$app = $this;
    static::setInstance($this);
    $this->state = self::STATE_BEGIN;
    $this->preInit($config);
    $this->registerErrorHandler($config);
    Component::__construct($config);
}

解读setInstance,函数定义如下:

public static function setInstance($instance)
{
    if ($instance === null) {
        unset(Yii::$app->loadedModules[get_called_class()]);
    } else {
        Yii::$app->loadedModules[get_class($instance)] = $instance;
    }
}

现在的loadedModules,我还没有具体看不知道啥意思,不过对于我这种小白来说,可以暂时忽略 解读preInit($config)

public function preInit(&$config)
{
    if (!isset($config['id'])) {
        throw new InvalidConfigException('The "id" configuration for the Application is required.');
    }
    if (isset($config['basePath'])) {
        $this->setBasePath($config['basePath']);
        unset($config['basePath']);
    } else {
        throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
    }

    if (isset($config['vendorPath'])) {
        $this->setVendorPath($config['vendorPath']);
        unset($config['vendorPath']);
    } else {
        // set "@vendor"
        $this->getVendorPath();
    }
    if (isset($config['runtimePath'])) {
        $this->setRuntimePath($config['runtimePath']);
        unset($config['runtimePath']);
    } else {
        // set "@runtime"
        $this->getRuntimePath();
    }

    if (isset($config['timeZone'])) {
        $this->setTimeZone($config['timeZone']);
        unset($config['timeZone']);
    } elseif (!ini_get('date.timezone')) {
        $this->setTimeZone('UTC');
    }
    //以上除了id,意外,配置文件必须包含 basePath,vendorPath  runtimePath等变量,并将变量放在module类中对应的成员变量中
    //并且设置完成之后进行unset操作
    // 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'])) {
            $config['components'][$id]['class'] = $component['class'];
        }
    }
}

需要注意的就是这个coreComponent函数了,这个函数,将web的Application和base的Application的coreComponents返回的数组进行合并操作,也就是说在 整个应用中需要用到的components都需要在webApplication的coreComponents或者其父类的coreComponents中进行定义,并且组件的定义优先取用配置文件中的定义 PS:以上这几步,我们可以认为是进行预整理$config 接下来是registerErrorHandler,这个函数以后会着重看,先按下不表。 看一下Component::__construct的定义,他最终调用的是Yii::Configure

public static function configure($object, $properties)
{
    foreach ($properties as $name => $value) {

        $object->$name = $value;
    }
    return $object;
}

接下来,要说的就是这个 ->$name = $value;我们知道这个$object是web下的Application,该类有一个父类是Component,不知道的可以去看,阅读前须知 该类中定义了注入函数__set

public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    } else {
        // behavior property
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name)) {
                $behavior->$name = $value;
                return;
            }
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

对于需要组件的会有单独的处理函数(set.$name函数),我们在配置文件中会定义诸如

return [
    'components' => [
        'db' => [
            'class' => 'yiidbConnection',
            'dsn' => 'mysql:host=db1-dev.bj1.haodf.net;dbname=test',
            'username' => 'weihu_dev',
            'password' => 'hdf@haodf.com',
            'charset' => 'gbk',
        ],
        'mailer' => [
            'class' => 'yiiswiftmailerMailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

但是在ServiceLocator中定义了也就是对于配置文件中的components,定义了setComponents函数进行处理

public function setComponents($components)
{
    foreach ($components as $id => $component) {
        $this->set($id, $component);
    }
}

继续分析



public function set($id, $definition)
{
    if ($definition === null) {
        unset($this->_components[$id], $this->_definitions[$id]);
        return;
    }

    unset($this->_components[$id]);

    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)) {
        // a configuration array
        if (isset($definition['class'])) {
            $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));
    }
}

这样所有Components就会有单独的_components数组进行统一维护了!!!!(比如db,比如cache就是在这里进行组成的) 还需要说明的就是除了components意外的其他配置,


可以在配置文件中添加整个应用的事件或者行为而不是单独针对某个controller的了,其配置如下:

$config['on test']=['class'=>'test'];





本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: 驱动电源

在工业自动化蓬勃发展的当下,工业电机作为核心动力设备,其驱动电源的性能直接关系到整个系统的稳定性和可靠性。其中,反电动势抑制与过流保护是驱动电源设计中至关重要的两个环节,集成化方案的设计成为提升电机驱动性能的关键。

关键字: 工业电机 驱动电源

LED 驱动电源作为 LED 照明系统的 “心脏”,其稳定性直接决定了整个照明设备的使用寿命。然而,在实际应用中,LED 驱动电源易损坏的问题却十分常见,不仅增加了维护成本,还影响了用户体验。要解决这一问题,需从设计、生...

关键字: 驱动电源 照明系统 散热

根据LED驱动电源的公式,电感内电流波动大小和电感值成反比,输出纹波和输出电容值成反比。所以加大电感值和输出电容值可以减小纹波。

关键字: LED 设计 驱动电源

电动汽车(EV)作为新能源汽车的重要代表,正逐渐成为全球汽车产业的重要发展方向。电动汽车的核心技术之一是电机驱动控制系统,而绝缘栅双极型晶体管(IGBT)作为电机驱动系统中的关键元件,其性能直接影响到电动汽车的动力性能和...

关键字: 电动汽车 新能源 驱动电源

在现代城市建设中,街道及停车场照明作为基础设施的重要组成部分,其质量和效率直接关系到城市的公共安全、居民生活质量和能源利用效率。随着科技的进步,高亮度白光发光二极管(LED)因其独特的优势逐渐取代传统光源,成为大功率区域...

关键字: 发光二极管 驱动电源 LED

LED通用照明设计工程师会遇到许多挑战,如功率密度、功率因数校正(PFC)、空间受限和可靠性等。

关键字: LED 驱动电源 功率因数校正

在LED照明技术日益普及的今天,LED驱动电源的电磁干扰(EMI)问题成为了一个不可忽视的挑战。电磁干扰不仅会影响LED灯具的正常工作,还可能对周围电子设备造成不利影响,甚至引发系统故障。因此,采取有效的硬件措施来解决L...

关键字: LED照明技术 电磁干扰 驱动电源

开关电源具有效率高的特性,而且开关电源的变压器体积比串联稳压型电源的要小得多,电源电路比较整洁,整机重量也有所下降,所以,现在的LED驱动电源

关键字: LED 驱动电源 开关电源

LED驱动电源是把电源供应转换为特定的电压电流以驱动LED发光的电压转换器,通常情况下:LED驱动电源的输入包括高压工频交流(即市电)、低压直流、高压直流、低压高频交流(如电子变压器的输出)等。

关键字: LED 隧道灯 驱动电源
关闭