1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class FileSystem
17: {
18:
19: 20: 21: 22: 23:
24: public static function createDir($dir, $mode = 0777)
25: {
26: if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE) && !is_dir($dir)) {
27: $error = error_get_last();
28: throw new Nette\IOException("Unable to create directory '$dir'. $error[message]");
29: }
30: }
31:
32:
33: 34: 35: 36: 37:
38: public static function copy($source, $dest, $overwrite = TRUE)
39: {
40: if (stream_is_local($source) && !file_exists($source)) {
41: throw new Nette\IOException("File or directory '$source' not found.");
42:
43: } elseif (!$overwrite && file_exists($dest)) {
44: throw new Nette\InvalidStateException("File or directory '$dest' already exists.");
45:
46: } elseif (is_dir($source)) {
47: static::createDir($dest);
48: foreach (new \FilesystemIterator($dest) as $item) {
49: static::delete($item);
50: }
51: foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
52: if ($item->isDir()) {
53: static::createDir($dest . '/' . $iterator->getSubPathName());
54: } else {
55: static::copy($item, $dest . '/' . $iterator->getSubPathName());
56: }
57: }
58:
59: } else {
60: static::createDir(dirname($dest));
61: if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === FALSE) {
62: throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
63: }
64: }
65: }
66:
67:
68: 69: 70: 71: 72:
73: public static function delete($path)
74: {
75: if (is_file($path) || is_link($path)) {
76: $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
77: if (!@$func($path)) {
78: throw new Nette\IOException("Unable to delete '$path'.");
79: }
80:
81: } elseif (is_dir($path)) {
82: foreach (new \FilesystemIterator($path) as $item) {
83: static::delete($item);
84: }
85: if (!@rmdir($path)) {
86: throw new Nette\IOException("Unable to delete directory '$path'.");
87: }
88: }
89: }
90:
91:
92: 93: 94: 95: 96: 97:
98: public static function rename($name, $newName, $overwrite = TRUE)
99: {
100: if (!$overwrite && file_exists($newName)) {
101: throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
102:
103: } elseif (!file_exists($name)) {
104: throw new Nette\IOException("File or directory '$name' not found.");
105:
106: } else {
107: static::createDir(dirname($newName));
108: static::delete($newName);
109: if (!@rename($name, $newName)) {
110: throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
111: }
112: }
113: }
114:
115:
116: 117: 118: 119: 120:
121: public static function read($file)
122: {
123: $content = @file_get_contents($file);
124: if ($content === FALSE) {
125: throw new Nette\IOException("Unable to read file '$file'.");
126: }
127: return $content;
128: }
129:
130:
131: 132: 133: 134: 135:
136: public static function write($file, $content, $mode = 0666)
137: {
138: static::createDir(dirname($file));
139: if (@file_put_contents($file, $content) === FALSE) {
140: throw new Nette\IOException("Unable to write file '$file'.");
141: }
142: if ($mode !== NULL && !@chmod($file, $mode)) {
143: throw new Nette\IOException("Unable to chmod file '$file'.");
144: }
145: }
146:
147:
148: 149: 150: 151:
152: public static function isAbsolute($path)
153: {
154: return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
155: }
156:
157: }
158: