继续了解Application.
<span> 1</span> <span>/*</span><span>* </span><span> 2</span> <span> * Registers the errorHandler component as a PHP error handler. </span><span> 3</span> <span> * 注册errorHandler组件作为PHP错误处理函数 </span><span> 4</span> <span> * @param array $config application config 应用程序配置 </span><span> 5</span> <span>*/</span> <span> 6</span> <span>protected</span> function registerErrorHandler(&<span>$config) </span><span> 7</span> <span> { </span><span> 8</span> <span>if</span> (YII_ENABLE_ERROR_HANDLER) {<span>//</span><span> YII_ENABLE_ERROR_HANDLER在BaseYii中被定义为true</span> <span> 9</span> <span>if</span> (!isset($config[<span>'</span><span>components</span><span>'</span>][<span>'</span><span>errorHandler</span><span>'</span>][<span>'</span><span>class</span><span>'</span><span>])) { </span><span> 10</span> <span>//</span><span>$config['components']['errorHandler']['class']不存在结束运行 </span> <span> 11</span> echo <span>"</span><span>Error: no errorHandler component is configured.\n</span><span>"</span><span>; </span><span> 12</span> exit(<span>1</span><span>); </span><span> 13</span> <span> } </span><span> 14</span> <span>//</span><span>将$config['components']['errorHandler']的内容设置到了$this->_definitions['errorHandler']中</span> <span> 15</span> $<span>this</span>-><span>set</span>(<span>'</span><span>errorHandler</span><span>'</span>, $config[<span>'</span><span>components</span><span>'</span>][<span>'</span><span>errorHandler</span><span>'</span><span>]); </span><span> 16</span> unset($config[<span>'</span><span>components</span><span>'</span>][<span>'</span><span>errorHandler</span><span>'</span>]);<span>//</span><span> 删除掉配置内容</span> <span> 17</span> $<span>this</span>->getErrorHandler()-><span>register(); </span><span> 18</span> <span> } </span><span> 19</span> <span> } </span><span> 20</span> <span> 21</span> <span>/*</span><span>* </span><span> 22</span> <span> * Returns an ID that uniquely identifies this module among all modules within the current application. </span><span> 23</span> <span> * Since this is an application instance, it will always return an empty string. </span><span> 24</span> <span> * 返回在当前应用程序中该模块的唯一标识。这是一个应用实例,它将返回一个空字符串。 </span><span> 25</span> <span> * @return string the unique ID of the module.模块的唯一标识。 </span><span> 26</span> <span>*/</span> <span> 27</span> <span>public</span><span> function getUniqueId() </span><span> 28</span> <span> { </span><span> 29</span> <span>return</span> <span>''</span><span>; </span><span> 30</span> <span> } </span><span> 31</span> <span> 32</span> <span>/*</span><span>* </span><span> 33</span> <span> * Sets the root directory of the application and the @app alias.设置应用程序的根目录 @ 加应用程序别名。 </span><span> 34</span> <span> * This method can only be invoked at the beginning of the constructor.只能在构造函数开始时调用该方法 </span><span> 35</span> <span> * @param string $path the root directory of the application.应用程序的根目录。 </span><span> 36</span> <span> * @property string the root directory of the application. 应用程序的根目录。 </span><span> 37</span> <span> * @throws InvalidParamException if the directory does not exist. 如果目录不存在。抛出异常 </span><span> 38</span> <span>*/</span> <span> 39</span> <span>public</span><span> function setBasePath($path) </span><span> 40</span> <span> { </span><span> 41</span> <span> parent::setBasePath($path); </span><span> 42</span> <span>//</span><span> 使用@app来记录basePath</span> <span> 43</span> Yii::setAlias(<span>'</span><span>@app</span><span>'</span>, $<span>this</span>-><span>getBasePath()); </span><span> 44</span> <span> } </span><span> 45</span> <span> 46</span> <span>/*</span><span>* </span><span> 47</span> <span> * Runs the application. 运行应用程序。 </span><span> 48</span> <span> * This is the main entrance of an application. 应用程序的主要入口。 </span><span> 49</span> <span> * @return integer the exit status (0 means normal, non-zero values mean abnormal) 状态(0正常,非0为不正常) </span><span> 50</span> <span>*/</span> <span> 51</span> <span>public</span><span> function run() </span><span> 52</span> <span> { </span><span> 53</span> <span>try</span><span> { </span><span> 54</span> <span> 55</span> $<span>this</span>->state =<span> self::STATE_BEFORE_REQUEST; </span><span> 56</span> $<span>this</span>->trigger(self::EVENT_BEFORE_REQUEST);<span>//</span><span>加载事件函数beforRequest函数</span> <span> 57</span> <span> 58</span> $<span>this</span>->state =<span> self::STATE_HANDLING_REQUEST; </span><span> 59</span> $response = $<span>this</span>->handleRequest($<span>this</span>->getRequest());<span>//</span><span>加载控制器 获取Request对象</span> <span> 60</span> <span> 61</span> $<span>this</span>->state =<span> self::STATE_AFTER_REQUEST; </span><span> 62</span> $<span>this</span>->trigger(self::EVENT_AFTER_REQUEST);<span>//</span><span>加载afterRequest事件函数</span> <span> 63</span> <span> 64</span> $<span>this</span>->state =<span> self::STATE_SENDING_RESPONSE; </span><span> 65</span> $response->send();<span>//</span><span>将页面内容输入缓冲,然后输出</span> <span> 66</span> <span> 67</span> $<span>this</span>->state =<span> self::STATE_END; </span><span> 68</span> <span> 69</span> <span>return</span> $response-><span>exitStatus; </span><span> 70</span> <span> 71</span> } <span>catch</span><span> (ExitException $e) { </span><span> 72</span> <span> 73</span> $<span>this</span>->end($e->statusCode, isset($response) ? $response : <span>null</span><span>); </span><span> 74</span> <span>return</span> $e-><span>statusCode; </span><span> 75</span> <span> 76</span> <span> } </span><span> 77</span> <span> } </span><span> 78</span> <span> 79</span> <span>/*</span><span>* </span><span> 80</span> <span> * Handles the specified request. </span><span> 81</span> <span> * 处理指定的请求 </span><span> 82</span> <span> * This method should return an instance of [[Response]] or its child class </span><span> 83</span> <span> * which represents the handling result of the request. </span><span> 84</span> <span> * 该方法应该返回一个[[Response]]实例,或者它的子类代表处理请求的结果 </span><span> 85</span> <span> * @param Request $request the request to be handled 被处理的请求 </span><span> 86</span> <span> * @return Response the resulting response 得到的响应 </span><span> 87</span> <span>*/</span> <span> 88</span> <span>abstract</span> <span>public</span><span> function handleRequest($request); </span><span> 89</span> <span> 90</span> <span>private</span><span> $_runtimePath; </span><span> 91</span> <span> 92</span> <span>/*</span><span>* </span><span> 93</span> <span> * Returns the directory that stores runtime files.返回存储运行时文件的路径 </span><span> 94</span> <span> * @return string the directory that stores runtime files.存储运行时文件的目录。 </span><span> 95</span> <span> * Defaults to the "runtime" subdirectory under [[basePath]].默认返回[[basePath]]下的 "runtime"目录 </span><span> 96</span> <span>*/</span> <span> 97</span> <span>public</span><span> function getRuntimePath() </span><span> 98</span> <span> { </span><span> 99</span> <span>if</span> ($<span>this</span>->_runtimePath === <span>null</span>) {<span>//</span><span>设置临时文件存储路径</span> <span>100</span> $<span>this</span>->setRuntimePath($<span>this</span>->getBasePath() . DIRECTORY_SEPARATOR . <span>'</span><span>runtime</span><span>'</span><span>); </span><span>101</span> <span> } </span><span>102</span> <span>103</span> <span>return</span> $<span>this</span>-><span>_runtimePath; </span><span>104</span> <span> } </span><span>105</span> <span>106</span> <span>/*</span><span>* </span><span>107</span> <span> * Sets the directory that stores runtime files.设置存储运行时文件的路径 </span><span>108</span> <span> * @param string $path the directory that stores runtime files.存储运行时文件的目录。 </span><span>109</span> <span>*/</span> <span>110</span> <span>public</span><span> function setRuntimePath($path) </span><span>111</span> <span> { </span><span>112</span> <span>//</span><span> 获取runtimePath的路径,并存到_runtimePath中</span> <span>113</span> $<span>this</span>->_runtimePath =<span> Yii::getAlias($path); </span><span>114</span> <span>//</span><span> 使用@runtime来记录 runtimePath</span> <span>115</span> Yii::setAlias(<span>'</span><span>@runtime</span><span>'</span>, $<span>this</span>-><span>_runtimePath); </span><span>116</span> <span> } </span><span>117</span> <span>118</span> <span>private</span><span> $_vendorPath; </span><span>119</span> <span>120</span> <span>/*</span><span>* </span><span>121</span> <span> * Returns the directory that stores vendor files.返回插件文件的目录。 </span><span>122</span> <span> * @return string the directory that stores vendor files. </span><span>123</span> <span> * Defaults to "vendor" directory under [[basePath]]. </span><span>124</span> <span> * 默认返回[[basePath]]下的 "vendor" 目录 </span><span>125</span> <span>*/</span> <span>126</span> <span>public</span><span> function getVendorPath() </span><span>127</span> <span> { </span><span>128</span> <span>if</span> ($<span>this</span>->_vendorPath === <span>null</span><span>) { </span><span>129</span> <span>//</span><span> 不存在,就将其设置为basePath/vendor</span> <span>130</span> $<span>this</span>->setVendorPath($<span>this</span>->getBasePath() . DIRECTORY_SEPARATOR . <span>'</span><span>vendor</span><span>'</span><span>); </span><span>131</span> <span> } </span><span>132</span> <span>133</span> <span>return</span> $<span>this</span>-><span>_vendorPath; </span><span>134</span> <span> } </span><span>135</span> <span>136</span> <span>/*</span><span>* </span><span>137</span> <span> * Sets the directory that stores vendor files.设置插件目录路径,并设置别名 </span><span>138</span> <span> * @param string $path the directory that stores vendor files. </span><span>139</span> <span>*/</span> <span>140</span> <span>public</span><span> function setVendorPath($path) </span><span>141</span> <span> { </span><span>142</span> $<span>this</span>->_vendorPath = Yii::getAlias($path);<span>//</span><span> 获取vendor的路径,并存到_vendorPath中</span> <span>143</span> Yii::setAlias(<span>'</span><span>@vendor</span><span>'</span>, $<span>this</span>->_vendorPath);<span>//</span><span> 设置@vendor的alias</span> <span>144</span> Yii::setAlias(<span>'</span><span>@bower</span><span>'</span>, $<span>this</span>->_vendorPath . DIRECTORY_SEPARATOR . <span>'</span><span>bower</span><span>'</span><span>); </span><span>145</span> Yii::setAlias(<span>'</span><span>@npm</span><span>'</span>, $<span>this</span>->_vendorPath . DIRECTORY_SEPARATOR . <span>'</span><span>npm</span><span>'</span><span>); </span><span>146</span> <span> } </span><span>147</span> <span>148</span> <span>/*</span><span>* </span><span>149</span> <span> * Returns the time zone used by this application.取得时区 </span><span>150</span> <span> * This is a simple wrapper of PHP function date_default_timezone_get(). </span><span>151</span> <span> * If time zone is not configured in php.ini or application config, </span><span>152</span> <span> * it will be set to UTC by default. </span><span>153</span> <span> * @return string the time zone used by this application. </span><span>154</span> <span> * @see </span><span>http://php.net/manual/en/function.date-default-timezone-get.php</span> <span>155</span> <span>*/</span> <span>156</span> <span>public</span><span> function getTimeZone() </span><span>157</span> <span> { </span><span>158</span> <span>return</span><span> date_default_timezone_get(); </span><span>159</span> <span> } </span><span>160</span> <span>161</span> <span>/*</span><span>* </span><span>162</span> <span> * Sets the time zone used by this application.设置时区 </span><span>163</span> <span> * This is a simple wrapper of PHP function date_default_timezone_set(). </span><span>164</span> <span> * Refer to the [php manual](</span><span>http://www.php.net/manual/en/timezones.php</span><span>) for available timezones. </span><span>165</span> <span> * @param string $value the time zone used by this application. </span><span>166</span> <span> * @see </span><span>http://php.net/manual/en/function.date-default-timezone-set.php</span> <span>167</span> <span>*/</span> <span>168</span> <span>public</span><span> function setTimeZone($value) </span><span>169</span> <span> { </span><span>170</span> <span> date_default_timezone_set($value); </span><span>171</span> <span> } </span><span>172</span> <span>173</span> <span>/*</span><span>* </span><span>174</span> <span> * Returns the database connection component.返回数据库连接组件 </span><span>175</span> <span> * @return \yii\db\Connection the database connection. </span><span>176</span> <span>*/</span> <span>177</span> <span>public</span><span> function getDb() </span><span>178</span> <span> { </span><span>179</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>db</span><span>'</span><span>); </span><span>180</span> <span> } </span><span>181</span> <span>182</span> <span>/*</span><span>* </span><span>183</span> <span> * Returns the log dispatcher component.返回日志调度组件 </span><span>184</span> <span> * @return \yii\log\Dispatcher the log dispatcher application component. </span><span>185</span> <span>*/</span> <span>186</span> <span>public</span><span> function getLog() </span><span>187</span> <span> { </span><span>188</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>log</span><span>'</span><span>); </span><span>189</span> <span> } </span><span>190</span> <span>191</span> <span>/*</span><span>* </span><span>192</span> <span> * Returns the error handler component.返回错误处理组件 </span><span>193</span> <span> * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component. </span><span>194</span> <span>*/</span> <span>195</span> <span>public</span><span> function getErrorHandler() </span><span>196</span> <span> { </span><span>197</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>errorHandler</span><span>'</span><span>); </span><span>198</span> <span> } </span><span>199</span> <span>200</span> <span>/*</span><span>* </span><span>201</span> <span> * Returns the cache component.返回缓存组件 </span><span>202</span> <span> * @return \yii\caching\Cache the cache application component. Null if the component is not enabled. </span><span>203</span> <span>*/</span> <span>204</span> <span>public</span><span> function getCache() </span><span>205</span> <span> { </span><span>206</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>cache</span><span>'</span>, <span>false</span><span>); </span><span>207</span> <span> } </span><span>208</span> <span>209</span> <span>/*</span><span>* </span><span>210</span> <span> * Returns the formatter component.返回格式化程序组件 </span><span>211</span> <span> * @return \yii\i18n\Formatter the formatter application component. </span><span>212</span> <span>*/</span> <span>213</span> <span>public</span><span> function getFormatter() </span><span>214</span> <span> { </span><span>215</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>formatter</span><span>'</span><span>); </span><span>216</span> <span> } </span><span>217</span> <span>218</span> <span>/*</span><span>* </span><span>219</span> <span> * Returns the request component.返回请求的组件对象 </span><span>220</span> <span> * @return \yii\web\Request|\yii\console\Request the request component. </span><span>221</span> <span>*/</span> <span>222</span> <span>public</span><span> function getRequest() </span><span>223</span> <span> { </span><span>224</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>request</span><span>'</span><span>); </span><span>225</span> <span> } </span><span>226</span> <span>227</span> <span>/*</span><span>* </span><span>228</span> <span> * Returns the response component.返回响应组件 </span><span>229</span> <span> * @return \yii\web\Response|\yii\console\Response the response component. </span><span>230</span> <span>*/</span> <span>231</span> <span>public</span><span> function getResponse() </span><span>232</span> <span> { </span><span>233</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>response</span><span>'</span><span>); </span><span>234</span> <span> } </span><span>235</span> <span>236</span> <span>/*</span><span>* </span><span>237</span> <span> * Returns the view object.返回视图对象 </span><span>238</span> <span> * @return View|\yii\web\View the view application component that is used to render various view files. </span><span>239</span> <span>*/</span> <span>240</span> <span>public</span><span> function getView() </span><span>241</span> <span> { </span><span>242</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>view</span><span>'</span><span>); </span><span>243</span> <span> } </span><span>244</span> <span>245</span> <span>/*</span><span>* </span><span>246</span> <span> * Returns the URL manager for this application.返回当前应用的URL管理组件 </span><span>247</span> <span> * @return \yii\web\UrlManager the URL manager for this application. </span><span>248</span> <span>*/</span> <span>249</span> <span>public</span><span> function getUrlManager() </span><span>250</span> <span> { </span><span>251</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>urlManager</span><span>'</span><span>); </span><span>252</span> <span> } </span><span>253</span> <span>254</span> <span>/*</span><span>* </span><span>255</span> <span> * Returns the internationalization (i18n) component返回国际化组件 </span><span>256</span> <span> * @return \yii\i18n\I18N the internationalization application component. </span><span>257</span> <span>*/</span> <span>258</span> <span>public</span><span> function getI18n() </span><span>259</span> <span> { </span><span>260</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>i18n</span><span>'</span><span>); </span><span>261</span> <span> } </span><span>262</span> <span>263</span> <span>/*</span><span>* </span><span>264</span> <span> * Returns the mailer component.返回邮件组件 </span><span>265</span> <span> * @return \yii\mail\MailerInterface the mailer application component. </span><span>266</span> <span>*/</span> <span>267</span> <span>public</span><span> function getMailer() </span><span>268</span> <span> { </span><span>269</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>mailer</span><span>'</span><span>); </span><span>270</span> <span> } </span><span>271</span> <span>272</span> <span>/*</span><span>* </span><span>273</span> <span> * Returns the auth manager for this application.返回该应用的权限管理组件 </span><span>274</span> <span> * @return \yii\rbac\ManagerInterface the auth manager application component. </span><span>275</span> <span> * Null is returned if auth manager is not configured. 管理权限没有配置返回null </span><span>276</span> <span>*/</span> <span>277</span> <span>public</span><span> function getAuthManager() </span><span>278</span> <span> { </span><span>279</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>authManager</span><span>'</span>, <span>false</span><span>); </span><span>280</span> <span> } </span><span>281</span> <span>282</span> <span>/*</span><span>* </span><span>283</span> <span> * Returns the asset manager.返回资源管理组件 </span><span>284</span> <span> * @return \yii\web\AssetManager the asset manager application component. </span><span>285</span> <span>*/</span> <span>286</span> <span>public</span><span> function getAssetManager() </span><span>287</span> <span> { </span><span>288</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>assetManager</span><span>'</span><span>); </span><span>289</span> <span> } </span><span>290</span> <span>291</span> <span>/*</span><span>* </span><span>292</span> <span> * Returns the security component.返回安全组件 </span><span>293</span> <span> * @return \yii\base\Security the security application component. </span><span>294</span> <span>*/</span> <span>295</span> <span>public</span><span> function getSecurity() </span><span>296</span> <span> { </span><span>297</span> <span>return</span> $<span>this</span>-><span>get</span>(<span>'</span><span>security</span><span>'</span><span>); </span><span>298</span> <span> } </span><span>299</span> <span>300</span> <span>/*</span><span>* </span><span>301</span> <span> * Returns the configuration of core application components.返回核心组件的配置 </span><span>302</span> <span> * @see set() </span><span>303</span> <span>*/</span> <span>304</span> <span>public</span><span> function coreComponents() </span><span>305</span> <span> { </span><span>306</span> <span>return</span><span> [ </span><span>307</span> <span>'</span><span>log</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\log\Dispatcher</span><span>'</span><span>], </span><span>308</span> <span>'</span><span>view</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\web\View</span><span>'</span><span>], </span><span>309</span> <span>'</span><span>formatter</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\i18n\Formatter</span><span>'</span><span>], </span><span>310</span> <span>'</span><span>i18n</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\i18n\I18N</span><span>'</span><span>], </span><span>311</span> <span>'</span><span>mailer</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\swiftmailer\Mailer</span><span>'</span><span>], </span><span>312</span> <span>'</span><span>urlManager</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\web\UrlManager</span><span>'</span><span>], </span><span>313</span> <span>'</span><span>assetManager</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\web\AssetManager</span><span>'</span><span>], </span><span>314</span> <span>'</span><span>security</span><span>'</span> => [<span>'</span><span>class</span><span>'</span> => <span>'</span><span>yii\base\Security</span><span>'</span><span>], </span><span>315</span> <span> ]; </span><span>316</span> <span> } </span><span>317</span> <span>318</span> <span>/*</span><span>* </span><span>319</span> <span> * Terminates the application.终止应用程序 </span><span>320</span> <span> * This method replaces the `exit()` function by ensuring the application life cycle is completed </span><span>321</span> <span> * before terminating the application.该方法代替`exit()` 确认一个应用的生命周期已经结束 </span><span>322</span> <span> * @param integer $status the exit status (value 0 means normal exit while other values mean abnormal exit). </span><span>323</span> <span> * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used. </span><span>324</span> <span> * @throws ExitException if the application is in testing mode </span><span>325</span> <span>*/</span> <span>326</span> <span>public</span> function end($status = <span>0</span>, $response = <span>null</span><span>) </span><span>327</span> <span> { </span><span>328</span> <span>if</span> ($<span>this</span>->state === self::STATE_BEFORE_REQUEST || $<span>this</span>->state ===<span> self::STATE_HANDLING_REQUEST) { </span><span>329</span> <span>//</span><span>判断当前状态为请求前或者处理请求</span> <span>330</span> $<span>this</span>->state = self::STATE_AFTER_REQUEST;<span>//</span><span>设置应用状态为请求完成后</span> <span>331</span> $<span>this</span>-><span>trigger(self::EVENT_AFTER_REQUEST); </span><span>332</span> <span> } </span><span>333</span> <span>334</span> <span>if</span> ($<span>this</span>->state !== self::STATE_SENDING_RESPONSE && $<span>this</span>->state !==<span> self::STATE_END) { </span><span>335</span> <span>//</span><span>如果应用状态不是发送应答和应用结束</span> <span>336</span> $<span>this</span>->state = self::STATE_END;<span>//</span><span>设置状态为应用结束</span> <span>337</span> $response = $response ? : $<span>this</span>-><span>getResponse(); </span><span>338</span> $response->send();<span>//</span><span>向客户端发送应答</span> <span>339</span> <span> } </span><span>340</span> <span>341</span> <span>if</span><span> (YII_ENV_TEST) { </span><span>342</span> <span>throw</span> <span>new</span><span> ExitException($status); </span><span>343</span> } <span>else</span><span> { </span><span>344</span> <span> exit($status); </span><span>345</span> <span> } </span><span>346</span> }
php