Yii源码阅读笔记(四)

所有控制器action的基类yii\base\Action.php

 1 namespace yii\base;//定义的命名空间
 2
 3 use Yii //使用的命名空间
 4
 5 class Action extends Component //继承了组建类
 6 {
 7      8     //定义属性action的id
 9
10     public $id;
11
14     public $controller; //定义拥有该action的控制器属性
15
24     public function __construct($id, $controller, $config = [])//构造函数,用于初始化id和controller属性
25     {
26         $this->id = $id;
27         $this->controller = $controller;
28         parent::__construct($config);
29     }
30
31       //通过调用控制器中的同名方法,返回在当前应用程序中的所有模块中唯一标识该模块的标识35
36     public function getUniqueId()
37     {
38         return $this->controller->getUniqueId() . ‘/‘ . $this->id;
39     }
40   //用指定的参数运行此操作
49     public function runWithParams($params)
50     {
51         if (!method_exists($this, ‘run‘)) {//如果类中的run方法是不存在
52             throw new InvalidConfigException(get_class($this) . ‘ must define a "run()" method.‘);//抛出异常
53         }
54         $args = $this->controller->bindActionParams($this, $params);//控制器的方法,绑定参数
55         Yii::trace(‘Running action: ‘ . get_class($this) . ‘::run()‘, __METHOD__);//输出trace信息
56         if (Yii::$app->requestedParams === null) {  //如果Yii::$app的属性requestedParams(操作请求的参数)为空
57             Yii::$app->requestedParams = $args;//该参数的值为上面绑定参数的返回值
58         }
59         if ($this->beforeRun()) {//beforRun方法,默认返回true,意思为run前
60             $result = call_user_func_array([$this, ‘run‘], $args);//把$args作为参数来调用当前控制器的run方法
61             $this->afterRun();//空方法
62
63             return $result;//返回函数的调用结果,如出错,返回false
64         } else {
65             return null;//否则返回空
66         }
67     }
68
69
76     protected function beforeRun()//该方法在run之前执行,调用时重写该方法,如果返回为false,则run方法不执行,runwithparams方法返回空
77     {
78         return true;
79     }
80
81     //该方法在run方法后执行,用于处理后续操作
85     protected function afterRun()
86     {
87     }
88 }

component.php组件类

class Component extends Object  //继承了object类
{

    private $_events = [];//私有的events属性

    private $_behaviors;//私有的behaviors属性

public function __get($name)//__get魔术方法,用于返回组组件的属性值
    {
        $getter = ‘get‘ . $name;//通过连字符构建该属性的get方法名
        if (method_exists($this, $getter)) {//如果该方法存在
            // read property, e.g. getName()
            return $this->$getter();//调用该方法获取属性
        } else {
            // behavior property
            $this->ensureBehaviors();//调用ensureBehaviors()方法确定该行为类中定义了该属性
            foreach ($this->_behaviors as $behavior) {
                if ($behavior->canGetProperty($name)) {//如果该行为类可以获取属性
                    return $behavior->$name;//返回该行为类的属性值
                }
            }
        }
        if (method_exists($this, ‘set‘ . $name)) {//如果该属性名的set方法存在抛出异常
            throw new InvalidCallException(‘Getting write-only property: ‘ . get_class($this) . ‘::‘ . $name);
        } else {//否则抛出异常,未知的属性
            throw new UnknownPropertyException(‘Getting unknown property: ‘ . get_class($this) . ‘::‘ . $name);
        }
    }

component的__set方法

 1  public function __set($name, $value)
 2     {
 3         $setter = ‘set‘ . $name;//在属性名前面加set构建set方法
 4         if (method_exists($this, $setter)) {//如果构建的方法存在
 5             // 调用该方法设置属性值
 6             $this->$setter($value);
 7
 8             return;
 9         } elseif (strncmp($name, ‘on ‘, 3) === 0) {//如果方法不存在,且属性名前三个字符为on+空格
10             // on event: attach event handler
11             $this->on(trim(substr($name, 3)), $value);//调用on方法将事件处理程序附加到事件
12
13             return;
14         } elseif (strncmp($name, ‘as ‘, 3) === 0) {//否则,如果属性名的前三个字符为as+空格
15             // as behavior: attach behavior
16             $name = trim(substr($name, 3));//截取as后面的字符
17             $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));//嗲用attachBehavior添加一个行为到组件
18
19             return;
20         } else {//否则
21             // behavior property
22             $this->ensureBehaviors();//调用ensureBehaviors()方法确定该行为类中定义了该属性
23             foreach ($this->_behaviors as $behavior) {
24                 if ($behavior->canSetProperty($name)) {//调用方法判断该属性是否口试被赋值
25                     $behavior->$name = $value;//给该行为类的属性赋值
26
27                     return;
28                 }
29             }
30         }
31         if (method_exists($this, ‘get‘ . $name)) {//如果该属性的get方法存在,抛出异常
32             throw new InvalidCallException(‘Setting read-only property: ‘ . get_class($this) . ‘::‘ . $name);
33         } else {//否则抛出异常,未知的属性
34             throw new UnknownPropertyException(‘Setting unknown property: ‘ . get_class($this) . ‘::‘ . $name);
35         }
36     }
时间: 2024-08-25 04:46:16

Yii源码阅读笔记(四)的相关文章

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(三十四)

Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 1 namespace yii\di; 2 3 use Yii; 4 use yii\base\InvalidConfigException; 5 6 /** 7 * Instance represents a reference to a named object in a dependency injection (DI) container or a service locator. 8 * Instance 表示依赖

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

Module类中获取子模块,注册子模块,实例化控制器,根据路由运行指定控制器方法的注释: 1 /** 2 * Retrieves the child module of the specified ID. 3 * 取出指定模块的子模块 4 * This method supports retrieving both child modules and grand child modules. 5 * 该方法支持检索子模块和子模块的子模块 6 * @param string $id module

Yii源码阅读笔记(十四)

Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: 1 /** 2 * Returns a list of scenarios and the corresponding active attributes. 3 * An active attribute is one that is subject to validation in the current scenario. 4 * 返回所有场景及与之对应的 active 属性的列表 5 * active 属性是指在默认场景中验

Yii源码阅读笔记(二)

接下来阅读BaseYii.php vendor/yiisoft/yii2/Yii.php—— 1 namespace yii; 2 3 use yii\base\InvalidConfigException; 4 use yii\base\InvalidParamException; 5 use yii\base\UnknownClassException; 6 use yii\log\Logger; 7 use yii\di\Container; 第1行定义命名空间为yii: 第3到7行使用了

Yii源码阅读笔记(三)

接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— 1 public static function getRootAlias($alias)//获取根别名 2 { //查找别名中斜线的位置 3 $pos = strpos($alias, '/'); //根据斜线的结果判断,如果不包含斜线,表示输入为根别名,否则截取斜线前面的部分作为根别名 4 $root = $pos === false ? $alias : substr($alias

Yii源码阅读笔记(八)

前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 nam

Yii源码阅读笔记(三十三)

ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: 1 namespace yii\di; 2 3 use Yii; 4 use Closure; 5 use yii\base\Component; 6 use yii\base\InvalidConfigException; 7 8 /** 9 * ServiceLocator implements a [service locator](http://en.wi