getPathname());
}
foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($origin, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
if ($item->isDir()) {
static::createDir($target . '/' . $iterator->getSubPathName());
} else {
static::copy($item->getPathname(), $target . '/' . $iterator->getSubPathName());
}
}
} else {
static::createDir(\dirname($target));
if (@\stream_copy_to_stream(static::open($origin, 'rb'), static::open($target, 'wb')) === \false) {
// @ is escalated to exception
throw new Nette\IOException(\sprintf("Unable to copy file '%s' to '%s'. %s", self::normalizePath($origin), self::normalizePath($target), Helpers::getLastError()));
}
}
}
/**
* Opens file and returns resource.
* @return resource
* @throws Nette\IOException on error occurred
*/
public static function open(string $path, string $mode)
{
$f = @\fopen($path, $mode);
// @ is escalated to exception
if (!$f) {
throw new Nette\IOException(\sprintf("Unable to open file '%s'. %s", self::normalizePath($path), Helpers::getLastError()));
}
return $f;
}
/**
* Deletes a file or an entire directory if exists. If the directory is not empty, it deletes its contents first.
* @throws Nette\IOException on error occurred
*/
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(\sprintf("Unable to delete '%s'. %s", self::normalizePath($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(\sprintf("Unable to delete directory '%s'. %s", self::normalizePath($path), Helpers::getLastError()));
}
}
}
/**
* Renames or moves a file or a directory. Overwrites existing files and directories by default.
* @throws Nette\IOException on error occurred
* @throws Nette\InvalidStateException if $overwrite is set to false and destination already exists
*/
public static function rename(string $origin, string $target, bool $overwrite = \true) : void
{
if (!$overwrite && \file_exists($target)) {
throw new Nette\InvalidStateException(\sprintf("File or directory '%s' already exists.", self::normalizePath($target)));
} elseif (!\file_exists($origin)) {
throw new Nette\IOException(\sprintf("File or directory '%s' not found.", self::normalizePath($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(\sprintf("Unable to rename file or directory '%s' to '%s'. %s", self::normalizePath($origin), self::normalizePath($target), Helpers::getLastError()));
}
}
}
/**
* Reads the content of a file.
* @throws Nette\IOException on error occurred
*/
public static function read(string $file) : string
{
$content = @\file_get_contents($file);
// @ is escalated to exception
if ($content === \false) {
throw new Nette\IOException(\sprintf("Unable to read file '%s'. %s", self::normalizePath($file), Helpers::getLastError()));
}
return $content;
}
/**
* Reads the file content line by line. Because it reads continuously as we iterate over the lines,
* it is possible to read files larger than the available memory.
* @return \Generator