Kohana : Directory Helper
17 february 2010
/**
 * Directory helper class.
 *
 * @package    Kohana
 * @author     Kohana Team
 * @copyright  (c) 2007-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Kohana_Directory {

	/**
	 * This function will recursively delete all files in the given path, without
	 * following symlinks.
	 *
	 * @param   string  path
	 */
	public static function remove_directory($path)
	{
		try
		{
			// Constructs a new directory iterator from a path
			$dir = new DirectoryIterator($path);

			foreach ($dir as $fileinfo)
			{
				// Determine if current DirectoryIterator item is a regular file or symbolic link
				if ($fileinfo->isFile() || $fileinfo->isLink())
				{
					// Changes file mode
					if (substr(decoct(fileperms($fileinfo->getPathName())), 1) != 0777)
						chmod($fileinfo->getPathName(), 0777);

					// Deletes a file
					unlink($fileinfo->getPathName());
				}

				// Determine if current DirectoryIterator item is not '.' or '..' and is a directory
				elseif ( ! $fileinfo->isDot() && $fileinfo->isDir())
				{
					// Recursion
					self::remove_directory($fileinfo->getPathName());
				}
			}

			// Changes file mode
			if (substr(decoct(fileperms($path)), 1) != 0777)
				chmod($path, 0777);

			// Removes directory
			rmdir($path);
		}

		catch (Exception $e)
		{
			throw new Kohana_Exception('Could not remove :path directory',
				array(':path' => Kohana::debug_path($path)));
		}
	}

	final private function __construct()
	{
		// This is a static class
	}

} // End file