Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix persisting files on a case insensitive filesystem
- Loading branch information
Showing
8 changed files
with
153 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Working with case insensitive filesystems | ||
|
||
By default this package assumes you're working with a case sensitive filesystem, meaning you can have 2 files `a` and `A` inside a same directory. However this is not possible on a case insensitive filesystem (such as APFS on macOS), it will create only one of both. | ||
|
||
If you're dealing with a case insensitive filesystem then you need to specify it on the adapter like this: | ||
|
||
```php | ||
use Innmind\Filesystem\{ | ||
Adapter\Filesystem, | ||
CaseSensitivity, | ||
}; | ||
use Innmind\Url\Path; | ||
|
||
$adapter = Filesystem::mount(Path::of('somewhere/')) | ||
->withCaseSensitivity(CaseSensitivity::insensitive); | ||
$adapter instanceof Filesystem; // true, use $adapter as usual | ||
``` | ||
|
||
**Advice**: If you persist user provided files on a filesystem you should use normalized names (like UUIDs) and keep the original names in a database to avoid collisions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\Filesystem; | ||
|
||
use Innmind\Immutable\Set; | ||
|
||
enum CaseSensitivity | ||
{ | ||
case sensitive; | ||
case insensitive; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @param Set<Name> $in | ||
*/ | ||
public function contains(Name $name, Set $in): bool | ||
{ | ||
return match ($this) { | ||
self::sensitive => $in | ||
->map($this->normalize(...)) | ||
->contains($this->normalize($name)), | ||
self::insensitive => $in | ||
->map($this->normalize(...)) | ||
->contains($this->normalize($name)), | ||
}; | ||
} | ||
|
||
private function normalize(Name $name): string | ||
{ | ||
return match ($this) { | ||
self::sensitive => $name->toString(), | ||
self::insensitive => $name->str()->toLower()->toString(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Tests\Innmind\Filesystem; | ||
|
||
use Innmind\Filesystem\{ | ||
CaseSensitivity, | ||
Name, | ||
}; | ||
use Innmind\Immutable\Set as ISet; | ||
use PHPUnit\Framework\TestCase; | ||
use Innmind\BlackBox\{ | ||
PHPUnit\BlackBox, | ||
Set, | ||
}; | ||
use Fixtures\Innmind\Filesystem\Name as FName; | ||
|
||
class CaseSensitivityTest extends TestCase | ||
{ | ||
use BlackBox; | ||
|
||
public function testContains() | ||
{ | ||
$this | ||
->forAll( | ||
FName::strings(), | ||
FName::strings(), | ||
) | ||
->filter(static fn($a, $b) => $a !== $b) | ||
->then(function($a, $b) { | ||
$this->assertTrue(CaseSensitivity::sensitive->contains( | ||
Name::of($a), | ||
ISet::of(Name::of($a)), | ||
)); | ||
$this->assertFalse(CaseSensitivity::sensitive->contains( | ||
Name::of($a), | ||
ISet::of(Name::of($b)), | ||
)); | ||
}); | ||
$this | ||
->forAll( | ||
FName::strings(), | ||
Set\Sequence::of(FName::strings()), | ||
) | ||
->filter(static fn($a, $b) => !\in_array($a, $b, true)) | ||
->then(function($a, $b) { | ||
$this->assertFalse(CaseSensitivity::sensitive->contains( | ||
Name::of($a), | ||
ISet::of(...$b)->map(Name::of(...)), | ||
)); | ||
}); | ||
$this | ||
->forAll(FName::strings()) | ||
->then(function($a) { | ||
$this->assertTrue(CaseSensitivity::insensitive->contains( | ||
Name::of($a), | ||
ISet::of($a)->map(\strtolower(...))->map(Name::of(...)), | ||
)); | ||
$this->assertTrue(CaseSensitivity::insensitive->contains( | ||
Name::of($a), | ||
ISet::of($a)->map(\strtoupper(...))->map(Name::of(...)), | ||
)); | ||
}); | ||
} | ||
} |