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