-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext.php
More file actions
119 lines (107 loc) · 3.02 KB
/
context.php
File metadata and controls
119 lines (107 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* 全局的上下文环境,用于保存请求参数和用户信息等
*
* context一旦初始化,就不能再修改
* 部分信息可在调用时初始化,如用户信息,此时类中需要包含get_字段名()的方法来获取并设置信息
* 注意:即使是可设置的属性,也只能设置一次
*
* 使用代码
* <code>
* $c = array(
* 'a' => 'aa',
* 'b' => 'bb',
* );
* $cc = CContext::instance($c);
* // 以下方式获取到的信息是相同的
* $cc->a = 1;
* $cc['a'] = 1;
*
* // 第一次调用存在get_xxx方法的属性时,offsetGet会调用对应的get_xxx来获取并设置相应项的信息
* $cc->userinfo;
* // 此类属性暂时可通过外界设置一次,如 $cc->aaa = '1111111';
* // 只能设置1次,重复赋值将抛出异常,看实际需求,可能在未来版本中去掉此功能
* </code>
*
* for auto complete
* @property string type
* @property array userinfo
*/
class CContext extends Context {
/**
* 取属性:可依情况调用个性化的 get_XXX 函数
*
* @param string $name 属性名
* @return mixed 属性值
*/
public function offsetGet($name) {
$method = 'get_' . $name;
if (!$this->offsetExists($name) && method_exists($this, $method)) {
parent::offsetSet($name, $this->$method());
}
return parent::offsetGet($name);
}
/**
* 设置属性:属性只能设置一次
* @param string $name 属性名
* @param mixed $value 属性值
* @return bool 设置成功返回 true
* @throws Exception 设置失败抛出异常(主要是发生重复设置时抛出异常)
*/
public function offsetSet($name, $value) {
if ($this->offsetExists($name)) {
throw new Exception(__CLASS__.'\'s property can not be overrided');
}
parent::offsetSet($name, $value);
return true;
}
/**
* 获取用户信息
* @return void
*/
protected function get_userinfo() {
$user = new User();
$info = $user->get();
unset($user);
return $info;
}
}
/**
* 全局的上下文环境
*/
class Context extends ArrayObject {
private static $_instance = array();
/**
* ArrayObjec的构造函数为public,不能降低访问权限
* 为了维持context的全局性,使用标志位控制实例化
* @var bool
*/
private static $_instance_flag = false;
final public function __construct($params) {
if (!self::$_instance_flag) {
throw new Context_Exception('Please use instance to create Context.');
}
parent::__construct($params, ArrayObject::ARRAY_AS_PROPS);
}
/**
* @param array $params
* @return mixed
*/
public static function instance(array $params=array()) {
$type = get_called_class();
if (!isset(self::$_instance[$type])) {
self::$_instance_flag = true;
self::$_instance[$type] = new $type($params);
self::$_instance_flag = false;
}
return self::$_instance[$type];
}
/**
* 获取context类型,默认使用类的名字
* @return string
*/
public function type() {
return get_class($this);
}
}
class Context_Exception extends Exception {}