Added file context to error class.

This commit is contained in:
Taylor Otwell 2011-07-11 08:38:49 -07:00
parent f6ff5370ef
commit 54311a41ce
1 changed files with 34 additions and 1 deletions

View File

@ -86,7 +86,8 @@ private static function show($e, $severity, $message, $file)
->bind('message', $message)
->bind('file', $file)
->bind('line', $e->getLine())
->bind('trace', $e->getTraceAsString());
->bind('trace', $e->getTraceAsString())
->bind('contexts', static::context($file, $e->getLine()));
Response::make($view, 500)->send();
}
@ -96,4 +97,36 @@ private static function show($e, $severity, $message, $file)
}
}
/**
* Get the code surrounding a given line in a file.
*
* @param string $path
* @param int $line
* @param int $padding
* @return string
*/
private static function context($path, $line, $padding = 5)
{
if (file_exists($path))
{
$file = file($path, FILE_IGNORE_NEW_LINES);
array_unshift($file, '');
if (($start = $line - $padding) < 0)
{
$start = 0;
}
if (($length = ($line - $start) + $padding + 1) < 0)
{
$length = 0;
}
return array_slice($file, $start, $length, true);
}
return array();
}
}