Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ $node->path()->toString(); // string - the full path
$node->path()->name(); // string - filename with extension
$node->path()->basename(); // string - filename without extension
$node->path()->extension(); // string|null - file extension
$node->path()->dirname(); // string - the parent directory
$node->path()->dirname(); // string|null - the parent directory (or null if there is none)

$node->dsn(); // Zenstruck\Filesystem\Node\Dsn
$node->dsn()->toString(); // string - <filesystem-name>://<full-path>
(string) $node->dsn(); // same as above
$node->dsn()->path(); // Zenstruck\Filesystem\Node\Path
$node->dsn()->filesystem(); // string - name of the filesystem this node belongs to

$node->directory(); // Zenstruck\Filesystem\Node\Directory|null - parent directory object
$node->directory(); // Zenstruck\Filesystem\Node\Directory|null - parent directory object (or null if there is none)

$node->visibility(); // string - ie "public" or "private"
$node->lastModified(); // \DateTimeImmutable (in currently configured timezone)
Expand Down
2 changes: 1 addition & 1 deletion src/Filesystem/Node/FlysystemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function directory(): ?Directory
{
$dirname = $this->path()->dirname();

return '.' === $dirname ? null : new FlysystemDirectory($dirname, $this->operator);
return null === $dirname ? null : new FlysystemDirectory($dirname, $this->operator);
}

public function lastModified(): \DateTimeImmutable
Expand Down
10 changes: 8 additions & 2 deletions src/Filesystem/Node/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ public function basename(): string
return $this->nameParts()[0];
}

public function dirname(): string
public function dirname(): ?string
{
return \dirname($this->value);
if (!$this->value) {
return null;
}

$dirname = \dirname($this->value);

return '.' === $dirname ? '' : $dirname;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/Filesystem/Node/DirectoryTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function metadata(): void

$dir = $this->createDirectory(fixture('sub1'), 'foo');

$this->assertNull($dir->directory());
$this->assertSame('', $dir->directory()->path()->toString());
$this->assertNull($dir->directory()->directory());
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Filesystem/Node/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,22 @@ public function can_handle_multi_part_extensions(): void
$this->assertSame('bar', $path->basename());
$this->assertSame('foo', $path->dirname());
}

/**
* @test
* @dataProvider dirnameProvider
*/
public function dirname(string $path, ?string $expected): void
{
$this->assertSame($expected, (new Path($path))->dirname());
}

public static function dirnameProvider(): iterable
{
yield 'empty' => ['', null];
yield 'one level' => ['foo', ''];
yield 'one level trailing slash' => ['foo/', ''];
yield 'two levels' => ['foo/bar', 'foo'];
yield 'two levels trailing slash' => ['foo/bar/', 'foo'];
}
}
12 changes: 12 additions & 0 deletions tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ public function can_chmod(): void
$this->assertSame('private', $fs->file('some/file.txt')->visibility());
}

/**
* @test
*/
public function root_parent_dir(): void
{
$fs = $this->createFilesystem();

$fs->write('foo/bar.txt', 'content');

$this->assertNull($fs->directory()->directory());
}

/**
* @test
* @dataProvider writeValueProvider
Expand Down
Loading