标签:
Behvaior类,Behavior类是所有事件类的基类:
1 namespace yii\base; 2 3 /** 4 * Behavior is the base class for all behavior classes. 5 * 所有行为的基类 6 * A behavior can be used to enhance the functionality of an existing component without modifying its code. 7 * In particular, it can "inject" its own methods and properties into the component 8 * and make them directly accessible via the component. It can also respond to the events triggered in the component 9 * and thus intercept the normal code execution. 10 * 11 * @author Qiang Xue <qiang.xue@gmail.com> 12 * @since 2.0 13 */ 14 class Behavior extends Object 15 { 16 /** 17 * @var 行为的依附对象 18 */ 19 public $owner; 20 21 22 /** 23 * Declares event handlers for the [[owner]]‘s events. 24 * 表示这个行为将对类的何种事件进行何种反馈即可 25 * 26 * Child classes may override this method to declare what PHP callbacks should 27 * be attached to the events of the [[owner]] component. 28 * 29 * The callbacks will be attached to the [[owner]]‘s events when the behavior is 30 * attached to the owner; and they will be detached from the events when 31 * the behavior is detached from the component. 32 * 33 * The callbacks can be any of the following: 34 * 事件handler可以是以下形式: 35 * 36 * - method in this behavior: `‘handleClick‘`, equivalent to `[$this, ‘handleClick‘]` 37 * 字符串,表示行为类的方法,和事件不同,事件总的字符串形式表示全局函数,这里表示当前行为类的方法 38 * - object method: `[$object, ‘handleClick‘]` 39 * 一个对象或类的成员函数,以数组的形式 40 * - static method: `[‘Page‘, ‘handleClick‘]` 41 * - anonymous function: `function ($event) { ... }` 42 * 一个匿名函数 43 * 44 * The following is an example: 45 * 46 * ```php 47 * [ 48 * Model::EVENT_BEFORE_VALIDATE => ‘myBeforeValidate‘, 49 * Model::EVENT_AFTER_VALIDATE => ‘myAfterValidate‘, 50 * ] 51 * ``` 52 * 53 * @return 行为所有要响应的事件. 54 */ 55 public function events() 56 { 57 return []; 58 } 59 60 /** 61 * Attaches the behavior object to the component. 62 * 绑定行为到组件 63 * The default implementation will set the [[owner]] property 64 * and attach event handlers as declared in [[events]]. 65 * 该方法会默认设置[[owner]]属性,且添加事件处理程序绑定到组件 66 * Make sure you call the parent implementation if you override this method. 67 * @param Component $owner the component that this behavior is to be attached to. 68 */ 69 public function attach($owner) 70 { 71 $this->owner = $owner;//设置行为的 $owner ,使得行为可以访问、操作所依附的对象 72 foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组 73 //将准备响应的事件,通过所依附类的 on() 绑定到类上 74 $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);//这里判断了事件是否是字符串的形式,如果是字符串,表示当前行为类的方法,用$this对象的形式添加 75 } 76 } 77 78 /** 79 * Detaches the behavior object from the component. 80 * 解除行为的绑定 81 * The default implementation will unset the [[owner]] property 82 * 默认将 owner 属性设置为 null ,且解除绑定到类的处理程序 83 * and detach event handlers declared in [[events]]. 84 * 85 * Make sure you call the parent implementation if you override this method. 86 */ 87 public function detach() 88 { 89 if ($this->owner) { 90 foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组 91 //通过Component的 off() 将绑定到类上的事件hanlder解除下来 92 $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler); 93 } 94 $this->owner = null;//将 $owner 设置为 null ,表示这个行为没有依附到任何类上 95 } 96 } 97 }
Request.php用于获取用户请求:
1 namespace yii\base; 2 3 use Yii; 4 5 /** 6 * Request represents a request that is handled by an [[Application]]. 7 * 8 * @property boolean $isConsoleRequest The value indicating whether the current request is made via console. 9 * @property string $scriptFile Entry script file path (processed w/ realpath()). 10 * 11 * @author Qiang Xue <qiang.xue@gmail.com> 12 * @since 2.0 13 */ 14 abstract class Request extends Component 15 { 16 // 属性scriptFile,用于表示入口脚本 17 private $_scriptFile; 18 // 属性isConsoleRequest,用于表示是否是命令行应用 19 private $_isConsoleRequest; 20 21 22 /** 23 * Resolves the current request into a route and the associated parameters. 24 * 抽象函数,要求子类来实现 这个函数的功能主要是为了把Request解析成路由和相应的参数 25 * @return array the first element is the route, and the second is the associated parameters. 26 */ 27 abstract public function resolve(); 28 29 /** 30 * Returns a value indicating whether the current request is made via command line 31 * isConsoleRequest属性的getter函数 使用 PHP_SAPI 常量判断当前应用是否是命令行应用 32 * @return boolean the value indicating whether the current request is made via console 33 */ 34 public function getIsConsoleRequest() 35 { 36 // PHP_SAPI --判断解析php服务是由那种服务器软件,是采用那种协议 37 return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === ‘cli‘; 38 } 39 40 /** 41 * Sets the value indicating whether the current request is made via command line 42 * isConsoleRequest属性的setter函数 用来设置是否是命令行应用 43 * @param boolean $value the value indicating whether the current request is made via command line 44 */ 45 public function setIsConsoleRequest($value) 46 { 47 $this->_isConsoleRequest = $value; 48 } 49 50 /** 51 * Returns entry script file path. 52 * scriptFile属性的getter函数 通过 $_SERVER[‘SCRIPT_FILENAME‘] 来获取入口脚本名 53 * @return string entry script file path (processed w/ realpath()) 54 * @throws InvalidConfigException if the entry script file path cannot be determined automatically. 55 */ 56 public function getScriptFile() 57 { 58 if ($this->_scriptFile === null) { 59 if (isset($_SERVER[‘SCRIPT_FILENAME‘])) { 60 $this->setScriptFile($_SERVER[‘SCRIPT_FILENAME‘]); 61 } else { 62 throw new InvalidConfigException(‘Unable to determine the entry script file path.‘); 63 } 64 } 65 66 return $this->_scriptFile; 67 } 68 69 /** 70 * Sets the entry script file path. 71 * scriptFile属性的setter函数,用于设置入口脚本 72 * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable. 73 * However, for some server configurations, this may not be correct or feasible. 74 * This setter is provided so that the entry script file path can be manually specified. 75 * @param string $value the entry script file path. This can be either a file path or a path alias. 76 * @throws InvalidConfigException if the provided entry script file path is invalid. 77 */ 78 public function setScriptFile($value) 79 { 80 $scriptFile = realpath(Yii::getAlias($value)); 81 if ($scriptFile !== false && is_file($scriptFile)) { 82 $this->_scriptFile = $scriptFile; 83 } else { 84 throw new InvalidConfigException(‘Unable to determine the entry script file path.‘); 85 } 86 } 87 }
标签:
原文地址:http://www.cnblogs.com/isleep/p/5402856.html