From 6862a0a02859cb491d19724c787810548ee42b4d Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:57:01 +0400 Subject: [PATCH] feat(core): Added `File::isFile()` and `File::isDirectory()`. --- packages/core/src/Utils/Path.php | 12 ++++++++++-- packages/core/src/Utils/PathTest.php | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Utils/Path.php b/packages/core/src/Utils/Path.php index 9535bf4a..dd16215b 100644 --- a/packages/core/src/Utils/Path.php +++ b/packages/core/src/Utils/Path.php @@ -4,7 +4,7 @@ use Symfony\Component\Filesystem\Path as SymfonyPath; -use function dirname; +use function is_dir; use function is_file; class Path { @@ -27,7 +27,15 @@ public static function getRelativePath(string $root, string $path): string { } public static function getDirname(string $path): string { - return static::normalize(is_file($path) ? dirname($path) : $path); + return static::normalize(static::isFile($path) ? SymfonyPath::getDirectory($path) : $path); + } + + public static function isFile(string $path): bool { + return is_file(static::normalize($path)); + } + + public static function isDirectory(string $path): bool { + return is_dir(static::normalize($path)); } public static function isRelative(string $path): bool { diff --git a/packages/core/src/Utils/PathTest.php b/packages/core/src/Utils/PathTest.php index 1165c9b5..875b6172 100644 --- a/packages/core/src/Utils/PathTest.php +++ b/packages/core/src/Utils/PathTest.php @@ -44,6 +44,16 @@ public function testNormalize(): void { self::assertEquals('any/path', Path::normalize('././any/path')); self::assertEquals('../any/path', Path::normalize('./../any/path')); self::assertEquals('path', Path::normalize('./any/../path')); + self::assertEquals('', Path::normalize('./')); + self::assertEquals('', Path::normalize('.')); + self::assertEquals('..', Path::normalize('../')); + self::assertEquals('..', Path::normalize('..')); + self::assertEquals('path', Path::normalize('./any/../path/.')); + self::assertEquals('/', Path::normalize('/..')); + self::assertEquals('../any/path', Path::normalize('.\\..\\any\\path')); + self::assertEquals('any/path', Path::normalize('any\\path')); + self::assertEquals('/any/path', Path::normalize('/any/path/')); + self::assertEquals('any/path', Path::normalize('any/path/')); } public function testIsNormalized(): void { @@ -53,4 +63,16 @@ public function testIsNormalized(): void { self::assertFalse(Path::isNormalized('././any/path')); self::assertFalse(Path::isNormalized('./../any/path')); } + + public function testIsFile(): void { + self::assertTrue(Path::isFile(__FILE__)); + self::assertFalse(Path::isFile(__DIR__)); + self::assertFalse(Path::isFile('/path/to/file')); + } + + public function testIsDirectory(): void { + self::assertTrue(Path::isDirectory(__DIR__)); + self::assertFalse(Path::isDirectory(__FILE__)); + self::assertFalse(Path::isDirectory('/path/to/file')); + } }