Application的run方法分析

来源:互联网 发布:mysql 缓存命中率 编辑:程序博客网 时间:2024/05/17 12:51

(new yii\web\Application($config))->run();

$config 是配置文件获取的参数

__construct方法调用的是Application的父类的__construct

public function __construct($config = []){   /**设置成单例**/    Yii::$app = $this;    static::setInstance($this);     /**APP应用状态 说明是刚开始 STATE_BEGIN = 0**/    $this->state = self::STATE_BEGIN;    /**应用的预先初始化**/    $this->preInit($config);    /**注册错误处理**/    $this->registerErrorHandler($config);    /**组件初始化**/    Component::__construct($config);}

run方法也是 调用的是Application的父类的__run

public function run(){    try {        //请求前过滤器        $this->state = self::STATE_BEFORE_REQUEST;        $this->trigger(self::EVENT_BEFORE_REQUEST);        //处理请求        $this->state = self::STATE_HANDLING_REQUEST;        $response = $this->handleRequest($this->getRequest());        //请求后过滤器        $this->state = self::STATE_AFTER_REQUEST;        $this->trigger(self::EVENT_AFTER_REQUEST);        //发送请求到浏览器        $this->state = self::STATE_SENDING_RESPONSE;        $response->send();        $this->state = self::STATE_END;        return $response->exitStatus;    } catch (ExitException $e) {        $this->end($e->statusCode, isset($response) ? $response : null);        return $e->statusCode;    }}
  1. 首先分析下 __construct 用到的函数preInit __construct 的config就是用到这里

    public function preInit(&$config){    /**应用ID 必须得有 不能为空**/    if (!isset($config['id'])) {        throw new InvalidConfigException('The "id" configuration for the Application is required.');    }    /**APP路径必须得设置好**/    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();    }    /**检查runtimePath debug和log文件位置**/    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');    }    /**检查setContainer**/    if (isset($config['container'])) {        /**设置容器**/        $this->setContainer($config['container']);        unset($config['container']);    }    // merge core components with custom components    /**coreComponents 是获取设置应用的核心组件**/    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'];        }    }}
  2. Component::__construct 调用的是父类Object的 __construct

    public function __construct($config = []){    if (!empty($config)) {        Yii::configure($this, $config);    }    $this->init();}/**这里调用的是yii\BaseYii里的configure,注册组件名和组件到APP**/public static function configure($object, $properties){    foreach ($properties as $name => $value) {        $object->$name = $value;    }    return $object;}
  3. run方法里的trigger方法是调用的其父类 Module-》ServiceLocator-》Component的方法

    public function trigger($name, Event $event = null){    $this->ensureBehaviors();    if (!empty($this->_events[$name])) {        if ($event === null) {            $event = new Event;        }        if ($event->sender === null) {            $event->sender = $this;        }        $event->handled = false;        $event->name = $name;        foreach ($this->_events[$name] as $handler) {            $event->data = $handler[1];            call_user_func($handler[0], $event);            // stop further handling if the event is handled            if ($event->handled) {                return;            }        }    }    // invoke class-level attached handlers    Event::trigger($this, $name, $event);}