mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-18 04:48:39 +00:00
Improve robustness
This commit is contained in:
parent
bc77b62685
commit
ff91b98a44
|
|
@ -74,24 +74,28 @@ final class File extends FileAbstract implements LocalContainerInterface, FileIn
|
|||
{
|
||||
$exists = \file_exists($path);
|
||||
|
||||
if ((ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists)
|
||||
|| (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists)
|
||||
|| (ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE) && $exists)
|
||||
|| (!$exists && ContentPutMode::hasFlag($mode, ContentPutMode::CREATE))
|
||||
) {
|
||||
if (ContentPutMode::hasFlag($mode, ContentPutMode::APPEND) && $exists) {
|
||||
\file_put_contents($path, \file_get_contents($path) . $content);
|
||||
} elseif (ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND) && $exists) {
|
||||
\file_put_contents($path, $content . \file_get_contents($path));
|
||||
} else {
|
||||
if (!Directory::exists(\dirname($path))) {
|
||||
Directory::create(\dirname($path), 0755, true);
|
||||
try {
|
||||
if (($exists && ContentPutMode::hasFlag($mode, ContentPutMode::APPEND))
|
||||
|| ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND))
|
||||
|| ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::REPLACE))
|
||||
|| (!$exists && ContentPutMode::hasFlag($mode, ContentPutMode::CREATE))
|
||||
) {
|
||||
if ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::APPEND)) {
|
||||
\file_put_contents($path, \file_get_contents($path) . $content);
|
||||
} elseif ($exists && ContentPutMode::hasFlag($mode, ContentPutMode::PREPEND)) {
|
||||
\file_put_contents($path, $content . \file_get_contents($path));
|
||||
} else {
|
||||
if (!Directory::exists(\dirname($path))) {
|
||||
Directory::create(\dirname($path), 0755, true);
|
||||
}
|
||||
|
||||
\file_put_contents($path, $content);
|
||||
}
|
||||
|
||||
\file_put_contents($path, $content);
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $e) {
|
||||
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
|
||||
{
|
||||
if (!\file_exists($path)) {
|
||||
throw new PathException($path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) \filesize($path);
|
||||
|
|
|
|||
|
|
@ -116,9 +116,7 @@ class FileTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function testInvalidSizePath() : void
|
||||
{
|
||||
self::expectException(\phpOMS\System\File\PathException::class);
|
||||
|
||||
File::size(__DIR__ . '/invalid.txt');
|
||||
self::assertEquals(0, File::size(__DIR__ . '/invalid.txt'));
|
||||
}
|
||||
|
||||
public function testInvalidPermissionPath() : void
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user