Maison > développement back-end > tutoriel php > yii2源码学习笔记(九),yii2源码学习笔记_PHP教程

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

WBOY
Libérer: 2016-07-12 08:51:20
original
1104 Les gens l'ont consulté

yii2源码学习笔记(九),yii2源码学习笔记

Application是所有应用程序类的基类,接下来了解一下它的源码。yii2\base\Application.php。

<span>  1</span> <?<span>php
</span><span>  2</span> <span>/*</span><span>*
</span><span>  3</span> <span> * @link </span><span>http://www.yiiframework.com/</span>
<span>  4</span> <span> * @copyright Copyright (c) 2008 Yii Software LLC
</span><span>  5</span> <span> * @license </span><span>http://www.yiiframework.com/license/</span>
<span>  6</span>  <span>*/</span>
<span>  7</span> 
<span>  8</span> <span>namespace</span> yii\<span>base</span><span>;
</span><span>  9</span> 
<span> 10</span> <span>use Yii;
</span><span> 11</span> 
<span> 12</span> <span>/*</span><span>*
</span><span> 13</span> <span> * Application is the base class for all application classes.
</span><span> 14</span> <span> * 是所有应用程序类的基类
</span><span> 15</span> <span> * @property \yii\web\AssetManager $assetManager The asset manager application component. This property is
</span><span> 16</span> <span> * read-only.资产管理器应用组件,只读
</span><span> 17</span> <span> * @property \yii\rbac\ManagerInterface $authManager The auth manager application component. Null is returned
</span><span> 18</span> <span> * if auth manager is not configured. This property is read-only.认证管理器应用程序组件。未配置返回null,只读
</span><span> 19</span> <span> * @property string $basePath The root directory of the application. 应用程序的根目录。
</span><span> 20</span> <span> * @property \yii\caching\Cache $cache The cache application component. Null if the component is not enabled.
</span><span> 21</span> <span> * This property is read-only.缓存应用程序组件。
</span><span> 22</span> <span> * @property \yii\db\Connection $db The database connection. This property is read-only.数据库连接。
</span><span> 23</span> <span> * @property \yii\web\ErrorHandler|\yii\console\ErrorHandler $errorHandler The error handler application
</span><span> 24</span> <span> * component. This property is read-only.错误处理程序应用程序组件
</span><span> 25</span> <span> * @property \yii\i18n\Formatter $formatter The formatter application component. This property is read-only.
</span><span> 26</span> <span> * 格式化程序的应用程序组件。
</span><span> 27</span> <span> * @property \yii\i18n\I18N $i18n The internationalization application component. This property is read-only.
</span><span> 28</span> <span> * 国际化应用组件。
</span><span> 29</span> <span> * @property \yii\log\Dispatcher $log The log dispatcher application component. This property is read-only.
</span><span> 30</span> <span> * 日志调度程序组件。
</span><span> 31</span> <span> * @property \yii\mail\MailerInterface $mailer The mailer application component. This property is read-only.
</span><span> 32</span> <span> * 邮件应用程序组件。
</span><span> 33</span> <span> * @property \yii\web\Request|\yii\console\Request $request The request component. This property is read-only.
</span><span> 34</span> <span> * 请求组件。
</span><span> 35</span> <span> * @property \yii\web\Response|\yii\console\Response $response The response component. This property is
</span><span> 36</span> <span> * read-only.反应元件。
</span><span> 37</span> <span> * @property string $runtimePath The directory that stores runtime files. Defaults to the "runtime"
</span><span> 38</span> <span> * subdirectory under [[basePath]].存储运行时文件的目录。
</span><span> 39</span> <span> * @property \yii\base\Security $security The security application component. This property is read-only.
</span><span> 40</span> <span> * 安全应用组件。
</span><span> 41</span> <span> * @property string $timeZone The time zone used by this application.该应用程序使用的时区。
</span><span> 42</span> <span> * @property string $uniqueId The unique ID of the module. This property is read-only.模块的唯一标识。
</span><span> 43</span> <span> * @property \yii\web\UrlManager $urlManager The URL manager for this application. This property is read-only.
</span><span> 44</span> <span> * 此应用程序的网址管理器。
</span><span> 45</span> <span> * @property string $vendorPath The directory that stores vendor files. Defaults to "vendor" directory under
</span><span> 46</span> <span> * [[basePath]].存储供应商文件的目录。
</span><span> 47</span> <span> * @property View|\yii\web\View $view The view application component that is used to render various view
</span><span> 48</span> <span> * files. This property is read-only.用于呈现各种视图文件的视图应用程序组件
</span><span> 49</span> <span> *
</span><span> 50</span> <span> * @author Qiang Xue <qiang.xue@gmail.com>
</span><span> 51</span> <span> * @since 2.0
</span><span> 52</span>  <span>*/</span>
<span> 53</span> <span>abstract</span> <span>class</span><span> Application extends Module
</span><span> 54</span> <span>{
</span><span> 55</span>     <span>/*</span><span>*
</span><span> 56</span> <span>     * @event Event an event raised before the application starts to handle a request.
</span><span> 57</span> <span>     * 在应用程序开始处理请求之前提出的事件。
</span><span> 58</span>      <span>*/</span>
<span> 59</span>     <span>const</span> EVENT_BEFORE_REQUEST = <span>'</span><span>beforeRequest</span><span>'</span><span>;
</span><span> 60</span>     <span>/*</span><span>*
</span><span> 61</span> <span>     * @event Event an event raised after the application successfully handles a request (before the response is sent out).
</span><span> 62</span> <span>     * 该应用程序成功处理请求后提出的事件
</span><span> 63</span>      <span>*/</span>
<span> 64</span>     <span>const</span> EVENT_AFTER_REQUEST = <span>'</span><span>afterRequest</span><span>'</span><span>;
</span><span> 65</span>     <span>/*</span><span>*
</span><span> 66</span> <span>     * Application state used by [[state]]: application just started.
</span><span> 67</span> <span>     * [[state]]适用状态:刚开始应用
</span><span> 68</span>      <span>*/</span>
<span> 69</span>     <span>const</span> STATE_BEGIN = <span>0</span><span>;
</span><span> 70</span>     <span>/*</span><span>*
</span><span> 71</span> <span>     * Application state used by [[state]]: application is initializing.
</span><span> 72</span> <span>     *  [[state]]应用程序状态:应用程序初始化。
</span><span> 73</span>      <span>*/</span>
<span> 74</span>     <span>const</span> STATE_INIT = <span>1</span><span>;
</span><span> 75</span>     <span>/*</span><span>*
</span><span> 76</span> <span>     * Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]].
</span><span> 77</span> <span>     *  [[state]]应用程序状态:应用触发[[EVENT_BEFORE_REQUEST]]
</span><span> 78</span>      <span>*/</span>
<span> 79</span>     <span>const</span> STATE_BEFORE_REQUEST = <span>2</span><span>;
</span><span> 80</span>     <span>/*</span><span>*
</span><span> 81</span> <span>     * Application state used by [[state]]: application is handling the request.
</span><span> 82</span> <span>     *  [[state]]应用程序状态:应用程序处理请求。
</span><span> 83</span>      <span>*/</span>
<span> 84</span>     <span>const</span> STATE_HANDLING_REQUEST = <span>3</span><span>;
</span><span> 85</span>     <span>/*</span><span>*
</span><span> 86</span> <span>     * Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]]..
</span><span> 87</span> <span>     *  [[state]]应用程序状态:应用触发[[EVENT_AFTER_REQUEST]]
</span><span> 88</span>      <span>*/</span>
<span> 89</span>     <span>const</span> STATE_AFTER_REQUEST = <span>4</span><span>;
</span><span> 90</span>     <span>/*</span><span>*
</span><span> 91</span> <span>     * Application state used by [[state]]: application is about to send response.
</span><span> 92</span> <span>     *  [[state]]应用程序状态:应用程序即将发送响应。
</span><span> 93</span>      <span>*/</span>
<span> 94</span>     <span>const</span> STATE_SENDING_RESPONSE = <span>5</span><span>;
</span><span> 95</span>     <span>/*</span><span>*
</span><span> 96</span> <span>     * Application state used by [[state]]: application has ended.
</span><span> 97</span> <span>     *  [[state]]应用程序状态:应用程序结束。
</span><span> 98</span>      <span>*/</span>
<span> 99</span>     <span>const</span> STATE_END = <span>6</span><span>;
</span><span>100</span> 
<span>101</span>     <span>/*</span><span>*
</span><span>102</span> <span>     * @var string the namespace that controller classes are located in.控制器类的命名空间位置。
</span><span>103</span> <span>     * This namespace will be used to load controller classes by prepending it to the controller class name.
</span><span>104</span> <span>     * The default namespace is `app\controllers`.
</span><span>105</span> <span>     * 此命名空间将用于负载控制器类重写它的控制器类的名字。 默认命名空间是`app\controllers`。
</span><span>106</span> <span>     * Please refer to the [guide about class autoloading](guide:concept-autoloading.md) for more details.
</span><span>107</span>      <span>*/</span>
<span>108</span>     <span>public</span> $controllerNamespace = <span>'</span><span>app\\controllers</span><span>'</span><span>;
</span><span>109</span>     <span>/*</span><span>*
</span><span>110</span> <span>     * @var string the application name.应用程序名称。
</span><span>111</span>      <span>*/</span>
<span>112</span>     <span>public</span> $name = <span>'</span><span>My Application</span><span>'</span><span>;
</span><span>113</span>     <span>/*</span><span>*
</span><span>114</span> <span>     * @var string the version of this application.此应用程序的版本。
</span><span>115</span>      <span>*/</span>
<span>116</span>     <span>public</span> $version = <span>'</span><span>1.0</span><span>'</span><span>;
</span><span>117</span>     <span>/*</span><span>*
</span><span>118</span> <span>     * @var string the charset currently used for the application.目前使用的字符集。
</span><span>119</span>      <span>*/</span>
<span>120</span>     <span>public</span> $charset = <span>'</span><span>UTF-8</span><span>'</span><span>;
</span><span>121</span>     <span>/*</span><span>*
</span><span>122</span> <span>     * @var string the language that is meant to be used for end users. It is recommended that you
</span><span>123</span> <span>     * use [IETF language tags](</span><span>http://en.wikipedia.org/wiki/IETF_language_tag</span><span>). For example, `en` stands
</span><span>124</span> <span>     * for English, while `en-US` stands for English (United States).
</span><span>125</span> <span>     * 用来作为终端用户使用的语言
</span><span>126</span> <span>     * @see sourceLanguage
</span><span>127</span>      <span>*/</span>
<span>128</span>     <span>public</span> $language = <span>'</span><span>en-US</span><span>'</span><span>;
</span><span>129</span>     <span>/*</span><span>*
</span><span>130</span> <span>     * @var string the language that the application is written in. This mainly refers to
</span><span>131</span> <span>     * the language that the messages and view files are written in.
</span><span>132</span> <span>     * 应用程序编写的语言。
</span><span>133</span> <span>     * @see language
</span><span>134</span>      <span>*/</span>
<span>135</span>     <span>public</span> $sourceLanguage = <span>'</span><span>en-US</span><span>'</span><span>;
</span><span>136</span>     <span>/*</span><span>*
</span><span>137</span> <span>     * @var Controller the currently active controller instance当前活动控制器实例
</span><span>138</span>      <span>*/</span>
<span>139</span>     <span>public</span><span> $controller;
</span><span>140</span>     <span>/*</span><span>*
</span><span>141</span> <span>     * @var string|boolean the layout that should be applied for views in this application. Defaults to 'main'.
</span><span>142</span> <span>     * If this is false, layout will be disabled.
</span><span>143</span> <span>     * 该应用程序中应用的布局。
</span><span>144</span>      <span>*/</span>
<span>145</span>     <span>public</span> $layout = <span>'</span><span>main</span><span>'</span><span>;
</span><span>146</span>     <span>/*</span><span>*
</span><span>147</span> <span>     * @var string the requested route请求的路径    请求的路径
</span><span>148</span>      <span>*/</span>
<span>149</span>     <span>public</span><span> $requestedRoute;
</span><span>150</span>     <span>/*</span><span>*
</span><span>151</span> <span>     * @var Action the requested Action. If null, it means the request cannot be resolved into an action.
</span><span>152</span> <span>     * 操作所要求的行动
</span><span>153</span>      <span>*/</span>
<span>154</span>     <span>public</span><span> $requestedAction;
</span><span>155</span>     <span>/*</span><span>*
</span><span>156</span> <span>     * @var array the parameters supplied to the requested action.
</span><span>157</span> <span>     * 所请求的动作提供的参数。
</span><span>158</span>      <span>*/</span>
<span>159</span>     <span>public</span><span> $requestedParams;
</span><span>160</span>     <span>/*</span><span>*
</span><span>161</span> <span>     * @var array list of installed Yii extensions. Each array element represents a single extension
</span><span>162</span> <span>     * with the following structure:
</span><span>163</span> <span>     * 安装Yii扩展名列表。每个数组元素代表一个扩展
</span><span>164</span> <span>     *
</span><span>165</span> <span>     * ~~~
</span><span>166</span> <span>     * [
</span><span>167</span> <span>     *     'name' => 'extension name',
</span><span>168</span> <span>     *     'version' => 'version number',
</span><span>169</span> <span>     *     'bootstrap' => 'BootstrapClassName',  // optional, may also be a configuration array
</span><span>170</span> <span>     *     'alias' => [
</span><span>171</span> <span>     *         '@alias1' => 'to/path1',
</span><span>172</span> <span>     *         '@alias2' => 'to/path2',
</span><span>173</span> <span>     *     ],
</span><span>174</span> <span>     * ]
</span><span>175</span> <span>     * ~~~
</span><span>176</span> <span>     *
</span><span>177</span> <span>     * The "bootstrap" class listed above will be instantiated during the application
</span><span>178</span> <span>     * [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]],
</span><span>179</span> <span>     * its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called.
</span><span>180</span> <span>     *
</span><span>181</span> <span>     * If not set explicitly in the application config, this property will be populated with the contents of
</span><span>182</span> <span>     * 如果在应用程序配置中没有设置,该属性将填充到内容
</span><span>183</span> <span>     * @vendor/yiisoft/extensions.php`.
</span><span>184</span>      <span>*/</span>
<span>185</span>     <span>public</span><span> $extensions;
</span><span>186</span>     <span>/*</span><span>*
</span><span>187</span> <span>     * @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].
</span><span>188</span> <span>     * 组件的列表,运行在 [[bootstrap()|bootstrapping process]]中的应用
</span><span>189</span> <span>     * Each component may be specified in one of the following formats:
</span><span>190</span> <span>     *
</span><span>191</span> <span>     * - an application component ID as specified via [[components]].
</span><span>192</span> <span>     * - a module ID as specified via [[modules]].
</span><span>193</span> <span>     * - a class name.
</span><span>194</span> <span>     * - a configuration array.
</span><span>195</span> <span>     *
</span><span>196</span> <span>     * During the bootstrapping process, each component will be instantiated. If the component class
</span><span>197</span> <span>     * implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method
</span><span>198</span> <span>     * will be also be called.
</span><span>199</span> <span>     * 在整个启动过程中,每个组件被实例化。如果组件类提到 [[BootstrapInterface]], 
</span><span>200</span> <span>     * [[BootstrapInterface::bootstrap()|bootstrap()]]方法也会调用
</span><span>201</span>      <span>*/</span>
<span>202</span>     <span>public</span> $bootstrap =<span> [];
</span><span>203</span>     <span>/*</span><span>*
</span><span>204</span> <span>     * @var integer the current application state during a request handling life cycle.
</span><span>205</span> <span>     * This property is managed by the application. Do not modify this property.    
</span><span>206</span> <span>     * 在请求处理生命周期中的当前应用程序状态。属性由应用程序管理。不要修改此属性。
</span><span>207</span>      <span>*/</span>
<span>208</span>     <span>public</span><span> $state;
</span><span>209</span>     <span>/*</span><span>*
</span><span>210</span> <span>     * @var array list of loaded modules indexed by their class names.
</span><span>211</span> <span>     * 加载模块列表由它们的类名称索引组成。
</span><span>212</span>      <span>*/</span>
<span>213</span>     <span>public</span> $loadedModules =<span> [];
</span><span>214</span> 
<span>215</span> 
<span>216</span>     <span>/*</span><span>*
</span><span>217</span> <span>     * Constructor.构造函数
</span><span>218</span> <span>     * @param array $config name-value pairs that will be used to initialize the object properties.
</span><span>219</span> <span>     * Note that the configuration must contain both [[id]] and [[basePath]].
</span><span>220</span> <span>     * 用来初始化对象属性的 name-value 注意配置必须包含[[id]] 和[[basePath]].
</span><span>221</span> <span>     * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
</span><span>222</span> <span>     * 如果是修改[[id]] 或[[basePath]] 则配置丢失。
</span><span>223</span>      <span>*/</span>
<span>224</span>     <span>public</span> function __construct($config =<span> [])
</span><span>225</span> <span>    {
</span><span>226</span>         Yii::$app = $<span>this</span>;<span>//</span><span> 将自身的实例绑到Yii的$app上</span>
<span>227</span>         $<span>this</span>->setInstance($<span>this</span>);<span>//</span><span> 将自身加入到loadedModules中</span>
<span>228</span> 
<span>229</span>         $<span>this</span>->state = self::STATE_BEGIN;<span>//</span><span> 设置状态为刚开始
</span><span>230</span> 
<span>231</span>         <span>//</span><span> 做预处理配置</span>
<span>232</span>         $<span>this</span>-><span>preInit($config);
</span><span>233</span> 
<span>234</span>         $<span>this</span>-><span>registerErrorHandler($config);
</span><span>235</span> 
<span>236</span> <span>        Component::__construct($config);
</span><span>237</span> <span>    }
</span><span>238</span> 
<span>239</span>     <span>/*</span><span>*
</span><span>240</span> <span>     * Pre-initializes the application. 初始化应用。
</span><span>241</span> <span>     * This method is called at the beginning of the application constructor.
</span><span>242</span> <span>     * It initializes several important application properties.
</span><span>243</span> <span>     * 在构造函数中调用该方法,用于初始化一些重要的属性
</span><span>244</span> <span>     * If you override this method, please make sure you call the parent implementation.
</span><span>245</span> <span>     * @param array $config the application configuration   应用的配置
</span><span>246</span> <span>     * @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
</span><span>247</span>      <span>*/</span>
<span>248</span>     <span>public</span> function preInit(&<span>$config)
</span><span>249</span> <span>    {
</span><span>250</span>          <span>//</span><span> 使用了&符号,表示$config的修改会保留</span>
<span>251</span>         <span>if</span> (!isset($config[<span>'</span><span>id</span><span>'</span>])) {<span>//</span><span>判断配置中是否有application ID ,如果没有,抛出异常</span>
<span>252</span>             <span>throw</span> <span>new</span> InvalidConfigException(<span>'</span><span>The "id" configuration for the Application is required.</span><span>'</span><span>);
</span><span>253</span> <span>        }
</span><span>254</span>         <span>if</span> (isset($config[<span>'</span><span>basePath</span><span>'</span><span>])) {
</span><span>255</span>             <span>//</span><span> 是否配置项目的root路径</span>
<span>256</span>             $<span>this</span>->setBasePath($config[<span>'</span><span>basePath</span><span>'</span><span>]);
</span><span>257</span>              <span>//</span><span>赋值给模块的_basepath属性,并在设置后删除</span>
<span>258</span>             unset($config[<span>'</span><span>basePath</span><span>'</span><span>]);
</span><span>259</span>         } <span>else</span> {<span>//</span><span>否则抛出异常</span>
<span>260</span>             <span>throw</span> <span>new</span> InvalidConfigException(<span>'</span><span>The "basePath" configuration for the Application is required.</span><span>'</span><span>);
</span><span>261</span> <span>        }
</span><span>262</span>         <span>//</span><span>如果配置文件中设置了 vendorPath 使用配置的值,并在设置后删除,否则使用默认的</span>
<span>263</span>         <span>if</span> (isset($config[<span>'</span><span>vendorPath</span><span>'</span><span>])) {
</span><span>264</span>             $<span>this</span>->setVendorPath($config[<span>'</span><span>vendorPath</span><span>'</span><span>]);
</span><span>265</span>             unset($config[<span>'</span><span>vendorPath</span><span>'</span><span>]);
</span><span>266</span>         } <span>else</span><span> {
</span><span>267</span>             <span>//</span><span> set "@vendor"</span>
<span>268</span>             $<span>this</span>-><span>getVendorPath();
</span><span>269</span> <span>        }
</span><span>270</span>         <span>//</span><span>如果配置文件中设置了 runtimePath 使用配置的值,并在设置后删除,否则使用默认的</span>
<span>271</span>         <span>if</span> (isset($config[<span>'</span><span>runtimePath</span><span>'</span><span>])) {
</span><span>272</span>             $<span>this</span>->setRuntimePath($config[<span>'</span><span>runtimePath</span><span>'</span><span>]);
</span><span>273</span>             unset($config[<span>'</span><span>runtimePath</span><span>'</span><span>]);
</span><span>274</span>         } <span>else</span><span> {
</span><span>275</span>             <span>//</span><span> set "@runtime"</span>
<span>276</span>             $<span>this</span>-><span>getRuntimePath();
</span><span>277</span> <span>        }
</span><span>278</span>          <span>//</span><span>如果配置文件中设置了 timeZone 使用配置的值,并在设置后删除,否则使用默认的时区</span>
<span>279</span>         <span>if</span> (isset($config[<span>'</span><span>timeZone</span><span>'</span><span>])) {
</span><span>280</span>             $<span>this</span>->setTimeZone($config[<span>'</span><span>timeZone</span><span>'</span><span>]);
</span><span>281</span>             unset($config[<span>'</span><span>timeZone</span><span>'</span><span>]);
</span><span>282</span>         } elseif (!ini_get(<span>'</span><span>date.timezone</span><span>'</span><span>)) {
</span><span>283</span>             $<span>this</span>->setTimeZone(<span>'</span><span>UTC</span><span>'</span><span>);
</span><span>284</span> <span>        }
</span><span>285</span> 
<span>286</span>         <span>//</span><span> merge core components with custom components</span>
<span>287</span>         <span>foreach</span> ($<span>this</span>->coreComponents() <span>as</span> $id =><span> $component) {
</span><span>288</span>             <span>if</span> (!isset($config[<span>'</span><span>components</span><span>'</span><span>][$id])) {
</span><span>289</span>                 <span>//</span><span> 如果配置中没有配置相应的核心component,就赋给它</span>
<span>290</span>                 $config[<span>'</span><span>components</span><span>'</span>][$id] =<span> $component;
</span><span>291</span>             } elseif (is_array($config[<span>'</span><span>components</span><span>'</span>][$id]) && !isset($config[<span>'</span><span>components</span><span>'</span>][$id][<span>'</span><span>class</span><span>'</span><span>])) {
</span><span>292</span>                  <span>//</span><span> 如果存在相应的核心component,但没有定义它的class,就直接赋到class的key上</span>
<span>293</span>                 $config[<span>'</span><span>components</span><span>'</span>][$id][<span>'</span><span>class</span><span>'</span>] = $component[<span>'</span><span>class</span><span>'</span><span>];
</span><span>294</span> <span>            }
</span><span>295</span> <span>        }
</span><span>296</span> <span>    }
</span><span>297</span> 
<span>298</span>     <span>/*</span><span>*
</span><span>299</span> <span>     * @inheritdoc
</span><span>300</span>      <span>*/</span>
<span>301</span>     <span>public</span><span> function init()
</span><span>302</span> <span>    {
</span><span>303</span>         $<span>this</span>->state =<span> self::STATE_INIT;
</span><span>304</span>         $<span>this</span>-><span>bootstrap();
</span><span>305</span> <span>    }
</span><span>306</span> 
<span>307</span>     <span>/*</span><span>*
</span><span>308</span> <span>     * Initializes extensions and executes bootstrap components.初始化扩展并执行初始化程序组件
</span><span>309</span> <span>     * This method is called by [[init()]] after the application has been fully configured.
</span><span>310</span> <span>     * 该方法在应用完全配置后被[[init()]]调用
</span><span>311</span> <span>     * If you override this method, make sure you also call the parent implementation.
</span><span>312</span>      <span>*/</span>
<span>313</span>     <span>protected</span><span> function bootstrap()
</span><span>314</span> <span>    {
</span><span>315</span>         <span>if</span> ($<span>this</span>->extensions === <span>null</span>) {<span>//</span><span>如果没有配置,则调用Yii的默认扩展组件</span>
<span>316</span>             $file = Yii::getAlias(<span>'</span><span>@vendor/yiisoft/extensions.php</span><span>'</span><span>);
</span><span>317</span>             $<span>this</span>->extensions = is_file($file) ?<span> include($file) : [];
</span><span>318</span> <span>        }
</span><span>319</span>         <span>foreach</span> ($<span>this</span>->extensions <span>as</span><span> $extension) {
</span><span>320</span>             <span>if</span> (!empty($extension[<span>'</span><span>alias</span><span>'</span>])) {<span>//</span><span>如果扩展组件有设置别名</span>
<span>321</span>                 <span>foreach</span> ($extension[<span>'</span><span>alias</span><span>'</span>] <span>as</span> $name =><span> $path) {
</span><span>322</span>                     Yii::setAlias($name, $path);<span>//</span><span>将给扩展的别名注册到别名数组中</span>
<span>323</span> <span>                }
</span><span>324</span> <span>            }
</span><span>325</span>             <span>if</span> (isset($extension[<span>'</span><span>bootstrap</span><span>'</span>])) {<span>//</span><span>如果扩展组件有[[bootstrap]]配置 则初始化给扩展组件</span>
<span>326</span>                 $component = Yii::createObject($extension[<span>'</span><span>bootstrap</span><span>'</span><span>]);
</span><span>327</span>                 <span>if</span><span> ($component instanceof BootstrapInterface) {
</span><span>328</span>                     Yii::trace(<span>"</span><span>Bootstrap with </span><span>"</span> . get_class($component) . <span>'</span><span>::bootstrap()</span><span>'</span><span>, __METHOD__);
</span><span>329</span>                     $component->bootstrap($<span>this</span><span>);
</span><span>330</span>                 } <span>else</span><span> {
</span><span>331</span>                     Yii::trace(<span>"</span><span>Bootstrap with </span><span>"</span><span> . get_class($component), __METHOD__);
</span><span>332</span> <span>                }
</span><span>333</span> <span>            }
</span><span>334</span> <span>        }
</span><span>335</span> 
<span>336</span>         <span>foreach</span> ($<span>this</span>->bootstrap <span>as</span> $<span>class</span><span>) {
</span><span>337</span>             $component = <span>null</span><span>;
</span><span>338</span>             <span>if</span> (is_string($<span>class</span><span>)) {
</span><span>339</span>                 <span>if</span> ($<span>this</span>->has($<span>class</span><span>)) {
</span><span>340</span>                     $component = $<span>this</span>-><span>get</span>($<span>class</span><span>);
</span><span>341</span>                 } elseif ($<span>this</span>->hasModule($<span>class</span><span>)) {
</span><span>342</span>                     $component = $<span>this</span>->getModule($<span>class</span><span>);
</span><span>343</span>                 } elseif (strpos($<span>class</span>, <span>'</span><span>\\</span><span>'</span>) === <span>false</span><span>) {
</span><span>344</span>                     <span>throw</span> <span>new</span> InvalidConfigException(<span>"</span><span>Unknown bootstrapping component ID: $class</span><span>"</span><span>);
</span><span>345</span> <span>                }
</span><span>346</span> <span>            }
</span><span>347</span>             <span>if</span> (!isset($component)) {<span>//</span><span>如果不存在,则调用Yii创建对象</span>
<span>348</span>                 $component = Yii::createObject($<span>class</span><span>);
</span><span>349</span> <span>            }
</span><span>350</span> 
<span>351</span>             <span>if</span><span> ($component instanceof BootstrapInterface) {
</span><span>352</span>                 Yii::trace(<span>"</span><span>Bootstrap with </span><span>"</span> . get_class($component) . <span>'</span><span>::bootstrap()</span><span>'</span><span>, __METHOD__);
</span><span>353</span>                 $component->bootstrap($<span>this</span><span>);
</span><span>354</span>             } <span>else</span><span> {
</span><span>355</span>                 Yii::trace(<span>"</span><span>Bootstrap with </span><span>"</span><span> . get_class($component), __METHOD__);
</span><span>356</span> <span>            }
</span><span>357</span> <span>        }
</span><span>358</span>     }
Copier après la connexion

未完待续。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1130670.htmlTechArticleyii2源码学习笔记(九),yii2源码学习笔记 Application是所有应用程序类的基类,接下来了解一下它的源码。yii2\base\Application.php。 1 ? php 2 /*...
Étiquettes associées:
yii
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal