Skip to content

Commit 67bfcce

Browse files
committed
:octocat: +Directory::relativePath()
1 parent a3b677f commit 67bfcce

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@ output and input `$format` for the functions `Crypto::encrypt()` and `Crypto::de
8080

8181
### `Directory`
8282

83-
| method | description |
84-
|-------------------------------------------------------------------------------------------|---------------------------------------------------------------|
85-
| `Directory::exists(string $dir):bool` | Checks whether a directory exists |
86-
| `Directory::isReadable(string $dir):bool` | Checks whether the given directory is readable |
87-
| `Directory::isWritable(string $dir):bool` | Checks whether the given directory is writable |
88-
| `Directory::create(string $dir, int $permissions = 0o777, bool $recursive = true):string` | Creates a directory |
89-
| `Directory::remove(string $dir):bool` | Removes a directory |
83+
| method | description |
84+
|-------------------------------------------------------------------------------------------------------|----------------------------------------------------|
85+
| `Directory::exists(string $dir):bool` | Checks whether a directory exists |
86+
| `Directory::isReadable(string $dir):bool` | Checks whether the given directory is readable |
87+
| `Directory::isWritable(string $dir):bool` | Checks whether the given directory is writable |
88+
| `Directory::create(string $dir, int $permissions = 0o777, bool $recursive = true):string` | Creates a directory |
89+
| `Directory::remove(string $dir):bool` | Removes a directory |
90+
| `Directory::relativePath(string $path, string $from, string $separator = DIRECTORY_SEPARATOR):string` | Returns the relative path from the given directory |
9091

9192

9293
### `File`

src/Directory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
use InvalidArgumentException;
1515
use RuntimeException;
1616
use function clearstatcache;
17+
use function dirname;
1718
use function file_exists;
1819
use function is_dir;
20+
use function is_file;
1921
use function is_readable;
2022
use function is_writable;
2123
use function mkdir;
2224
use function rmdir;
2325
use function sprintf;
26+
use function str_replace;
2427
use function trim;
28+
use const DIRECTORY_SEPARATOR;
2529

2630
/**
2731
* Basic directory utilities
@@ -100,4 +104,22 @@ public static function remove(string $dir):bool{
100104
return true;
101105
}
102106

107+
/**
108+
* Returns the relative path from the given directory (realpath)
109+
*/
110+
public static function relativePath(string $path, string $from, string $separator = DIRECTORY_SEPARATOR):string{
111+
$path = File::realpath($path);
112+
$from = File::realpath($from);
113+
114+
if(is_file($path)){
115+
$path = dirname($path);
116+
}
117+
118+
if(is_file($from)){
119+
$from = dirname($from);
120+
}
121+
122+
return trim(str_replace([$from, '\\', '/'], ['', $separator, $separator], $path), '\\/');
123+
}
124+
103125
}

tests/DirAndFileTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ public function removeDirectoryEmptyNameException():void{
112112
Directory::remove('');
113113
}
114114

115+
#[Test]
116+
public function relativePath():void{
117+
$relative = Directory::relativePath(__DIR__.'/filetest/.gitkeep', __DIR__.'/..', '/');
118+
119+
$this::assertSame('tests/filetest', $relative);
120+
}
121+
115122
}

0 commit comments

Comments
 (0)