-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProxyMethod.php
264 lines (241 loc) · 8.14 KB
/
ProxyMethod.php
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* Class ProxyMethod
* @author xiaofeng
* 代理类的方法
* 可以代理private protected方法,不使用setAccessible,无副作用
*/
class ProxyMethod
{
private static $staticMethods = []; // bug
private $methods = [];
/* @var ReflectionClass $refClass */
private $refClass;
private $ctx;
/**
* @param mixed $class classname or object
* @param mixed ctor arg1 , ctor arg2...
* Proxy constructor.
*/
public function __construct()
{
list($this->refClass, $this->ctx) =self::getRefCtx(func_get_args());
$this->proxy();
}
public function __destruct()
{
self::$staticMethods = [];
}
public static function __callStatic($name, $args)
{
if(!isset(self::$staticMethods[$name])) {
throw new BadMethodCallException ("method::{$name} does not exists.");
}
/* @var ReflectionMethod $method */
$method = self::$staticMethods[$name];
// return $method->invokeArgs(null, $args);
// pass Accessible
return call_user_func_array($method->getClosure(), $args);
}
public function __call($name, $args)
{
if(!isset($this->methods[$name])) {
throw new BadMethodCallException ("method->{$name} does not exists.");
}
/* @var ReflectionMethod $method */
$method = $this->methods[$name];
// return $method->invokeArgs($this->ctx, $args);
// pass Accessible
return call_user_func_array($method->getClosure($this->ctx), $args);
}
private function proxy()
{
// filter do not work
$allMethods = $this->refClass->getMethods();
/* @var ReflectionMethod $method */
foreach($allMethods as $method) {
if($method->isAbstract()) {
continue;
}
// no side effect
// $method->setAccessible(true);
if($method->isStatic()) {
self::$staticMethods[$method->name] = $method;
} else {
$this->methods[$method->name] = $method;
}
}
}
/**
* rename method name
* @param string $name old method name
* @param string $newName new method name
* @param boolean $deleteOld
* @throws InvalidArgumentException
* @return null
*/
public function rename($name, $newName, $deleteOld = true)
{
if(isset($this->methods[$newName]) || isset(self::$staticMethods[$newName])) {
throw new InvalidArgumentException("rename error: [class.{$this->refClass->name} method.{$newName}] has already exists.");
}
if(isset($this->methods[$name])) {
$this->methods[$newName] = $this->methods[$name];
if($deleteOld) {
unset($this->methods[$name]);
}
} else if(isset(self::$staticMethods[$name])) {
self::$staticMethods[$newName] = self::$staticMethods[$name];
if($deleteOld) {
unset(self::$staticMethods[$name]);
}
} else {
throw new InvalidArgumentException("rename error: [class.{$this->refClass->name} method.{$name}] do not exists.");
}
}
/**
* alias method
* @param string $name old method name
* @throws InvalidArgumentException
* @return null
*/
public function alias($name, $alisaName)
{
$this->rename($name, $alisaName, false);
}
/**
* getClosure
* @param $name
* @return Closure
*/
public function getClosure($name)
{
$methods = $this->methods + self::$staticMethods;
if(!isset($methods[$name])) {
throw new BadMethodCallException ("class.{$this->refClass->name} method.{$name} does not exists.");
}
/* @var ReflectionMethod $method */
$method = $methods[$name];
if($method->isStatic()) {
return $method->getClosure();
} else {
return $method->getClosure($this->ctx);
}
}
/**
* export method to global function
* @param string $name
* @param string $exportName
* @return null
*/
public function export($name, $exportName = "")
{
$methods = $this->methods + self::$staticMethods;
if(!isset($methods[$name])) {
throw new BadMethodCallException ("class.{$this->refClass->name} method.{$name} does not exists.");
}
if(!$exportName) {
$exportName = $name;
}
if(function_exists($exportName)) {
throw new InvalidArgumentException("function.{$exportName} has already exists.");
}
/* @var ReflectionMethod $method */
$method = $methods[$name];
if($method->isStatic()) {
// export static method
$func = <<<FUNC
function {$exportName}() {
\$ref_method = new ReflectionMethod("{$method->class}", "{$method->name}");
return call_user_func_array(\$ref_method->getClosure(), func_get_args());
}
FUNC;
} else {
$class_name = $this->refClass->name;
// export not static method
// avoid setAccessible(true) using getClosure
$func = <<<FUNC
function {$exportName}() {
if(func_num_args() < 1) {
throw new InvalidArgumentException("first argument must be object intanceof {$class_name}).");;
}
\$args = func_get_args();
\$class = array_shift(\$args);
\$ctx = null;
if(is_object(\$class) && (\$class instanceof {$class_name})) {
\$ctx = \$class;
} else {
throw new InvalidArgumentException("first argument must be object intanceof {$class_name}).");;
}
\$ref_method = new ReflectionMethod(\$ctx, "{$method->name}");
return call_user_func_array(\$ref_method->getClosure(\$ctx), \$args);
}
FUNC;
}
self::myEval($func);
}
/**
* get refClass and Ctx object
* @param array $args
* @return array
*/
private static function getRefCtx(array $args)
{
$ctx = null;
$refClass = null;
$e = new InvalidArgumentException("first argument must be string(className with ctor parameter) or object.");
if (count($args) < 1) {
throw $e;
}
$class = array_shift($args);
if (is_object($class)) {
$refClass = new ReflectionClass($class);
$ctx = $class;
} else if (is_string($class)) {
if (!class_exists($class)) {
throw $e ;
}
$refClass = new ReflectionClass($class);
$className = $refClass->name;
if ($ctor = $refClass->getConstructor()) {
if($paras = $refClass->getConstructor()->getParameters()) {
$at_least_ctor_paras_num = count($paras);
/* @var ReflectionParameter $para*/
foreach($paras as $para) {
if($para->isOptional()) {
$at_least_ctor_paras_num--;
}
}
$left_arg_num = count($args);
if ($left_arg_num < $at_least_ctor_paras_num) {
throw new InvalidArgumentException("class $className ctor need $at_least_ctor_paras_num parameters, but $left_arg_num be given.");
}
}
if($ctor->isPublic()) {
$ctx = $refClass->newInstanceArgs($args);
} else {
$ctx = $refClass->newInstanceWithoutConstructor();
$ctor->setAccessible(true);
$ctor->invokeArgs($ctx, $args);
$ctor->setAccessible(false);
}
} else {
$ctx = $refClass->newInstanceArgs();
}
} else {
throw $e;
}
return [$refClass, $ctx];
}
private static function myEval($code)
{
$flag = false;
eval('$flag = true;');
if($flag) {
return eval($code);
} else {
file_put_contents($tmp = tempnam(sys_get_temp_dir(), "eval"), "<?php\n$code");
return include($tmp);
}
}
}