Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystem: messages use normalized paths #250

Merged
merged 25 commits into from Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/Utils/FileSystem.php
Expand Up @@ -26,7 +26,7 @@ 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("Unable to create directory '" . self::normalizePath($dir) . "' with mode " . decoct($mode) . '. ' . Helpers::getLastError());
}
}

Expand All @@ -39,10 +39,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("File or directory '" . self::normalizePath($origin) . "' not found.");

} elseif (!$overwrite && file_exists($target)) {
throw new Nette\InvalidStateException("File or directory '$target' already exists.");
throw new Nette\InvalidStateException("File or directory '" . self::normalizePath($target) . "' already exists.");

} elseif (is_dir($origin)) {
static::createDir($target);
Expand All @@ -64,7 +64,7 @@ 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("Unable to copy file '" . self::normalizePath($origin) . "' to '" . self::normalizePath($target) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -79,15 +79,15 @@ 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("Unable to delete '" . 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("Unable to delete directory '$path'. " . Helpers::getLastError());
throw new Nette\IOException("Unable to delete directory '" . self::normalizePath($path) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -101,18 +101,18 @@ 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("File or directory '" . self::normalizePath($target) . "' already exists.");

} elseif (!file_exists($origin)) {
throw new Nette\IOException("File or directory '$origin' not found.");
throw new Nette\IOException("File or directory '" . self::normalizePath($origin) . "' not found.");

} 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("Unable to rename file or directory '" . self::normalizePath($origin) . "' to '" . self::normalizePath($target) . "'. " . Helpers::getLastError());
}
}
}
Expand All @@ -126,7 +126,7 @@ 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("Unable to read file '" . self::normalizePath($file) . "'. " . Helpers::getLastError());
}
return $content;
}
Expand All @@ -140,10 +140,31 @@ 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("Unable to write file '" . self::normalizePath($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("Unable to chmod file '" . self::normalizePath($file) . "' to mode " . decoct($mode) . '. ' . Helpers::getLastError());
}
}


/**
* Fixes permissions to a specific file or directory. Directories can be fixed recursively.
* @throws Nette\IOException on error occurred
*/
public static function makeWritable(string $path, int $dirMode = 0777, int $fileMode = 0666): void
{
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());
}
} 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());
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Utils/Reflection.getReturnType.phpt
Expand Up @@ -63,6 +63,11 @@ class A
public function nullableUnionType(): array|self|null
{
}


function unionType(): array|A
{
}
}

class AExt extends A
Expand Down
24 changes: 24 additions & 0 deletions tests/Utils/Reflection.getReturnType.x.phpt
@@ -0,0 +1,24 @@
<?php

/**
* Test: Nette\Utils\Reflection::getReturnType
* @phpversion 7
*/

require __DIR__ . '/../bootstrap.php';

class FunctionReflection
{
}

interface FunctionReflectionFactory
{

public function create(
\ReflectionFunction $reflection,
array $phpDocParameterTypes
): FunctionReflection|string;

}

dump(Nette\Utils\Reflection::getReturnType(new \ReflectionMethod(FunctionReflectionFactory::class, 'create')));