Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use function Hyde\normalize_slashes;
use function Hyde\path_join;
use function file_exists;
use function str_replace;
use function strlen;
use function substr;
use function array_map;
use function is_array;
use function str_starts_with;
Expand Down Expand Up @@ -90,8 +91,14 @@ public function pathToAbsolute(string|array $path): string|array
*/
public function pathToRelative(string $path): string
{
return normalize_slashes(str_starts_with($path, $this->path())
? unslash(str_replace($this->path(), '', $path))
$basePath = $this->path();

$isWithinBasePath = $path === $basePath
|| str_starts_with($path, $basePath.'/')
|| str_starts_with($path, $basePath.'\\');

return normalize_slashes($isWithinBasePath
? unslash(substr($path, strlen($basePath)))
: $path);
}

Expand Down
22 changes: 22 additions & 0 deletions packages/framework/tests/Feature/Foundation/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,28 @@ public function testPathToRelativeHelperDoesNotModifyAlreadyRelativePaths()
$this->assertSame('foo/bar.md', Hyde::pathToRelative('foo/bar.md'));
}

public function testPathToRelativeHelperOnlyStripsTheLeadingBasePath()
{
Hyde::getInstance()->setBasePath('/app');

$this->assertSame('_media/app.css', Hyde::pathToRelative('/app/_media/app.css'));
$this->assertSame('_media/app/app.css', Hyde::pathToRelative('/app/_media/app/app.css'));
}

public function testPathToRelativeHelperDoesNotModifyPathsThatMerelyShareAPrefixWithTheBasePath()
{
Hyde::getInstance()->setBasePath('/app');

$this->assertSame('/applesauce/foo.css', Hyde::pathToRelative('/applesauce/foo.css'));
}

public function testPathToRelativeHelperReturnsEmptyStringForTheBasePathItself()
{
Hyde::getInstance()->setBasePath('/app');

$this->assertSame('', Hyde::pathToRelative('/app'));
}

public function testPathToRelativeHelperDoesNotModifyNonProjectPaths()
{
$testStrings = [
Expand Down
Loading