Skip to content

Commit

Permalink
FileSystem: uses sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 24, 2021
1 parent fcd9bc6 commit c46df2a
Showing 1 changed file with 61 additions and 15 deletions.
76 changes: 61 additions & 15 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ final class FileSystem
public static function createDir(string $dir, int $mode = 0777): void
{
if (!is_dir($dir) && !@mkdir($dir, $mode, true) && !is_dir($dir)) { // @ - dir may already exist
throw new Nette\IOException("Unable to create directory '$dir' with mode " . decoct($mode) . '. ' . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to create directory '%s' with mode %s. %s",
$dir,
decoct($mode),
Helpers::getLastError()
));
}
}

Expand All @@ -39,10 +44,10 @@ public static function createDir(string $dir, int $mode = 0777): void
public static function copy(string $origin, string $target, bool $overwrite = true): void
{
if (stream_is_local($origin) && !file_exists($origin)) {
throw new Nette\IOException("File or directory '$origin' not found.");
throw new Nette\IOException(sprintf("File or directory '%s' not found.", $origin));

} elseif (!$overwrite && file_exists($target)) {
throw new Nette\InvalidStateException("File or directory '$target' already exists.");
throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", $target));

} elseif (is_dir($origin)) {
static::createDir($target);
Expand All @@ -64,7 +69,12 @@ public static function copy(string $origin, string $target, bool $overwrite = tr
&& ($d = @fopen($target, 'wb'))
&& @stream_copy_to_stream($s, $d) === false
) { // @ is escalated to exception
throw new Nette\IOException("Unable to copy file '$origin' to '$target'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to copy file '%s' to '%s'. %s",
$origin,
$target,
Helpers::getLastError()
));
}
}
}
Expand All @@ -79,15 +89,23 @@ public static function delete(string $path): void
if (is_file($path) || is_link($path)) {
$func = DIRECTORY_SEPARATOR === '\\' && is_dir($path) ? 'rmdir' : 'unlink';
if (!@$func($path)) { // @ is escalated to exception
throw new Nette\IOException("Unable to delete '$path'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to delete '%s'. %s",
$path,
Helpers::getLastError()
));
}

} elseif (is_dir($path)) {
foreach (new \FilesystemIterator($path) as $item) {
static::delete($item->getPathname());
}
if (!@rmdir($path)) { // @ is escalated to exception
throw new Nette\IOException("Unable to delete directory '$path'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to delete directory '%s'. %s",
$path,
Helpers::getLastError()
));
}
}
}
Expand All @@ -101,18 +119,23 @@ public static function delete(string $path): void
public static function rename(string $origin, string $target, bool $overwrite = true): void
{
if (!$overwrite && file_exists($target)) {
throw new Nette\InvalidStateException("File or directory '$target' already exists.");
throw new Nette\InvalidStateException(sprintf("File or directory '%s' already exists.", $target));

} elseif (!file_exists($origin)) {
throw new Nette\IOException("File or directory '$origin' not found.");
throw new Nette\IOException(sprintf("File or directory '%s' not found.", $origin));

} else {
static::createDir(dirname($target));
if (realpath($origin) !== realpath($target)) {
static::delete($target);
}
if (!@rename($origin, $target)) { // @ is escalated to exception
throw new Nette\IOException("Unable to rename file or directory '$origin' to '$target'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to rename file or directory '%s' to '%s'. %s",
$origin,
$target,
Helpers::getLastError()
));
}
}
}
Expand All @@ -126,7 +149,11 @@ public static function read(string $file): string
{
$content = @file_get_contents($file); // @ is escalated to exception
if ($content === false) {
throw new Nette\IOException("Unable to read file '$file'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to read file '%s'. %s",
$file,
Helpers::getLastError()
));
}
return $content;
}
Expand All @@ -140,10 +167,19 @@ public static function write(string $file, string $content, ?int $mode = 0666):
{
static::createDir(dirname($file));
if (@file_put_contents($file, $content) === false) { // @ is escalated to exception
throw new Nette\IOException("Unable to write file '$file'. " . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to write file '%s'. %s",
$file,
Helpers::getLastError()
));
}
if ($mode !== null && !@chmod($file, $mode)) { // @ is escalated to exception
throw new Nette\IOException("Unable to chmod file '$file' to mode " . decoct($mode) . '. ' . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to chmod file '%s' to mode %s. %s",
$file,
decoct($mode),
Helpers::getLastError()
));
}
}

Expand All @@ -156,17 +192,27 @@ public static function makeWritable(string $path, int $dirMode = 0777, int $file
{
if (is_file($path)) {
if (!@chmod($path, $fileMode)) { // @ is escalated to exception
throw new Nette\IOException("Unable to chmod file '$path' to mode " . decoct($fileMode) . '. ' . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to chmod file '%s' to mode %s. %s",
$path,
decoct($fileMode),
Helpers::getLastError()
));
}
} elseif (is_dir($path)) {
foreach (new \FilesystemIterator($path) as $item) {
static::makeWritable($item->getPathname(), $dirMode, $fileMode);
}
if (!@chmod($path, $dirMode)) { // @ is escalated to exception
throw new Nette\IOException("Unable to chmod directory '$path' to mode " . decoct($dirMode) . '. ' . Helpers::getLastError());
throw new Nette\IOException(sprintf(
"Unable to chmod directory '%s' to mode %s. %s",
$path,
decoct($dirMode),
Helpers::getLastError()
));
}
} else {
throw new Nette\IOException("File or directory '$path' not found.");
throw new Nette\IOException(sprintf("File or directory '%s' not found.", $path));
}
}

Expand Down

0 comments on commit c46df2a

Please sign in to comment.