public static function detect($index = 0)
{
$context = new Context();
$backtrace = debug_backtrace();
$position = $backtrace[$index];
if (isset($position['file'])) {
$context->filename = $position['file'];
}
if (isset($position['line'])) {
$context->line = $position['line'];
}
$caller = isset($backtrace[$index + 1]) ? $backtrace[$index + 1] : null;
if (isset($caller['function'])) {
$context->method = $caller['function'];
if (isset($caller['type']) && $caller['type'] === '::') {
$context->static = true;
}
}
if (isset($caller['class'])) {
$fqn = explode('\\', $caller['class']);
$context->class = array_pop($fqn);
if (count($fqn)) {
$context->namespace = implode('\\', $fqn);
}
}
// @todo extract namespaces and use statements
return $context;
}