ArrayHash.php
2 years ago
ArrayList.php
2 years ago
Arrays.php
2 years ago
Callback.php
2 years ago
DateTime.php
2 years ago
FileSystem.php
2 years ago
Floats.php
2 years ago
Helpers.php
2 years ago
Html.php
2 years ago
Image.php
2 years ago
Json.php
2 years ago
ObjectHelpers.php
2 years ago
ObjectMixin.php
2 years ago
Paginator.php
2 years ago
Random.php
2 years ago
Reflection.php
2 years ago
Strings.php
2 years ago
Type.php
2 years ago
Validators.php
2 years ago
exceptions.php
2 years ago
FileSystem.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Nette Framework (https://nette.org) |
| 5 | * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | */ |
| 7 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Utils; |
| 9 | |
| 10 | use FapiMember\Library\Nette; |
| 11 | /** |
| 12 | * File system tool. |
| 13 | */ |
| 14 | final class FileSystem |
| 15 | { |
| 16 | use Nette\StaticClass; |
| 17 | /** |
| 18 | * Creates a directory if it does not exist, including parent directories. |
| 19 | * @throws Nette\IOException on error occurred |
| 20 | */ |
| 21 | public static function createDir(string $dir, int $mode = 0777): void |
| 22 | { |
| 23 | if (!is_dir($dir) && !@mkdir($dir, $mode, \true) && !is_dir($dir)) { |
| 24 | // @ - dir may already exist |
| 25 | throw new Nette\IOException(sprintf("Unable to create directory '%s' with mode %s. %s", self::normalizePath($dir), decoct($mode), Helpers::getLastError())); |
| 26 | } |
| 27 | } |
| 28 | /** |
| 29 | * Copies a file or an entire directory. Overwrites existing files and directories by default. |
| 30 | * @throws Nette\IOException on error occurred |
| 31 | * @throws Nette\InvalidStateException if $overwrite is set to false and destination already exists |
| 32 | */ |
| 33 | public static function copy(string $origin, string $target, bool $overwrite = \true): void |
| 34 | { |
| 35 | if (stream_is_local($origin) && !file_exists($origin)) { |
| 36 | throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($origin))); |
| 37 | } elseif (!$overwrite && file_exists($target)) { |
| 38 | throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", self::normalizePath($target))); |
| 39 | } elseif (is_dir($origin)) { |
| 40 | static::createDir($target); |
| 41 | foreach (new \FilesystemIterator($target) as $item) { |
| 42 | static::delete($item->getPathname()); |
| 43 | } |
| 44 | foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($origin, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) { |
| 45 | if ($item->isDir()) { |
| 46 | static::createDir($target . '/' . $iterator->getSubPathName()); |
| 47 | } else { |
| 48 | static::copy($item->getPathname(), $target . '/' . $iterator->getSubPathName()); |
| 49 | } |
| 50 | } |
| 51 | } else { |
| 52 | static::createDir(dirname($target)); |
| 53 | if (($s = @fopen($origin, 'rb')) && ($d = @fopen($target, 'wb')) && @stream_copy_to_stream($s, $d) === \false) { |
| 54 | // @ is escalated to exception |
| 55 | throw new Nette\IOException(sprintf("Unable to copy file '%s' to '%s'. %s", self::normalizePath($origin), self::normalizePath($target), Helpers::getLastError())); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | /** |
| 60 | * Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first. |
| 61 | * @throws Nette\IOException on error occurred |
| 62 | */ |
| 63 | public static function delete(string $path): void |
| 64 | { |
| 65 | if (is_file($path) || is_link($path)) { |
| 66 | $func = (\DIRECTORY_SEPARATOR === '\\' && is_dir($path)) ? 'rmdir' : 'unlink'; |
| 67 | if (!@$func($path)) { |
| 68 | // @ is escalated to exception |
| 69 | throw new Nette\IOException(sprintf("Unable to delete '%s'. %s", self::normalizePath($path), Helpers::getLastError())); |
| 70 | } |
| 71 | } elseif (is_dir($path)) { |
| 72 | foreach (new \FilesystemIterator($path) as $item) { |
| 73 | static::delete($item->getPathname()); |
| 74 | } |
| 75 | if (!@rmdir($path)) { |
| 76 | // @ is escalated to exception |
| 77 | throw new Nette\IOException(sprintf("Unable to delete directory '%s'. %s", self::normalizePath($path), Helpers::getLastError())); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | /** |
| 82 | * Renames or moves a file or a directory. Overwrites existing files and directories by default. |
| 83 | * @throws Nette\IOException on error occurred |
| 84 | * @throws Nette\InvalidStateException if $overwrite is set to false and destination already exists |
| 85 | */ |
| 86 | public static function rename(string $origin, string $target, bool $overwrite = \true): void |
| 87 | { |
| 88 | if (!$overwrite && file_exists($target)) { |
| 89 | throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", self::normalizePath($target))); |
| 90 | } elseif (!file_exists($origin)) { |
| 91 | throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($origin))); |
| 92 | } else { |
| 93 | static::createDir(dirname($target)); |
| 94 | if (realpath($origin) !== realpath($target)) { |
| 95 | static::delete($target); |
| 96 | } |
| 97 | if (!@rename($origin, $target)) { |
| 98 | // @ is escalated to exception |
| 99 | throw new Nette\IOException(sprintf("Unable to rename file or directory '%s' to '%s'. %s", self::normalizePath($origin), self::normalizePath($target), Helpers::getLastError())); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | /** |
| 104 | * Reads the content of a file. |
| 105 | * @throws Nette\IOException on error occurred |
| 106 | */ |
| 107 | public static function read(string $file): string |
| 108 | { |
| 109 | $content = @file_get_contents($file); |
| 110 | // @ is escalated to exception |
| 111 | if ($content === \false) { |
| 112 | throw new Nette\IOException(sprintf("Unable to read file '%s'. %s", self::normalizePath($file), Helpers::getLastError())); |
| 113 | } |
| 114 | return $content; |
| 115 | } |
| 116 | /** |
| 117 | * Writes the string to a file. |
| 118 | * @throws Nette\IOException on error occurred |
| 119 | */ |
| 120 | public static function write(string $file, string $content, ?int $mode = 0666): void |
| 121 | { |
| 122 | static::createDir(dirname($file)); |
| 123 | if (@file_put_contents($file, $content) === \false) { |
| 124 | // @ is escalated to exception |
| 125 | throw new Nette\IOException(sprintf("Unable to write file '%s'. %s", self::normalizePath($file), Helpers::getLastError())); |
| 126 | } |
| 127 | if ($mode !== null && !@chmod($file, $mode)) { |
| 128 | // @ is escalated to exception |
| 129 | throw new Nette\IOException(sprintf("Unable to chmod file '%s' to mode %s. %s", self::normalizePath($file), decoct($mode), Helpers::getLastError())); |
| 130 | } |
| 131 | } |
| 132 | /** |
| 133 | * Sets file permissions to `$fileMode` or directory permissions to `$dirMode`. |
| 134 | * Recursively traverses and sets permissions on the entire contents of the directory as well. |
| 135 | * @throws Nette\IOException on error occurred |
| 136 | */ |
| 137 | public static function makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666): void |
| 138 | { |
| 139 | if (is_file($path)) { |
| 140 | if (!@chmod($path, $fileMode)) { |
| 141 | // @ is escalated to exception |
| 142 | throw new Nette\IOException(sprintf("Unable to chmod file '%s' to mode %s. %s", self::normalizePath($path), decoct($fileMode), Helpers::getLastError())); |
| 143 | } |
| 144 | } elseif (is_dir($path)) { |
| 145 | foreach (new \FilesystemIterator($path) as $item) { |
| 146 | static::makeWritable($item->getPathname(), $dirMode, $fileMode); |
| 147 | } |
| 148 | if (!@chmod($path, $dirMode)) { |
| 149 | // @ is escalated to exception |
| 150 | throw new Nette\IOException(sprintf("Unable to chmod directory '%s' to mode %s. %s", self::normalizePath($path), decoct($dirMode), Helpers::getLastError())); |
| 151 | } |
| 152 | } else { |
| 153 | throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($path))); |
| 154 | } |
| 155 | } |
| 156 | /** |
| 157 | * Determines if the path is absolute. |
| 158 | */ |
| 159 | public static function isAbsolute(string $path): bool |
| 160 | { |
| 161 | return (bool) preg_match('#([a-z]:)?[/\\\\]|[a-z][a-z0-9+.-]*://#Ai', $path); |
| 162 | } |
| 163 | /** |
| 164 | * Normalizes `..` and `.` and directory separators in path. |
| 165 | */ |
| 166 | public static function normalizePath(string $path): string |
| 167 | { |
| 168 | $parts = ($path === '') ? [] : preg_split('~[/\\\\]+~', $path); |
| 169 | $res = []; |
| 170 | foreach ($parts as $part) { |
| 171 | if ($part === '..' && $res && end($res) !== '..' && end($res) !== '') { |
| 172 | array_pop($res); |
| 173 | } elseif ($part !== '.') { |
| 174 | $res[] = $part; |
| 175 | } |
| 176 | } |
| 177 | return ($res === ['']) ? \DIRECTORY_SEPARATOR : implode(\DIRECTORY_SEPARATOR, $res); |
| 178 | } |
| 179 | /** |
| 180 | * Joins all segments of the path and normalizes the result. |
| 181 | */ |
| 182 | public static function joinPaths(string ...$paths): string |
| 183 | { |
| 184 | return self::normalizePath(implode('/', $paths)); |
| 185 | } |
| 186 | } |
| 187 |