Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 18... Zeile 18...
18
final class FileSystem
18
final class FileSystem
19
{
19
{
20
	use Nette\StaticClass;
20
	use Nette\StaticClass;
Zeile 21... Zeile 21...
21
 
21
 
22
	/**
22
	/**
23
	 * Creates a directory if it doesn't exist.
23
	 * Creates a directory if it does not exist, including parent directories.
24
	 * @throws Nette\IOException  on error occurred
24
	 * @throws Nette\IOException  on error occurred
25
	 */
25
	 */
26
	public static function createDir(string $dir, int $mode = 0777): void
26
	public static function createDir(string $dir, int $mode = 0777): void
27
	{
27
	{
28
		if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
28
		if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
29
			throw new Nette\IOException(sprintf(
29
			throw new Nette\IOException(sprintf(
30
				"Unable to create directory '%s' with mode %s. %s",
30
				"Unable to create directory '%s' with mode %s. %s",
31
				self::normalizePath($dir),
31
				self::normalizePath($dir),
32
				decoct($mode),
32
				decoct($mode),
33
				Helpers::getLastError()
33
				Helpers::getLastError(),
34
			));
34
			));
35
		}
35
		}
Zeile 36... Zeile 36...
36
	}
36
	}
37
 
37
 
38
 
38
 
39
	/**
39
	/**
40
	 * Copies a file or a directory. Overwrites existing files and directories by default.
40
	 * Copies a file or an entire directory. Overwrites existing files and directories by default.
41
	 * @throws Nette\IOException  on error occurred
41
	 * @throws Nette\IOException  on error occurred
42
	 * @throws Nette\InvalidStateException  if $overwrite is set to false and destination already exists
42
	 * @throws Nette\InvalidStateException  if $overwrite is set to false and destination already exists
Zeile 62... Zeile 62...
62
					static::copy($item->getPathname(), $target . '/' . $iterator->getSubPathName());
62
					static::copy($item->getPathname(), $target . '/' . $iterator->getSubPathName());
63
				}
63
				}
64
			}
64
			}
65
		} else {
65
		} else {
66
			static::createDir(dirname($target));
66
			static::createDir(dirname($target));
67
			if (
-
 
68
				($s = @fopen($origin, 'rb'))
-
 
69
				&& ($d = @fopen($target, 'wb'))
-
 
70
				&& @stream_copy_to_stream($s, $d) === false
67
			if (@stream_copy_to_stream(static::open($origin, 'rb'), static::open($target, 'wb')) === false) { // @ is escalated to exception
71
			) { // @ is escalated to exception
-
 
72
				throw new Nette\IOException(sprintf(
68
				throw new Nette\IOException(sprintf(
73
					"Unable to copy file '%s' to '%s'. %s",
69
					"Unable to copy file '%s' to '%s'. %s",
74
					self::normalizePath($origin),
70
					self::normalizePath($origin),
75
					self::normalizePath($target),
71
					self::normalizePath($target),
76
					Helpers::getLastError()
72
					Helpers::getLastError(),
77
				));
73
				));
78
			}
74
			}
79
		}
75
		}
80
	}
76
	}
Zeile 81... Zeile 77...
81
 
77
 
82
 
78
 
-
 
79
	/**
-
 
80
	 * Opens file and returns resource.
-
 
81
	 * @return resource
-
 
82
	 * @throws Nette\IOException  on error occurred
-
 
83
	 */
-
 
84
	public static function open(string $path, string $mode)
-
 
85
	{
-
 
86
		$f = @fopen($path, $mode); // @ is escalated to exception
-
 
87
		if (!$f) {
-
 
88
			throw new Nette\IOException(sprintf(
-
 
89
				"Unable to open file '%s'. %s",
-
 
90
				self::normalizePath($path),
-
 
91
				Helpers::getLastError(),
-
 
92
			));
-
 
93
		}
-
 
94
		return $f;
-
 
95
	}
-
 
96
 
-
 
97
 
83
	/**
98
	/**
84
	 * Deletes a file or directory if exists.
99
	 * Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first.
85
	 * @throws Nette\IOException  on error occurred
100
	 * @throws Nette\IOException  on error occurred
86
	 */
101
	 */
87
	public static function delete(string $path): void
102
	public static function delete(string $path): void
88
	{
103
	{
89
		if (is_file($path) || is_link($path)) {
104
		if (is_file($path) || is_link($path)) {
90
			$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
105
			$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
91
			if (!@$func($path)) { // @ is escalated to exception
106
			if (!@$func($path)) { // @ is escalated to exception
92
				throw new Nette\IOException(sprintf(
107
				throw new Nette\IOException(sprintf(
93
					"Unable to delete '%s'. %s",
108
					"Unable to delete '%s'. %s",
94
					self::normalizePath($path),
109
					self::normalizePath($path),
95
					Helpers::getLastError()
110
					Helpers::getLastError(),
96
				));
111
				));
97
			}
112
			}
98
		} elseif (is_dir($path)) {
113
		} elseif (is_dir($path)) {
Zeile 102... Zeile 117...
102
 
117
 
103
			if (!@rmdir($path)) { // @ is escalated to exception
118
			if (!@rmdir($path)) { // @ is escalated to exception
104
				throw new Nette\IOException(sprintf(
119
				throw new Nette\IOException(sprintf(
105
					"Unable to delete directory '%s'. %s",
120
					"Unable to delete directory '%s'. %s",
106
					self::normalizePath($path),
121
					self::normalizePath($path),
107
					Helpers::getLastError()
122
					Helpers::getLastError(),
108
				));
123
				));
109
			}
124
			}
110
		}
125
		}
Zeile 133... Zeile 148...
133
			if (!@rename($origin, $target)) { // @ is escalated to exception
148
			if (!@rename($origin, $target)) { // @ is escalated to exception
134
				throw new Nette\IOException(sprintf(
149
				throw new Nette\IOException(sprintf(
135
					"Unable to rename file or directory '%s' to '%s'. %s",
150
					"Unable to rename file or directory '%s' to '%s'. %s",
136
					self::normalizePath($origin),
151
					self::normalizePath($origin),
137
					self::normalizePath($target),
152
					self::normalizePath($target),
138
					Helpers::getLastError()
153
					Helpers::getLastError(),
139
				));
154
				));
140
			}
155
			}
141
		}
156
		}
142
	}
157
	}
Zeile 151... Zeile 166...
151
		$content = @file_get_contents($file); // @ is escalated to exception
166
		$content = @file_get_contents($file); // @ is escalated to exception
152
		if ($content === false) {
167
		if ($content === false) {
153
			throw new Nette\IOException(sprintf(
168
			throw new Nette\IOException(sprintf(
154
				"Unable to read file '%s'. %s",
169
				"Unable to read file '%s'. %s",
155
				self::normalizePath($file),
170
				self::normalizePath($file),
156
				Helpers::getLastError()
171
				Helpers::getLastError(),
157
			));
172
			));
158
		}
173
		}
Zeile 159... Zeile 174...
159
 
174
 
160
		return $content;
175
		return $content;
Zeile 161... Zeile 176...
161
	}
176
	}
-
 
177
 
-
 
178
 
-
 
179
	/**
-
 
180
	 * Reads the file content line by line. Because it reads continuously as we iterate over the lines,
-
 
181
	 * it is possible to read files larger than the available memory.
-
 
182
	 * @return \Generator<int, string>
-
 
183
	 * @throws Nette\IOException  on error occurred
-
 
184
	 */
-
 
185
	public static function readLines(string $file, bool $stripNewLines = true): \Generator
-
 
186
	{
-
 
187
		return (function ($f) use ($file, $stripNewLines) {
-
 
188
			$counter = 0;
-
 
189
			do {
-
 
190
				$line = Callback::invokeSafe('fgets', [$f], fn($error) => throw new Nette\IOException(sprintf(
-
 
191
					"Unable to read file '%s'. %s",
-
 
192
					self::normalizePath($file),
-
 
193
					$error,
-
 
194
				)));
-
 
195
				if ($line === false) {
-
 
196
					fclose($f);
-
 
197
					break;
-
 
198
				}
-
 
199
				if ($stripNewLines) {
-
 
200
					$line = rtrim($line, "\r\n");
-
 
201
				}
-
 
202
 
-
 
203
				yield $counter++ => $line;
-
 
204
 
-
 
205
			} while (true);
-
 
206
		})(static::open($file, 'r'));
-
 
207
	}
162
 
208
 
163
 
209
 
164
	/**
210
	/**
165
	 * Writes the string to a file.
211
	 * Writes the string to a file.
166
	 * @throws Nette\IOException  on error occurred
212
	 * @throws Nette\IOException  on error occurred
167
	 */
213
	 */
168
	public static function write(string $file, string $content, ?int $mode = 0666): void
214
	public static function write(string $file, string $content, ?int $mode = 0666): void
169
	{
215
	{
170
		static::createDir(dirname($file));
216
		static::createDir(dirname($file));
171
		if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
217
		if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
172
			throw new Nette\IOException(sprintf(
218
			throw new Nette\IOException(sprintf(
173
				"Unable to write file '%s'. %s",
219
				"Unable to write file '%s'. %s",
174
				self::normalizePath($file),
220
				self::normalizePath($file),
Zeile 175... Zeile 221...
175
				Helpers::getLastError()
221
				Helpers::getLastError(),
176
			));
222
			));
177
		}
223
		}
178
 
224
 
179
		if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
225
		if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
180
			throw new Nette\IOException(sprintf(
226
			throw new Nette\IOException(sprintf(
181
				"Unable to chmod file '%s' to mode %s. %s",
227
				"Unable to chmod file '%s' to mode %s. %s",
182
				self::normalizePath($file),
228
				self::normalizePath($file),
183
				decoct($mode),
229
				decoct($mode),
Zeile 184... Zeile 230...
184
				Helpers::getLastError()
230
				Helpers::getLastError(),
185
			));
231
			));
-
 
232
		}
186
		}
233
	}
187
	}
234
 
188
 
235
 
189
 
236
	/**
190
	/**
237
	 * Sets file permissions to `$fileMode` or directory permissions to `$dirMode`.
191
	 * Fixes permissions to a specific file or directory. Directories can be fixed recursively.
238
	 * Recursively traverses and sets permissions on the entire contents of the directory as well.
192
	 * @throws Nette\IOException  on error occurred
239
	 * @throws Nette\IOException  on error occurred
193
	 */
240
	 */
194
	public static function makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666): void
241
	public static function makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666): void
195
	{
242
	{
196
		if (is_file($path)) {
243
		if (is_file($path)) {
197
			if (!@chmod($path, $fileMode)) { // @ is escalated to exception
244
			if (!@chmod($path, $fileMode)) { // @ is escalated to exception
198
				throw new Nette\IOException(sprintf(
245
				throw new Nette\IOException(sprintf(
199
					"Unable to chmod file '%s' to mode %s. %s",
246
					"Unable to chmod file '%s' to mode %s. %s",
200
					self::normalizePath($path),
247
					self::normalizePath($path),
201
					decoct($fileMode),
248
					decoct($fileMode),
Zeile 210... Zeile 257...
210
			if (!@chmod($path, $dirMode)) { // @ is escalated to exception
257
			if (!@chmod($path, $dirMode)) { // @ is escalated to exception
211
				throw new Nette\IOException(sprintf(
258
				throw new Nette\IOException(sprintf(
212
					"Unable to chmod directory '%s' to mode %s. %s",
259
					"Unable to chmod directory '%s' to mode %s. %s",
213
					self::normalizePath($path),
260
					self::normalizePath($path),
214
					decoct($dirMode),
261
					decoct($dirMode),
215
					Helpers::getLastError()
262
					Helpers::getLastError(),
216
				));
263
				));
217
			}
264
			}
218
		} else {
265
		} else {
219
			throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($path)));
266
			throw new Nette\IOException(sprintf("File or directory '%s' not found.", self::normalizePath($path)));
220
		}
267
		}
Zeile 256... Zeile 303...
256
	 */
303
	 */
257
	public static function joinPaths(string ...$paths): string
304
	public static function joinPaths(string ...$paths): string
258
	{
305
	{
259
		return self::normalizePath(implode('/', $paths));
306
		return self::normalizePath(implode('/', $paths));
260
	}
307
	}
-
 
308
 
-
 
309
 
-
 
310
	/**
-
 
311
	 * Converts backslashes to slashes.
-
 
312
	 */
-
 
313
	public static function unixSlashes(string $path): string
-
 
314
	{
-
 
315
		return strtr($path, '\\', '/');
-
 
316
	}
-
 
317
 
-
 
318
 
-
 
319
	/**
-
 
320
	 * Converts slashes to platform-specific directory separators.
-
 
321
	 */
-
 
322
	public static function platformSlashes(string $path): string
-
 
323
	{
-
 
324
		return DIRECTORY_SEPARATOR === '/'
-
 
325
			? strtr($path, '\\', '/')
-
 
326
			: str_replace(':\\\\', '://', strtr($path, '/', '\\')); // protocol://
-
 
327
	}
261
}
328
}