您好,欢迎访问一九零五行业门户网

yii2源码学习笔记(九),yii2源码学习笔记_PHP教程

yii2源码学习笔记(九),yii2源码学习笔记application是所有应用程序类的基类,接下来了解一下它的源码。yii2\base\application.php。
1 'extension name',168 * 'version' => 'version number',169 * 'bootstrap' => 'bootstrapclassname', // optional, may also be a configuration array170 * 'alias' => [171 * '@alias1' => 'to/path1',172 * '@alias2' => 'to/path2',173 * ],174 * ]175 * ~~~176 *177 * the bootstrap class listed above will be instantiated during the application178 * [[bootstrap()|bootstrapping process]]. if the class implements [[bootstrapinterface]],179 * its [[bootstrapinterface::bootstrap()|bootstrap()]] method will be also be called.180 *181 * if not set explicitly in the application config, this property will be populated with the contents of182 * 如果在应用程序配置中没有设置,该属性将填充到内容183 * @vendor/yiisoft/extensions.php`.184 */185 public $extensions;186 /**187 * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].188 * 组件的列表,运行在 [[bootstrap()|bootstrapping process]]中的应用189 * each component may be specified in one of the following formats:190 *191 * - an application component id as specified via [[components]].192 * - a module id as specified via [[modules]].193 * - a class name.194 * - a configuration array.195 *196 * during the bootstrapping process, each component will be instantiated. if the component class197 * implements [[bootstrapinterface]], its [[bootstrapinterface::bootstrap()|bootstrap()]] method198 * will be also be called.199 * 在整个启动过程中,每个组件被实例化。如果组件类提到 [[bootstrapinterface]], 200 * [[bootstrapinterface::bootstrap()|bootstrap()]]方法也会调用201 */202 public $bootstrap = [];203 /**204 * @var integer the current application state during a request handling life cycle.205 * this property is managed by the application. do not modify this property. 206 * 在请求处理生命周期中的当前应用程序状态。属性由应用程序管理。不要修改此属性。207 */208 public $state;209 /**210 * @var array list of loaded modules indexed by their class names.211 * 加载模块列表由它们的类名称索引组成。212 */213 public $loadedmodules = [];214 215 216 /**217 * constructor.构造函数218 * @param array $config name-value pairs that will be used to initialize the object properties.219 * note that the configuration must contain both [[id]] and [[basepath]].220 * 用来初始化对象属性的 name-value 注意配置必须包含[[id]] 和[[basepath]].221 * @throws invalidconfigexception if either [[id]] or [[basepath]] configuration is missing.222 * 如果是修改[[id]] 或[[basepath]] 则配置丢失。223 */224 public function __construct($config = [])225 {226 yii::$app = $this;// 将自身的实例绑到yii的$app上227 $this->setinstance($this);// 将自身加入到loadedmodules中228 229 $this->state = self::state_begin;// 设置状态为刚开始230 231 // 做预处理配置232 $this->preinit($config);233 234 $this->registererrorhandler($config);235 236 component::__construct($config);237 }238 239 /**240 * pre-initializes the application. 初始化应用。241 * this method is called at the beginning of the application constructor.242 * it initializes several important application properties.243 * 在构造函数中调用该方法,用于初始化一些重要的属性244 * if you override this method, please make sure you call the parent implementation.245 * @param array $config the application configuration 应用的配置246 * @throws invalidconfigexception if either [[id]] or [[basepath]] configuration is missing.247 */248 public function preinit(&$config)249 {250 // 使用了&符号,表示$config的修改会保留251 if (!isset($config['id'])) {//判断配置中是否有application id ,如果没有,抛出异常252 throw new invalidconfigexception('the id configuration for the application is required.');253 }254 if (isset($config['basepath'])) {255 // 是否配置项目的root路径256 $this->setbasepath($config['basepath']);257 //赋值给模块的_basepath属性,并在设置后删除258 unset($config['basepath']);259 } else {//否则抛出异常260 throw new invalidconfigexception('the basepath configuration for the application is required.');261 }262 //如果配置文件中设置了 vendorpath 使用配置的值,并在设置后删除,否则使用默认的263 if (isset($config['vendorpath'])) {264 $this->setvendorpath($config['vendorpath']);265 unset($config['vendorpath']);266 } else {267 // set @vendor268 $this->getvendorpath();269 }270 //如果配置文件中设置了 runtimepath 使用配置的值,并在设置后删除,否则使用默认的271 if (isset($config['runtimepath'])) {272 $this->setruntimepath($config['runtimepath']);273 unset($config['runtimepath']);274 } else {275 // set @runtime276 $this->getruntimepath();277 }278 //如果配置文件中设置了 timezone 使用配置的值,并在设置后删除,否则使用默认的时区279 if (isset($config['timezone'])) {280 $this->settimezone($config['timezone']);281 unset($config['timezone']);282 } elseif (!ini_get('date.timezone')) {283 $this->settimezone('utc');284 }285 286 // merge core components with custom components287 foreach ($this->corecomponents() as $id => $component) {288 if (!isset($config['components'][$id])) {289 // 如果配置中没有配置相应的核心component,就赋给它290 $config['components'][$id] = $component;291 } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {292 // 如果存在相应的核心component,但没有定义它的class,就直接赋到class的key上293 $config['components'][$id]['class'] = $component['class'];294 }295 }296 }297 298 /**299 * @inheritdoc300 */301 public function init()302 {303 $this->state = self::state_init;304 $this->bootstrap();305 }306 307 /**308 * initializes extensions and executes bootstrap components.初始化扩展并执行初始化程序组件309 * this method is called by [[init()]] after the application has been fully configured.310 * 该方法在应用完全配置后被[[init()]]调用311 * if you override this method, make sure you also call the parent implementation.312 */313 protected function bootstrap()314 {315 if ($this->extensions === null) {//如果没有配置,则调用yii的默认扩展组件316 $file = yii::getalias('@vendor/yiisoft/extensions.php');317 $this->extensions = is_file($file) ? include($file) : [];318 }319 foreach ($this->extensions as $extension) {320 if (!empty($extension['alias'])) {//如果扩展组件有设置别名321 foreach ($extension['alias'] as $name => $path) {322 yii::setalias($name, $path);//将给扩展的别名注册到别名数组中323 }324 }325 if (isset($extension['bootstrap'])) {//如果扩展组件有[[bootstrap]]配置 则初始化给扩展组件326 $component = yii::createobject($extension['bootstrap']);327 if ($component instanceof bootstrapinterface) {328 yii::trace(bootstrap with . get_class($component) . '::bootstrap()', __method__);329 $component->bootstrap($this);330 } else {331 yii::trace(bootstrap with . get_class($component), __method__);332 }333 }334 }335 336 foreach ($this->bootstrap as $class) {337 $component = null;338 if (is_string($class)) {339 if ($this->has($class)) {340 $component = $this->get($class);341 } elseif ($this->hasmodule($class)) {342 $component = $this->getmodule($class);343 } elseif (strpos($class, '\\') === false) {344 throw new invalidconfigexception(unknown bootstrapping component id: $class);345 }346 }347 if (!isset($component)) {//如果不存在,则调用yii创建对象348 $component = yii::createobject($class);349 }350 351 if ($component instanceof bootstrapinterface) {352 yii::trace(bootstrap with . get_class($component) . '::bootstrap()', __method__);353 $component->bootstrap($this);354 } else {355 yii::trace(bootstrap with . get_class($component), __method__);356 }357 }358 }
未完待续。
http://www.bkjia.com/phpjc/1130670.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1130670.htmltecharticleyii2源码学习笔记(九),yii2源码学习笔记 application是所有应用程序类的基类,接下来了解一下它的源码。yii2\base\application.php。 1 ? php 2 /*...
其它类似信息

推荐信息