程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> yii2源碼學習筆記(十),yii2源碼學習筆記

yii2源碼學習筆記(十),yii2源碼學習筆記

編輯:關於PHP編程

yii2源碼學習筆記(十),yii2源碼學習筆記


繼續了解Application.

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

 

php

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved