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)) {
27: throw new Nette\IOException("Unable to create directory '$dir'.");
28: }
29: }
30:
31:
32: 33: 34: 35: 36:
37: public static function copy($source, $dest, $overwrite = TRUE)
38: {
39: if (stream_is_local($source) && !file_exists($source)) {
40: throw new Nette\IOException("File or directory '$source' not found.");
41:
42: } elseif (!$overwrite && file_exists($dest)) {
43: throw new Nette\InvalidStateException("File or directory '$dest' already exists.");
44:
45: } elseif (is_dir($source)) {
46: static::createDir($dest);
47: foreach (new \FilesystemIterator($dest) as $item) {
48: static::delete($item);
49: }
50: foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
51: if ($item->isDir()) {
52: static::createDir($dest . '/' . $iterator->getSubPathName());
53: } else {
54: static::copy($item, $dest . '/' . $iterator->getSubPathName());
55: }
56: }
57:
58: } else {
59: static::createDir(dirname($dest));
60: if (@stream_copy_to_stream(fopen($source, 'r'), fopen($dest, 'w')) === FALSE) {
61: throw new Nette\IOException("Unable to copy file '$source' to '$dest'.");
62: }
63: }
64: }
65:
66:
67: 68: 69: 70: 71:
72: public static function delete($path)
73: {
74: if (is_file($path) || is_link($path)) {
75: $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
76: if (!@$func($path)) {
77: throw new Nette\IOException("Unable to delete '$path'.");
78: }
79:
80: } elseif (is_dir($path)) {
81: foreach (new \FilesystemIterator($path) as $item) {
82: static::delete($item);
83: }
84: if (!@rmdir($path)) {
85: throw new Nette\IOException("Unable to delete directory '$path'.");
86: }
87: }
88: }
89:
90:
91: 92: 93: 94: 95: 96:
97: public static function rename($name, $newName, $overwrite = TRUE)
98: {
99: if (!$overwrite && file_exists($newName)) {
100: throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
101:
102: } elseif (!file_exists($name)) {
103: throw new Nette\IOException("File or directory '$name' not found.");
104:
105: } else {
106: static::createDir(dirname($newName));
107: static::delete($newName);
108: if (!@rename($name, $newName)) {
109: throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
110: }
111: }
112: }
113:
114:
115: 116: 117: 118: 119:
120: public static function write($file, $content, $mode = 0666)
121: {
122: static::createDir(dirname($file));
123: if (@file_put_contents($file, $content) === FALSE) {
124: throw new Nette\IOException("Unable to write file '$file'.");
125: }
126: if ($mode !== NULL && !@chmod($file, $mode)) {
127: throw new Nette\IOException("Unable to chmod file '$file'.");
128: }
129: }
130:
131:
132: 133: 134: 135:
136: public static function isAbsolute($path)
137: {
138: return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
139: }
140:
141: }
142: