-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathWindClassProxy.php
More file actions
239 lines (225 loc) · 7.69 KB
/
WindClassProxy.php
File metadata and controls
239 lines (225 loc) · 7.69 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* 类代理定义
*
* 通过使用类代理机制,可以实现对类方法或属性的监听过滤机制.<code>
* //相关组件配置,只需设置 proxy为true,就可以通过组件工厂创建一个具有代理功能的类实例对象.
* <component name='windApplication' path='WIND:web.WindWebApplication'
* scope='singleton' proxy='true'>
* <properties>
* <property name='dispatcher' ref='dispatcher' />
* <property name='handlerAdapter' ref='router' />
* </properties>
* </component>
* $object = Wind::getApp()->getFactory()->getInstance('windApplication');
* $object->registerEventListener('runProcess', new Listener());
* </code>
* @author Qiong Wu <[email protected]>
* @copyright ©2003-2103 phpwind.com
* @license http://www.windframework.com
* @version $Id$
* @package base
*/
class WindClassProxy {
const EVENT_TYPE_METHOD = 'method';
const EVENT_TYPE_SETTER = 'setter';
const EVENT_TYPE_GETTER = 'getter';
/**
* 默认过滤链类型定义
*
* @var string
*/
private $_interceptorChain = 'WIND:filter.WindHandlerInterceptorChain';
/**
* 过滤链对象
*
* @var WindHandlerInterceptorChain
*/
private $_interceptorChainObj = null;
protected $_className = '';
protected $_classPath = '';
protected $_instance = null;
protected $_listener = array();
/**
* @param object $targetObj 需要被代理监听的类对象实例 默认为null
*/
public function __construct($targetObject = null) {
$this->registerTargetObject($targetObject);
}
/**
* 注册事件以及事件监听类
*
* 通过调用该方法,将事件以及对事件的监听方法注册进来,当事件方法被调用的时候监听的方法被触发.例:<code>
* <component name='windApplication' path='WIND:web.WindWebApplication'
* scope='singleton' proxy='true'>...</component>
* $object = Wind::getApp()->getFactory()->getInstance('windApplication');
* $object->registerEventListener('runProcess', new Listener());
* </code>
* @param stinrg $event 被监听的事件
* @param object $listener 事件监听器
* @param string $type 事件监听类型
* @return void
*/
public function registerEventListener($event, $listener, $type = self::EVENT_TYPE_METHOD) {
if (!in_array($type, array(self::EVENT_TYPE_METHOD, self::EVENT_TYPE_GETTER, self::EVENT_TYPE_SETTER))) {
throw new WindException('[base.WindClassProxy.registerEventListener] Unsupport event type:' . $type, WindException::ERROR_PARAMETER_TYPE_ERROR);
}
!isset($this->_listener[$type][$event]) && $this->_listener[$type][$event] = array();
array_push($this->_listener[$type][$event], $listener);
}
/**
* 注册目标对象,如果已经注册了不重复注册
*
* WindFactory中创建类代理的一段例子:<code>
* $instance = new Object();
* $this->addClassDefinitions($alias, array('path' => $proxy, 'scope' => 'prototype'));
* $proxy = $this->getInstance($alias);
* $proxy->registerTargetObject($instance);
* $instance->_proxy = $proxy;
* </code><note><b>注意:</b>$instance继承自WindModule</note>
* @param object $targetObject
* @return WindClassProxy
*/
public function registerTargetObject($targetObject) {
if ($this->_instance !== null || !is_object($targetObject)) return;
$this->_setClassName(get_class($targetObject));
$this->_instance = $targetObject;
$types = array(self::EVENT_TYPE_METHOD, self::EVENT_TYPE_GETTER, self::EVENT_TYPE_SETTER);
foreach ($types as $type)
$this->_listener[$type] = array();
return $this;
}
/**
* 该方法用于监听类属性
*
* @param string $propertyName 属性名称
* @param mixed $value 属性值
* @deprecated
* @throws WindException
* @return mixed
*/
public function __set($propertyName, $value) {
$property = $this->_getReflection()->getProperty($propertyName);
if (!$property || !$property->isPublic()) {
throw new WindException('undefined property name. ');
}
$listeners = $this->_getListenerByType(self::EVENT_TYPE_SETTER, $propertyName);
if (empty($listeners)) return call_user_func_array(array($this, '_setProperty'), array($propertyName, $value));
$interceptorChain = $this->_getInterceptorChain($propertyName);
$interceptorChain->addInterceptors($listeners);
$interceptorChain->setCallBack(array($this, '_setProperty'), array($propertyName, $value));
return $interceptorChain->getHandler()->handle($value);
}
/**
* 监听属性调用
*
* @param string $propertyName 属性名
* @deprecated
* @return mixed
*/
public function __get($propertyName) {
$property = $this->_getReflection()->getProperty($propertyName);
if (!$property || !$property->isPublic()) {
throw new WindException('undefined property name. ');
}
$listeners = $this->_getListenerByType(self::EVENT_TYPE_GETTER, $propertyName);
if (empty($listeners)) return call_user_func_array(array($this, '_getProperty'), array($propertyName));
$interceptorChain = $this->_getInterceptorChain($propertyName);
$interceptorChain->addInterceptors($listeners);
$interceptorChain->setCallBack(array($this, '_getProperty'), array($propertyName));
return $interceptorChain->getHandler()->handle($propertyName);
}
/**
* 监听类方法
*
* @param string $methodName 方法名
* @param array $args 方法参数
* @return mixed
* @throws WindException
*/
public function __call($methodName, $args) {
$listeners = $this->_getListenerByType(self::EVENT_TYPE_METHOD, $methodName);
if (empty($listeners)) return call_user_func_array(array($this->_getInstance(), $methodName), (array) $args);
$interceptorChain = $this->_getInterceptorChain($methodName);
$interceptorChain->addInterceptors($listeners);
$interceptorChain->setCallBack(array($this->_getInstance(), $methodName), $args);
return call_user_func_array(array($interceptorChain->getHandler(), 'handle'), (array) $args);
}
/**
* 创建并返回过滤链,如果过滤链已经被创建不重复创建
*
* @param string $event 事件名称 默认值为空
* @return WindHandlerInterceptorChain
* @throws WindException
*/
private function _getInterceptorChain($event = '') {
if (null === $this->_interceptorChainObj) {
$chain = Wind::import($this->_interceptorChain);
$interceptorChain = WindFactory::createInstance($chain);
if ($interceptorChain && $interceptorChain instanceof WindHandlerInterceptorChain) {
$this->_interceptorChainObj = $interceptorChain;
} else
throw new WindException('[base.WindClassProxy._getInterceptorChain] Unable to create interceptorChain.');
}
$this->_interceptorChainObj->reset();
return $this->_interceptorChainObj;
}
/**
* 获得事件的所有监听器
*
* @param string $type 监听器类型
* @param string $event 事件名称
* @return array
*/
private function _getListenerByType($type, $event) {
$listener = array();
if (isset($this->_listener[$type]) && isset($this->_listener[$type][$event])) {
$listener = $this->_listener[$type][$event];
}
return $listener;
}
/**
* 返回当前代理对象的真实类对象
*
* @return object
*/
public function _getInstance() {
return $this->_instance;
}
/**
* 返回当前代理对象的真实类名称
*
* @return string
*/
public function _getClassName() {
return $this->_className;
}
/**
* 返回当前代理对象的真实类的路径信息
*
* @return string
*/
public function _getClassPath() {
return $this->_classPath;
}
/**
* 设置类名称
*
* @param string $className
* @return void
*/
public function _setClassName($className) {
$this->_className = $className;
}
/**
* 设置类路径
*
* @param string $classPath
* @return void
*/
public function _setClassPath($classPath) {
$this->_setClassName(Wind::import($classPath));
$this->_classPath = $classPath;
}
}
?>