public static function populate_callback(array $callback)
{
if (is_string($callback['function']) and false !== strpos($callback['function'], '::')) {
$callback['function'] = explode('::', $callback['function']);
}
try {
if (is_array($callback['function'])) {
if (is_object($callback['function'][0])) {
$class = get_class($callback['function'][0]);
$access = '->';
} else {
$class = $callback['function'][0];
$access = '::';
}
$callback['name'] = $class . $access . $callback['function'][1] . '()';
$ref = new ReflectionMethod($class, $callback['function'][1]);
} else {
if (is_object($callback['function'])) {
if (is_a($callback['function'], 'Closure')) {
$ref = new ReflectionFunction($callback['function']);
$file = QM_Util::standard_dir($ref->getFileName(), '');
$callback['name'] = sprintf(__('Closure on line %1$d of %2$s', 'query-monitor'), $ref->getStartLine(), $file);
} else {
// the object should have a __invoke() method
$class = get_class($callback['function']);
$callback['name'] = $class . '->__invoke()';
$ref = new ReflectionMethod($class, '__invoke');
}
} else {
$callback['name'] = $callback['function'] . '()';
$ref = new ReflectionFunction($callback['function']);
}
}
$callback['file'] = $ref->getFileName();
$callback['line'] = $ref->getStartLine();
// https://github.com/facebook/hhvm/issues/5856
$name = trim($ref->getName());
if ('__lambda_func' === $name || 0 === strpos($name, 'lambda_')) {
if (preg_match('|(?P<file>.*)\\((?P<line>[0-9]+)\\)|', $callback['file'], $matches)) {
$callback['file'] = $matches['file'];
$callback['line'] = $matches['line'];
$file = trim(QM_Util::standard_dir($callback['file'], ''), '/');
$callback['name'] = sprintf(__('Anonymous function on line %1$d of %2$s', 'query-monitor'), $callback['line'], $file);
} else {
// https://github.com/facebook/hhvm/issues/5807
unset($callback['line'], $callback['file']);
$callback['name'] = $name . '()';
$callback['error'] = new WP_Error('unknown_lambda', __('Unable to determine source of lambda function', 'query-monitor'));
}
}
if (!empty($callback['file'])) {
$callback['component'] = self::get_file_component($callback['file']);
}
} catch (ReflectionException $e) {
$callback['error'] = new WP_Error('reflection_exception', $e->getMessage());
}
return $callback;
}