From b5de50fab90684c76355f92aa88dfdef873b149d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 22 Aug 2011 22:49:44 -0500 Subject: [PATCH] refactoring the file class. --- laravel/file.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/laravel/file.php b/laravel/file.php index b40fdbde..c3a9d630 100644 --- a/laravel/file.php +++ b/laravel/file.php @@ -51,6 +51,13 @@ public static function extension($path) /** * Get the lines surrounding a given line in a file. * + * The amount of padding with which to surround the line may also be specified. + * + * + * // Get lines 10 - 20 of the "routes.php" file + * $lines = File::snapshot(APP_PATH.'routes'.EXT, 15, 5); + * + * * @param string $path * @param int $line * @param int $padding @@ -64,11 +71,9 @@ public static function snapshot($path, $line, $padding = 5) array_unshift($file, ''); - if (($start = $line - $padding) < 0) $start = 0; + $length = ($line - ($start = $line - $padding)) + $padding + 1; - if (($length = ($line - $start) + $padding + 1) < 0) $length = 0; - - return array_slice($file, $start, $length, true); + return array_slice($file, ($start > 0) ? $start : 0, ($length > 0) ? $length : 0, true); } /** @@ -89,12 +94,9 @@ public static function mime($extension, $default = 'application/octet-stream') { $mimes = Config::get('mimes'); - if (array_key_exists($extension, $mimes)) - { - return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; - } + if ( ! array_key_exists($extension, $mimes)) return $default; - return $default; + return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; } /**