* // 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 * @return array */ public function snapshot($path, $line, $padding = 5) { if ( ! file_exists($path)) return array(); $file = file($path, FILE_IGNORE_NEW_LINES); array_unshift($file, ''); $start = $line - $padding; $length = ($line - $start) + $padding + 1; return array_slice($file, ($start > 0) ? $start : 0, ($length > 0) ? $length : 0, true); } /** * Get a file MIME type by extension. * * Any extension in the MIMEs configuration file may be passed to the method. * * * // Returns "application/x-tar" * $mime = $file->mime('tar'); * * * @param string $extension * @param string $default * @return string */ public function mime($extension, $default = 'application/octet-stream') { $mimes = Config::get('mimes'); if ( ! array_key_exists($extension, $mimes)) return $default; return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; } /** * Determine if a file is a given type. * * The Fileinfo PHP extension will be used to determine the MIME type of the file. Any extension * in the MIMEs configuration file may be passed as a type. * * * // Determine if the file is a JPG image * $image = $file->is('jpg', 'path/to/image.jpg'); * * * @param string $extension * @param string $path * @return bool */ public function is($extension, $path) { $mimes = Config::get('mimes'); if ( ! array_key_exists($extension, $mimes)) { throw new \Exception("File extension [$extension] is unknown. Cannot determine file type."); } $mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path); return (is_array($mimes[$extension])) ? in_array($mime, $mimes[$extension]) : $mime === $mimes[$extension]; } }