Inject code in a PHP closure
I have an already defined closure and I want to inject code inside when I execute it. Here is an example:
$predefined = "print 'my predefined injected code<br />';";
$closure = function () {
print 'hello<br />';
};
call_user_func_array($closure, array());
// output : hello
I want to mix 2 codes : a predefined one and the closure's one. After modification, I want my closure to look like this
$closure = function () {
print 'my predefined injected code<br />';
print 'hello<br />';
};
Is it possible to insert some code in the closure before executing it ?
Note: I can not use "create_function" that take the code as a string, so can be modified easily. The closures are already defined and are defined in a certain way (through a function that take a callback arg, not a string arg).
Thanks for your help.
EDIT:
Here is the solution
function hackClosure($closure, $inject_code)
{
$reflection = new ReflectionFunction($closure);
$tmp = $reflection->getParameters();
$args = array();
foreach ($tmp as $a) array_push($args, '$'.$a->getName() . ($a->isDefaultValueAvailable() ? '=\''.$a->getDefaultValue().'\'' : ''));
$file = new SplFileObject($reflection->getFileName());
$file->seek($reflection->getStartLine()-1);
$code = '';
while ($file->key() < $reflection->getEndLine())
{
$code .= $file->current();
$file->next();
}
$start = strpos($code开发者_运维问答, '{')+1;
$end = strrpos($code, '}');
return create_function(implode(', ', $args), substr($code, $start, $end - $start) . $inject_code);
}
$theClosure = function () { print 'something'; };
$inject_code = "print ' to say';";
$func = hackClosure($theClosure, $inject_code);
$func();
It renders
something to say
instead of
something
You can't directly inject it. However, you can wrap it.
$newClosure = function() use ($closure) {
print 'my predefined injected code<br />';
$closure();
};
Also, there's no need to use call_user_func_array
since you're not passing any arguments. Just call $closure();
Also, you could build a wrapper to get a new closure:
$creator = function($closure) {
return function() use ($closure) {
print 'my predefined injected code<br />';
$closure();
};
};
$newClosure = $creator($closure);
$newClosure();
perhaps something like this:
$closure = function($optionalcode = null) {
print('blah blah blah');
if (!isnull($optionalcode)) { eval($optionalcode); }
print('blah blah blah');
}
$closure("print('yoohoo!');");
However, eval()
is evil and should be avoided at all costs.
Is't this what you are looking for ? http://php.net/manual/en/function.eval.php
精彩评论