Skip to content

Commit

Permalink
FileSystem: add resolvePath
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Feb 17, 2024
1 parent b8ec3bc commit f3d2baa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Utils/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ public static function joinPaths(string ...$paths): string
}


public static function resolvePath(string ...$paths): string
{
for ($i = count($paths) - 1; $i >= 0; $i--) {
if (self::isAbsolute($paths[$i])) {
return self::joinPaths(...array_slice($paths, $i));
}
}

return self::joinPaths(getcwd(), ...$paths);
}


/**
* Converts backslashes to slashes.
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/Utils/FileSystem.resolvePath.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Nette\Utils\FileSystem;
use Tester\Assert;


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


test('', function () {
$S = DIRECTORY_SEPARATOR;
$cwd = getcwd();
Assert::same($cwd, FileSystem::resolvePath());
Assert::same("/foo{$S}bar", FileSystem::resolvePath('/foo', 'bar'));
Assert::same("/foo{$S}bar{$S}baz", FileSystem::resolvePath('/foo', 'bar', 'baz'));
Assert::same("/foo{$S}baz", FileSystem::resolvePath('/foo', 'bar/..', 'baz'));
Assert::same("/bar{$S}baz", FileSystem::resolvePath('foo', '/bar', 'baz'));
Assert::same("{$cwd}{$S}foo{$S}bar{$S}baz", FileSystem::resolvePath('foo', 'bar', 'baz'));
});

0 comments on commit f3d2baa

Please sign in to comment.