<?php namespace Laravel; use Closure, FilesystemIterator as fIterator;

class File {

	/**
	 * Determine if a file exists.
	 * 判断文件是否存在。
	 * @param  string  $path
	 * @return bool
	 */
	public static function exists($path)
	{
		return file_exists($path);
	}

	/**
	 * Get the contents of a file.
	 * 获取文件的内容。
	 * <code>
	 *		// Get the contents of a file
	 *		$contents = File::get(path('app').'routes'.EXT);
	 *
	 *		// Get the contents of a file or return a default value if it doesn't exist
	 *		$contents = File::get(path('app').'routes'.EXT, 'Default Value');
	 * </code>
	 *
	 * @param  string  $path
	 * @param  mixed   $default
	 * @return string
	 */
	public static function get($path, $default = null)
	{
		return (file_exists($path)) ? file_get_contents($path) : value($default);
	}

	/**
	 * Write to a file.
	 * 写入文件。
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
	public static function put($path, $data)
	{
	    // 将一个字符串写入文件
		return file_put_contents($path, $data, LOCK_EX);
	}

	/**
	 * Append to a file.
	 * 追加到文件
	 * @param  string  $path
	 * @param  string  $data
	 * @return int
	 */
	public static function append($path, $data)
	{
		return file_put_contents($path, $data, LOCK_EX | FILE_APPEND);
	}

	/**
	 * Delete a file.
	 * 删除文件
	 * @param  string  $path
	 * @return void
	 */
	public static function delete($path)
	{
	    // unlink-删除文件
		if (static::exists($path)) @unlink($path);
	}

	/**
	 * Extract the file extension from a file path.
	 *  从文件路径中提取文件扩展名。
	 * @param  string  $path
	 * @return string
	 */
	public static function extension($path)
	{
	    // pathinfo- 获取文件路径信息
		return pathinfo($path, PATHINFO_EXTENSION);
	}

	/**
	 * Get the file type of a given file.
	 * 获取给定文件的文件类型
	 * @param  string  $path
	 * @return string
	 */
	public static function type($path)
	{
	    // filetype-获取文件类型
		return filetype($path);
	}

	/**
	 * Get the file size of a given file.
	 * 获取给定文件的大小
	 * @param  string  $path
	 * @return int
	 */
	public static function size($path)
	{
	    // filesize-获取文件大小
		return filesize($path);
	}

	/**
	 * Get the file's last modification time.
	 *
	 * @param  string  $path
	 * @return int
	 */
	public static function modified($path)
	{
	    // filemtime-获取文件修改时间
		return filemtime($path);
	}

	/**
	 * Get a file MIME type by extension.
	 * 按扩展名获取文件MIME类型。
	 * <code>
	 *		// Determine the MIME type for the .tar extension
	 *		$mime = File::mime('tar');
	 *
	 *		// Return a default value if the MIME can't be determined
	 *		$mime = File::mime('ext', 'application/octet-stream');
	 * </code>
	 *
	 * @param  string  $extension
	 * @param  string  $default
	 * @return string
	 */
	public static 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 is used to determine the file's MIME type.
	 * Fileinfo PHP 扩展名用于确定文件的 MIME 类型。
	 * <code>
	 *		// Determine if a file is a JPG image
	 *		$jpg = File::is('jpg', 'path/to/file.jpg');
	 *
	 *		// Determine if a file is one of a given list of types
	 *		$image = File::is(array('jpg', 'png', 'gif'), 'path/to/file');
	 * </code>
	 *
	 * @param  array|string  $extensions
	 * @param  string        $path
	 * @return bool
	 */
	public static function is($extensions, $path)
	{
		$mimes = Config::get('mimes');
        // finfo_file 返回一个文件的信息 finfo_open-创建一个fileinfo资源
		$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), $path);

		// The MIME configuration file contains an array of file extensions and
		// their associated MIME types. We will spin through each extension the
		// developer wants to check and look for the MIME type.
        // MIME配置文件包含文件扩展名及其关联的MIME类型的数组。
        // 我们将遍历开发人员想要检查的每个扩展并查找 MIME 类型。
		foreach ((array) $extensions as $extension)
		{
			if (isset($mimes[$extension]) and in_array($mime, (array) $mimes[$extension]))
			{
				return true;
			}
		}

		return false;
	}

	/**
	 * Move a directory from one location to another.
	 * 将目录从一个位置移动到另一个位置。
	 * @param  string  $source
	 * @param  string  $destination
	 * @param  int     $options
	 * @return void
	 */
	public static function mvdir($source, $destination, $options = fIterator::SKIP_DOTS)
	{
		static::cpdir($source, $destination, true, $options);
	}

	/**
	 * Recursively copy directory contents to another directory.
	 * 将目录内容递归复制到另一个目录。
	 * @param  string  $source
	 * @param  string  $destination
	 * @param  bool    $delete
	 * @param  int     $options
	 * @return void
	 */
	public static function cpdir($source, $destination, $delete = false, $options = fIterator::SKIP_DOTS)
	{
		if ( ! is_dir($source)) return;

		// First we need to create the destination directory if it doesn't
		// already exists. This directory hosts all of the assets we copy
		// from the installed bundle's source directory.
        // 首先,如果目标目录尚不存在,则需要创建它。 该目录包含我们从已安装包的源目录中复制的所有资源。
        // 判断是否为目录
		if ( ! is_dir($destination))
		{
		    // mkdir 创建目录
			mkdir($destination, 0777, true);
		}

		$items = new fIterator($source, $options);

		foreach ($items as $item)
		{
			$location = $destination.DS.$item->getBasename();

			// If the file system item is a directory, we will recurse the
			// function, passing in the item directory. To get the proper
			// destination path, we'll add the basename of the source to
			// to the destination directory.
            // 如果文件系统项是目录,我们将递归函数,传入项目录。
            // 为了获得正确的目标路径,我们将源的基本名称添加到目标目录。
			if ($item->isDir())
			{
				$path = $item->getRealPath();

				static::cpdir($path, $location, $delete, $options);
                // rmdir-删除目录
				if ($delete) @rmdir($item->getRealPath());
			}
			// If the file system item is an actual file, we can copy the
			// file from the bundle asset directory to the public asset
			// directory. The "copy" method will overwrite any existing
			// files with the same name.
            // 如果文件系统项是实际文件,我们可以将文件从捆绑资产目录复制到公共资产目录。
            // “复制”方法将覆盖任何具有相同名称的现有文件。
			else
			{
                // copy-拷贝文件
				copy($item->getRealPath(), $location);
                // unlink-删除文件
				if ($delete) @unlink($item->getRealPath());
			}
		}
        // rmdir-删除目录
		if ($delete) rmdir($source);
	}

	/**
	 * Recursively delete a directory.
	 * 递归删除目录。
	 * @param  string  $directory
	 * @return void
	 */
	public static function rmdir($directory)
	{
		if ( ! is_dir($directory)) return;

		$items = new fIterator($directory);

		foreach ($items as $item)
		{
			// If the item is a directory, we can just recurse into the
			// function and delete that sub-directory, otherwise we'll
			// just deleete the file and keep going!
			if ($item->isDir())
			{
				static::rmdir($item->getRealPath());
			}
			else
			{
				@unlink($item->getRealPath());
			}
		}

		@rmdir($directory);
	}

	/**
	 * Get the most recently modified file in a directory.
	 * 获取目录中最近修改的文件。
	 * @param  string       $directory
	 * @param  int          $options
	 * @return SplFileInfo
	 */
	public static function latest($directory, $options = fIterator::SKIP_DOTS)
	{
		$time = 0;

		$items = new fIterator($directory, $options);

		// To get the latest created file, we'll simply spin through the
		// directory, setting the latest file if we encounter a file
		// with a UNIX timestamp greater than the latest one we
		// have encountered thus far in the loop.
        // 为了获得最新创建的文件,我们将简单地遍历目录,
        // 如果我们遇到一个文件的 UNIX 时间戳大于我们迄今为止在循环中遇到的最新文件,则设置最新文件。
		foreach ($items as $item)
		{
			if ($item->getMTime() > $time) $latest = $item;
		}

		return $latest;
	}

}

github地址: https://github.com/liu-shilong/laravel3-scr