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: public static function createDir($dir, $mode = 0777)
26: {
27: if (!is_dir($dir) && !@mkdir($dir, $mode, TRUE)) {
28: throw new Nette\IOException("Unable to create directory '$dir'.");
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: public static function delete($path)
72: {
73: if (is_file($path) || is_link($path)) {
74: $func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
75: if (!@$func($path)) {
76: throw new Nette\IOException("Unable to delete '$path'.");
77: }
78:
79: } elseif (is_dir($path)) {
80: foreach (new \FilesystemIterator($path) as $item) {
81: static::delete($item);
82: }
83: if (!@rmdir($path)) {
84: throw new Nette\IOException("Unable to delete directory '$path'.");
85: }
86: }
87: }
88:
89:
90: 91: 92: 93:
94: public static function rename($name, $newName, $overwrite = TRUE)
95: {
96: if (!$overwrite && file_exists($newName)) {
97: throw new Nette\InvalidStateException("File or directory '$newName' already exists.");
98:
99: } elseif (!file_exists($name)) {
100: throw new Nette\IOException("File or directory '$name' not found.");
101:
102: } else {
103: static::createDir(dirname($newName));
104: static::delete($newName);
105: if (!@rename($name, $newName)) {
106: throw new Nette\IOException("Unable to rename file or directory '$name' to '$newName'.");
107: }
108: }
109: }
110:
111:
112: 113: 114: 115:
116: public static function write($file, $content, $mode = 0666)
117: {
118: static::createDir(dirname($file));
119: if (@file_put_contents($file, $content) === FALSE) {
120: throw new Nette\IOException("Unable to write file '$file'.");
121: }
122: if ($mode !== NULL && !@chmod($file, $mode)) {
123: throw new Nette\IOException("Unable to chmod file '$file'.");
124: }
125: }
126:
127:
128: 129: 130: 131:
132: public static function isAbsolute($path)
133: {
134: return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path);
135: }
136:
137: }
138: