Improve robustness

This commit is contained in:
Dennis Eichhorn 2019-04-27 12:05:06 +02:00
parent bc77b62685
commit ff91b98a44
2 changed files with 21 additions and 19 deletions

View File

@ -74,24 +74,28 @@ final class File extends FileAbstract implements LocalContainerInterface, FileIn
{ {
$exists = \file_exists($path); $exists = \file_exists($path);
if ((ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) try {
|| (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) if (($exists && ContentPutMode::hasFlag($mode, ContentPutMode::APPEND))
|| (ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE) && $exists) || ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND))
|| (!$exists && ContentPutMode::hasFlag($mode, ContentPutMode::CREATE)) || ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE))
) { || (!$exists && ContentPutMode::hasFlag($mode, ContentPutMode::CREATE))
if (ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) { ) {
\file_put_contents($path, \file_get_contents($path) . $content); if ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::APPEND)) {
} elseif (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) { \file_put_contents($path, \file_get_contents($path) . $content);
\file_put_contents($path, $content . \file_get_contents($path)); } elseif ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND)) {
} else { \file_put_contents($path, $content . \file_get_contents($path));
if (!Directory::exists(\dirname($path))) { } else {
Directory::create(\dirname($path), 0755, true); if (!Directory::exists(\dirname($path))) {
Directory::create(\dirname($path), 0755, true);
}
\file_put_contents($path, $content);
} }
\file_put_contents($path, $content); return true;
} }
} catch (\Throwable $e) {
return true; return false;
} }
return false; return false;
@ -251,7 +255,7 @@ final class File extends FileAbstract implements LocalContainerInterface, FileIn
public static function size(string $path, bool $recursive = true) : int public static function size(string $path, bool $recursive = true) : int
{ {
if (!\file_exists($path)) { if (!\file_exists($path)) {
throw new PathException($path); return 0;
} }
return (int) \filesize($path); return (int) \filesize($path);

View File

@ -116,9 +116,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
public function testInvalidSizePath() : void public function testInvalidSizePath() : void
{ {
self::expectException(\phpOMS\System\File\PathException::class); self::assertEquals(0, File::size(__DIR__ . '/invalid.txt'));
File::size(__DIR__ . '/invalid.txt');
} }
public function testInvalidPermissionPath() : void public function testInvalidPermissionPath() : void