From 45d0748cf0dcd2806a6500e0101096de6f71b224 Mon Sep 17 00:00:00 2001 From: Damien MATHIEU Date: Thu, 1 Aug 2024 16:19:29 +0200 Subject: [PATCH 1/4] Restore File::unique() method --- src/Filesystem/Filesystem.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Filesystem/Filesystem.php b/src/Filesystem/Filesystem.php index 9904402b9..a40ca19dc 100644 --- a/src/Filesystem/Filesystem.php +++ b/src/Filesystem/Filesystem.php @@ -166,6 +166,34 @@ public function isLocalDisk(\Illuminate\Filesystem\FilesystemAdapter $disk): boo return ($disk->getAdapter() instanceof \League\Flysystem\Local\LocalFilesystemAdapter); } + /** + * Apply a unique index to a filename from provided list i.e. + * winter.txt, [winter_1.txt, winter_2.txt] -> winter_3.txt + * winter.txt, [winter_1.txt, winter_3.txt] -> winter_4.txt + */ + public function unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1): string + { + $indexes = []; + + $info = pathinfo($str); + + if (empty($info['filename']) || empty($info['extension'])) { + throw new \InvalidArgumentException('$str must be a file name'); + } + + foreach ($list as $item) { + if (!preg_match('/' . $info['filename'] . $separator . '(\d*)\.' . $info['extension'] . '/', $item, $matches)) { + continue; + } + + $indexes[] = (int) $matches[1]; + } + + return empty($indexes) + ? $info['filename'] . $separator . $starting . '.' . $info['extension'] + : $info['filename'] . $separator . (max($indexes) + $step) . '.' . $info['extension']; + } + /** * Finds the path of a given class. * From 0f2b4c492c7c941b2be7635396743cebdb4f6e84 Mon Sep 17 00:00:00 2001 From: Damien MATHIEU Date: Thu, 1 Aug 2024 16:24:44 +0200 Subject: [PATCH 2/4] Retore FileSystem unique test --- tests/Filesystem/FilesystemTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php index 3ffa36362..f9af0bf3e 100644 --- a/tests/Filesystem/FilesystemTest.php +++ b/tests/Filesystem/FilesystemTest.php @@ -14,6 +14,14 @@ public function setUp(): void $this->filesystem = new Filesystem(); } + public function testUnique() + { + $this->assertSame('winter_4.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); + $this->assertSame('winter_98.cms', $this->filesystem->unique('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); + $this->assertSame('winter 1.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); + $this->assertSame('winter_1.cms', $this->filesystem->unique('winter.cms', ['test_5'])); + } + /** * @dataProvider providePathsForIsAbsolutePath * @see Symfony\Component\Filesystem\Tests\FilesystemTest::testIsAbsolutePath From c9e90950c357100be2e7aa5131fd748bf5f78d8d Mon Sep 17 00:00:00 2001 From: Damien MATHIEU Date: Thu, 1 Aug 2024 16:36:07 +0200 Subject: [PATCH 3/4] Fix code quality check --- tests/Filesystem/FilesystemTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php index f9af0bf3e..0f7c71e6d 100644 --- a/tests/Filesystem/FilesystemTest.php +++ b/tests/Filesystem/FilesystemTest.php @@ -16,10 +16,10 @@ public function setUp(): void public function testUnique() { - $this->assertSame('winter_4.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); + $this->assertSame('winter_4.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'])); $this->assertSame('winter_98.cms', $this->filesystem->unique('winter.cms', ['winter_97.cms', 'test_5', 'winter_1.cms'])); - $this->assertSame('winter 1.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); - $this->assertSame('winter_1.cms', $this->filesystem->unique('winter.cms', ['test_5'])); + $this->assertSame('winter 1.cms', $this->filesystem->unique('winter.cms', ['winter_1.cms', 'test_5', 'winter_3.cms'], ' ')); + $this->assertSame('winter_1.cms', $this->filesystem->unique('winter.cms', ['test_5'])); } /** From d66cc3f45bb97addd62ddb4735f14956c25d695e Mon Sep 17 00:00:00 2001 From: DamsFX Date: Thu, 5 Dec 2024 19:19:57 +0100 Subject: [PATCH 4/4] Add unique() method signature to the File facade --- src/Support/Facades/File.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Support/Facades/File.php b/src/Support/Facades/File.php index df56aac40..ed24ada92 100644 --- a/src/Support/Facades/File.php +++ b/src/Support/Facades/File.php @@ -55,6 +55,7 @@ * @method static bool fileNameMatch(string $fileName, string $pattern) * @method static bool copyBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null) * @method static bool moveBetweenDisks(string|FilesystemAdapter $sourceDisk, string|FilesystemAdapter $destinationDisk, string $filePath, ?string $targetPath = null) + * @method static string unique(string $str, array $list, string $separator = '_', int $starting = 1, int $step = 1) * * @see \Winter\Storm\Filesystem\Filesystem */