-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByRefPassthrough.php
272 lines (229 loc) · 8.55 KB
/
ByRefPassthrough.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
264
265
266
267
268
269
270
271
272
<?php
/**
* ByRefPassthrough.php
* A simple object which attempts to seamlessly redirect calls through to callbacks.
*
* @package Cericlabs
* @subpackage Misc
*
* @author Chris "Ceiu" Rog <[email protected]>
*/
namespace Cericlabs\Misc;
use ReflectionClass,
ReflectionFunction;
/**
* The ByRefPassthrough object is a simple object which attempts to seamlessly redirect calls
* through to callbacks to test whether or not PHP is properly mapping references between calls.
*
* This class defines eight methods of interest, each slightly changing how the callback is
* executed in an effort to properly execute it while passing arguments both by-ref and by-val.
*
* @package Cericlabs
* @subpackage Misc
*
* @author Chris "Ceiu" Rog <[email protected]>
*/
abstract class ByRefPassthrough
{
/**
* The callback to receive our redirected calls.
*
* @var callback
*/
protected $callback;
////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Creates a new ByRefPassthrough using the specified callback. Protected because we don't want
* this thing getting allocated directly -- we still have some black magic to do.
*
* @param callable $callback
* The callback to which calls should be redirected.
*/
protected function __construct(callable $callback)
{
$this->callback = $callback;
}
/**
* Creates a new ByRefPassthrough using the specified callback. This function dynamically creates
* the necessary subclass for the closure to achieve it's passthrough magic.
*
* @param callable $callback
* The callback to which calls should be redirected.
*/
public static final function newInstance(callable $callback)
{
$reflection = static::getReflection($callback);
$plist = '';
$pcount = 0;
foreach ($reflection->getParameters() as $param) {
if ($pcount++) {
$plist .= ', ';
}
// Typehinting
if ($param->isArray()) {
$plist .= 'array ';
} else if ($param->isCallable()) {
$plist .= 'callable ';
} else if ($rc = $param->getClass()) {
$plist .= '\\' . $rc->getName() . ' ';
}
// By-ref
if ($param->isPassedByReference()) {
$plist .= '&';
}
// Name
$plist .= '$' . $param->getName();
// Default value
if ($param->isDefaultValueAvailable()) {
$value = $param->getDefaultValue();
if (is_string($value)) {
$value = '\'' . addslashes($value) . '\'';
}
$plist .= ' = ' . ($value ? (string) $value : 'null');
}
}
$rtype = $reflection->returnsReference() ? 'R' : 'V';
$class = __CLASS__ . "_{$rtype}" . bin2hex($plist);
return new $class($callback);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Retrieves an appropriate reflection object for the current callback.
*/
protected static function getReflection(callable $callback)
{
if (is_array($callback) && count($callback) === 2) {
$rc = new ReflectionClass($callback[0]);
return $rc->getMethod($callback[1]);
}
return new ReflectionFunction($callback);
}
/**
* Executes the callback using the "traditional" methods of execution: call_user_func_array with
* func_get_args.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWork()
{
return call_user_func_array($this->callback, func_get_args());
}
/**
* Executes the callback using ReflectionFunction's invokeArgs method and func_get_args.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWorkUsingReflection()
{
$reflection = static::getReflection($this->callback);
return ($reflection instanceof ReflectionFunction ? $reflection->invokeArgs(func_get_args()) : $reflection->invokeArgs($this->callback[0], func_get_args()));
}
/**
* Executes the callback using call_user_func_array and debug_backtrace to retrieve the arguments
* to pass to the callback.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWorkUsingStack()
{
$stack = debug_backtrace(0);
return call_user_func_array($this->callback, $stack[0]['args']);
}
/**
* Executes the callback using ReflectionFunction's invokeArgs method and debug_backtrace.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWorkUsingReflectionWithStack()
{
$stack = debug_backtrace(0);
$reflection = static::getReflection($this->callback);
$result = ($reflection instanceof ReflectionFunction ? $reflection->invokeArgs($stack[0]['args']) : $reflection->invokeArgs($this->callback[0], $stack[0]['args']));
return $result;
}
/**
* Executes the callback using the "traditional" methods of execution: call_user_func_array with
* func_get_args.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWorkWithMagic(&$arg0 = null, &$arg1 = null, &$arg2 = null)
{
return call_user_func_array($this->callback, func_get_args());
}
/**
* Executes the callback using ReflectionFunction's invokeArgs method and func_get_args.
*
* This should fail to pass arguments by-ref, but by-val is okay.
*/
public function doWorkUsingReflectionAndMagic(&$arg0 = null, &$arg1 = null, &$arg2 = null)
{
$reflection = static::getReflection($this->callback);
return ($reflection instanceof ReflectionFunction ? $reflection->invokeArgs(func_get_args()) : $reflection->invokeArgs($this->callback[0], func_get_args()));
}
/**
* Executes the callback using call_user_func_array and debug_backtrace to retrieve the arguments
* to pass to the callback.
*
* This method should succeed in passing arguments by-ref if they're within the first three
* parameters. By-val should work on any parameter.
*/
public function doWorkUsingStackAndMagic(&$arg0 = null, &$arg1 = null, &$arg2 = null)
{
$stack = debug_backtrace(0);
return call_user_func_array($this->callback, $stack[0]['args']);
}
/**
* Executes the callback using ReflectionFunction's invokeArgs method and debug_backtrace.
*
* This method should succeed in passing arguments by-ref if they're within the first three
* parameters. By-val should work on any parameter.
*/
public function doWorkUsingReflectionStackAndMagic(&$arg0 = null, &$arg1 = null, &$arg2 = null)
{
$stack = debug_backtrace(0);
$reflection = static::getReflection($this->callback);
return ($reflection instanceof ReflectionFunction ? $reflection->invokeArgs($stack[0]['args']) : $reflection->invokeArgs($this->callback[0], $stack[0]['args']));
}
}
/**
* The classloader we use to generate our dynamic classes.
*/
$result = spl_autoload_register(function($class) {
// Check if the class matches our expression. This example only has one dynamic function, so our
// expression is setup accordingly. More complex classes will require something even crazier.
//
// Grouping:
// 0 - Entire match
// 1 - Namespace
// 2 - Unqualified class name
// 3 - The base class name (ByRefPassthrough, in this case)
// 4 - Return type (reference or value)
// 5 - Encoded parameter list
if (preg_match('/\\A(Cericlabs\\\\Misc)\\\\((ByRefPassthrough)_(R|V)([A-Za-z0-9+\\/]*))\\z/', $class, $matches)) {
// First, pull the param list out of the classname and decode it.
$plist = pack("H*", $matches[5]);
// Next, we build our dynamic subclass using the param list.
// Note:
// This currently does not support passing through values returned by reference. I may add it
// as time permits; but if you're impatient, a solution that can work involves rebuilding the
// parameter list and calling eval from within the to-be-eval'd string below.
$subclass = sprintf(
'namespace %s {
class %s extends %s {
public function %s__invoke(%s) {
$stack = debug_backtrace(0);
$reflection = static::getReflection($this->callback);
$result = ($reflection instanceof \\ReflectionFunction ? $reflection->invokeArgs($stack[0][\'args\']) : $reflection->invokeArgs($this->callback[0], $stack[0][\'args\']));
return $result;
}
}
}',
$matches[1], $matches[2], $matches[3], ($matches[4] === 'R' ? '&' : ''), $plist
);
// Eval!
eval($subclass);
// At this point, the subclass should be defined properly and should be able to act as a
// transparent passthrough.
}
}, true, true);