码迷,mamicode.com
首页 > 其他好文 > 详细

Yii源码阅读笔记(二十六)

时间:2016-05-11 01:15:44      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释:

  1     /**
  2      * Handles the specified request.
  3      * 处理指定的请求--抽象方法
  4      * This method should return an instance of [[Response]] or its child class
  5      * which represents the handling result of the request.
  6      * 该方法应该返回一个[[Response]]实例,或者它的子类代表处理请求的结果
  7      *
  8      * @param Request $request the request to be handled
  9      * @return Response the resulting response
 10      */
 11     abstract public function handleRequest($request);
 12 
 13     private $_runtimePath;
 14 
 15     /**
 16      * Returns the directory that stores runtime files.
 17      * 返回存储运行时文件的路径
 18      * @return string the directory that stores runtime files.
 19      * Defaults to the "runtime" subdirectory under [[basePath]].
 20      * 默认返回[[basePath]]下的 "runtime"目录
 21      */
 22     public function getRuntimePath()
 23     {
 24         if ($this->_runtimePath === null) {
 25             $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . ‘runtime‘);
 26         }
 27 
 28         return $this->_runtimePath;
 29     }
 30 
 31     /**
 32      * Sets the directory that stores runtime files.
 33      * 设置存储运行时文件的路径
 34      * @param string $path the directory that stores runtime files.
 35      */
 36     public function setRuntimePath($path)
 37     {
 38         $this->_runtimePath = Yii::getAlias($path);
 39         Yii::setAlias(‘@runtime‘, $this->_runtimePath);
 40     }
 41 
 42     private $_vendorPath;
 43 
 44     /**
 45      * Returns the directory that stores vendor files.
 46      * 返回插件目录路径
 47      * @return string the directory that stores vendor files.
 48      * Defaults to "vendor" directory under [[basePath]].
 49      * 默认返回[[basePath]]下的 "vendor" 目录
 50      */
 51     public function getVendorPath()
 52     {
 53         if ($this->_vendorPath === null) {
 54             $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . ‘vendor‘);
 55         }
 56 
 57         return $this->_vendorPath;
 58     }
 59 
 60     /**
 61      * Sets the directory that stores vendor files.
 62      * 设置插件目录路径,并设置别名
 63      * @param string $path the directory that stores vendor files.
 64      */
 65     public function setVendorPath($path)
 66     {
 67         $this->_vendorPath = Yii::getAlias($path);
 68         Yii::setAlias(‘@vendor‘, $this->_vendorPath);
 69         Yii::setAlias(‘@bower‘, $this->_vendorPath . DIRECTORY_SEPARATOR . ‘bower‘);
 70         Yii::setAlias(‘@npm‘, $this->_vendorPath . DIRECTORY_SEPARATOR . ‘npm‘);
 71     }
 72 
 73     /**
 74      * Returns the time zone used by this application.
 75      * 取得时区
 76      * This is a simple wrapper of PHP function date_default_timezone_get().
 77      * If time zone is not configured in php.ini or application config,
 78      * it will be set to UTC by default.
 79      * @return string the time zone used by this application.
 80      * @see http://php.net/manual/en/function.date-default-timezone-get.php
 81      */
 82     public function getTimeZone()
 83     {
 84         return date_default_timezone_get();
 85     }
 86 
 87     /**
 88      * Sets the time zone used by this application.
 89      * 设置时区
 90      * This is a simple wrapper of PHP function date_default_timezone_set().
 91      * Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
 92      * @param string $value the time zone used by this application.
 93      * @see http://php.net/manual/en/function.date-default-timezone-set.php
 94      */
 95     public function setTimeZone($value)
 96     {
 97         date_default_timezone_set($value);
 98     }
 99 
100     /**
101      * Returns the database connection component.
102      * 返回数据库连接组件
103      * @return \yii\db\Connection the database connection.
104      */
105     public function getDb()
106     {
107         return $this->get(‘db‘);
108     }
109 
110     /**
111      * Returns the log dispatcher component.
112      * 返回日志调度组件
113      * @return \yii\log\Dispatcher the log dispatcher application component.
114      */
115     public function getLog()
116     {
117         return $this->get(‘log‘);
118     }
119 
120     /**
121      * Returns the error handler component.
122      * 返回错误处理组件
123      * @return \yii\web\ErrorHandler|\yii\console\ErrorHandler the error handler application component.
124      */
125     public function getErrorHandler()
126     {
127         return $this->get(‘errorHandler‘);
128     }
129 
130     /**
131      * Returns the cache component.
132      * 返回缓存组件
133      * @return \yii\caching\Cache the cache application component. Null if the component is not enabled.
134      */
135     public function getCache()
136     {
137         return $this->get(‘cache‘, false);
138     }
139 
140     /**
141      * Returns the formatter component.
142      * 返回格式化程序组件
143      * @return \yii\i18n\Formatter the formatter application component.
144      */
145     public function getFormatter()
146     {
147         return $this->get(‘formatter‘);
148     }
149 
150     /**
151      * Returns the request component.
152      * 返回请求的组件对象
153      * @return \yii\web\Request|\yii\console\Request the request component.
154      */
155     public function getRequest()
156     {
157         return $this->get(‘request‘);
158     }
159 
160     /**
161      * Returns the response component.
162      * 返回响应组件
163      * @return \yii\web\Response|\yii\console\Response the response component.
164      */
165     public function getResponse()
166     {
167         return $this->get(‘response‘);
168     }
169 
170     /**
171      * Returns the view object.
172      * 返回视图对象
173      * @return View|\yii\web\View the view application component that is used to render various view files.
174      */
175     public function getView()
176     {
177         return $this->get(‘view‘);
178     }
179 
180     /**
181      * Returns the URL manager for this application.
182      * 返回当前应用的URL管理组件
183      * @return \yii\web\UrlManager the URL manager for this application.
184      */
185     public function getUrlManager()
186     {
187         return $this->get(‘urlManager‘);
188     }
189 
190     /**
191      * Returns the internationalization (i18n) component
192      * 返回国际化组件
193      * @return \yii\i18n\I18N the internationalization application component.
194      */
195     public function getI18n()
196     {
197         return $this->get(‘i18n‘);
198     }
199 
200     /**
201      * Returns the mailer component.
202      * 返回邮件组件
203      * @return \yii\mail\MailerInterface the mailer application component.
204      */
205     public function getMailer()
206     {
207         return $this->get(‘mailer‘);
208     }
209 
210     /**
211      * Returns the auth manager for this application.
212      * 返回该应用的权限管理组件
213      * @return \yii\rbac\ManagerInterface the auth manager application component.
214      * Null is returned if auth manager is not configured.
215      */
216     public function getAuthManager()
217     {
218         return $this->get(‘authManager‘, false);
219     }
220 
221     /**
222      * Returns the asset manager.
223      * 返回资源管理组件
224      * @return \yii\web\AssetManager the asset manager application component.
225      */
226     public function getAssetManager()
227     {
228         return $this->get(‘assetManager‘);
229     }
230 
231     /**
232      * Returns the security component.
233      * 返回安全组件
234      * @return \yii\base\Security the security application component.
235      */
236     public function getSecurity()
237     {
238         return $this->get(‘security‘);
239     }
240 
241     /**
242      * Returns the configuration of core application components.
243      * 返回核心组件的配置
244      * @see set()
245      */
246     public function coreComponents()
247     {
248         return [
249             ‘log‘ => [‘class‘ => ‘yii\log\Dispatcher‘],
250             ‘view‘ => [‘class‘ => ‘yii\web\View‘],
251             ‘formatter‘ => [‘class‘ => ‘yii\i18n\Formatter‘],
252             ‘i18n‘ => [‘class‘ => ‘yii\i18n\I18N‘],
253             ‘mailer‘ => [‘class‘ => ‘yii\swiftmailer\Mailer‘],
254             ‘urlManager‘ => [‘class‘ => ‘yii\web\UrlManager‘],
255             ‘assetManager‘ => [‘class‘ => ‘yii\web\AssetManager‘],
256             ‘security‘ => [‘class‘ => ‘yii\base\Security‘],
257         ];
258     }
259 
260     /**
261      * Terminates the application.
262      * 终止应用程序
263      * This method replaces the `exit()` function by ensuring the application life cycle is completed
264      * before terminating the application.
265      * 该方法代替`exit()` 方法确认一个应用的生命周期已经结束
266      * @param integer $status the exit status (value 0 means normal exit while other values mean abnormal exit).
267      * @param Response $response the response to be sent. If not set, the default application [[response]] component will be used.
268      * @throws ExitException if the application is in testing mode
269      */
270     public function end($status = 0, $response = null)
271     {
272         if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {//判断当前状态为请求前或者处理请求
273             $this->state = self::STATE_AFTER_REQUEST;//则设置应用状态为请求完成后
274             $this->trigger(self::EVENT_AFTER_REQUEST);//触发afterRequest事件
275         }
276 
277         if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {//如果应用状态不是发送应答和应用结束
278             $this->state = self::STATE_END;//设置状态为应用结束
279             $response = $response ? : $this->getResponse();//
280             $response->send();//向客户端发送应答
281         }
282 
283         if (YII_ENV_TEST) {
284             throw new ExitException($status);
285         } else {
286             exit($status);
287         }
288     }

 

Yii源码阅读笔记(二十六)

标签:

原文地址:http://www.cnblogs.com/isleep/p/5479919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!