Skip to content

Commit

Permalink
add resolvePath
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Oct 21, 2023
1 parent 671a8b8 commit c415af2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Filesystem/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class Path implements PathInterface

public function __construct(string $absolute)
{
$absolute = resolvePath($absolute);
$assert = new AssertPathFormat($absolute);
$this->absolute = $assert->path();
}
Expand Down
20 changes: 20 additions & 0 deletions src/Filesystem/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ function filePhpReturnForPath(string $path): FilePhpReturnInterface
{
return new FilePhpReturn(filePhpForPath($path));
}

function resolvePath(string $path): string
{
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
$parts = explode(DIRECTORY_SEPARATOR, $path);
$out = [];
foreach ($parts as $part) {
if ($part === '.') {
continue;
}
if ($part === '..') {
array_pop($out);

continue;
}
$out[] = $part;
}

return implode(DIRECTORY_SEPARATOR, $out);
}

0 comments on commit c415af2

Please sign in to comment.