00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 final class FileUtils extends StaticFactory
00019 {
00024 public static function convertLineEndings(
00025 $dir, $ignore, $from = "\r\n", $to = "\n"
00026 )
00027 {
00028 $converted = 0;
00029
00030 if (!is_dir($dir) || !is_readable($dir)) {
00031 throw new WrongArgumentException();
00032 }
00033
00034 $files = scandir($dir);
00035
00036 foreach ($files as $file) {
00037 if (
00038 '.' != $file
00039 && '..' != $file
00040 &&
00041 !in_array(
00042 substr($file, strrpos($file, '.')), $ignore, true
00043 )
00044 ) {
00045 if (is_dir($path = $dir . DIRECTORY_SEPARATOR . $file)) {
00046 $converted += self::convertLineEndings(
00047 $path, $ignore, $from, $to
00048 );
00049 } else {
00050 file_put_contents(
00051 $path,
00052 preg_replace(
00053 "/$from/",
00054 $to,
00055 file_get_contents($path)
00056 )
00057 );
00058
00059 ++$converted;
00060 }
00061 }
00062 }
00063
00064 return $converted;
00065 }
00066
00067 public static function makeTempFile(
00068 $where = 'file-utils/', $prefix = '', $mkdirMode = 0700
00069 )
00070 {
00071 $directory = ONPHP_TEMP_PATH.$where;
00072
00073 if (!is_writable($directory))
00074 if (!mkdir($directory, $mkdirMode, true))
00075 throw new WrongArgumentException(
00076 "can not write to '{$directory}'"
00077 );
00078
00079 $result = tempnam($directory, $prefix);
00080
00081 if ($result === false)
00082 throw new WrongArgumentException(
00083 'failed to create temp file in '.$directory
00084 );
00085
00086 return $result;
00087 }
00088
00089 public static function makeTempDirectory(
00090 $where = 'file-utils/', $prefix = '', $mode = 0700
00091 )
00092 {
00093 $directory = ONPHP_TEMP_PATH.$where;
00094
00095 if (substr($directory, -1) != DIRECTORY_SEPARATOR)
00096 $directory .= DIRECTORY_SEPARATOR;
00097
00098 $attempts = 42;
00099
00100 do {
00101 --$attempts;
00102 $path = $directory.$prefix.mt_rand();
00103 } while (
00104 !mkdir($path, $mode, true)
00105 && $attempts > 0
00106
00107 && !usleep(100)
00108 );
00109
00110 if ($attempts == 0)
00111 throw new WrongArgumentException(
00112 'failed to create subdirectory in '.$directory
00113 );
00114
00115 return $path;
00116 }
00117
00118 public static function makeUniqueName($fileName)
00119 {
00120 $extensionPosition = strrpos($fileName, '.');
00121
00122 return
00123 substr($fileName, 0, $extensionPosition)
00124 .'_'.uniqid()
00125 .substr($fileName, $extensionPosition);
00126 }
00127
00128 public static function removeDirectory($directory, $recursive = false)
00129 {
00130 if (!$recursive) {
00131 try {
00132 rmdir($directory);
00133 } catch (BaseException $e) {
00134 throw new WrongArgumentException($e->getMessage());
00135 }
00136 } else {
00137 if (!$handle = opendir($directory))
00138 throw new WrongArgumentException(
00139 'cannot read directory '.$directory
00140 );
00141
00142 while (($item = readdir($handle)) !== false) {
00143 if ($item == '.' || $item == '..')
00144 continue;
00145
00146 $path = $directory.DIRECTORY_SEPARATOR.$item;
00147
00148 if (is_dir($path))
00149 self::removeDirectory($path);
00150 elseif (!unlink($path))
00151 throw new WrongStateException(
00152 "cannot unlink {$path}"
00153 );
00154 }
00155
00156 closedir($handle);
00157
00158 try {
00159 rmdir($directory);
00160 } catch (BaseException $e) {
00161 throw new WrongStateException(
00162 "cannot unlink {$directory}, though it should be empty now"
00163 );
00164 }
00165 }
00166 }
00167
00168 public static function upload($source, $target)
00169 {
00170 if (
00171 is_readable($source)
00172 && is_writable(pathinfo($target, PATHINFO_DIRNAME))
00173 )
00174 return move_uploaded_file($source, $target);
00175
00176 throw new WrongArgumentException(
00177 "can not move {$source} to {$target}"
00178 );
00179 }
00180
00181 public static function move($source, $target)
00182 {
00183 if (
00184 is_readable($source)
00185 && is_writable(pathinfo($target, PATHINFO_DIRNAME))
00186 )
00187 return rename($source, $target);
00188
00189 throw new WrongArgumentException(
00190 "can not move {$source} to {$target}"
00191 );
00192 }
00193
00194 public static function unlink($filePath)
00195 {
00196 if (
00197 file_exists($filePath)
00198 && is_writable(pathinfo($filePath, PATHINFO_DIRNAME))
00199 )
00200 return unlink($filePath);
00201
00202 throw new WrongArgumentException(
00203 "can not unlink {$filePath}"
00204 );
00205 }
00206 }
00207 ?>