Skip to content

Commit 442ab34

Browse files
committed
Add GridFS\Bucket::renameByName()
1 parent 8b5ec29 commit 442ab34

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/GridFS/Bucket.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,24 @@ public function rename(mixed $id, string $newFilename)
661661
}
662662
}
663663

664+
/**
665+
* Renames all the revisions of a file name in the GridFS bucket.
666+
*
667+
* @param string $filename Filename
668+
* @param string $newFilename New filename
669+
*
670+
* @throws FileNotFoundException if no file could be selected
671+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
672+
*/
673+
public function renameByName(string $filename, string $newFilename): void
674+
{
675+
$matchedCount = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);
676+
677+
if (! $matchedCount) {
678+
throw FileNotFoundException::byFilename($filename);
679+
}
680+
}
681+
664682
/**
665683
* Writes the contents of a readable stream to a GridFS file.
666684
*

tests/GridFS/BucketFunctionalTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,24 @@ public function testRenameShouldRequireFileToExist(): void
758758
$this->bucket->rename('nonexistent-id', 'b');
759759
}
760760

761+
public function testRenameByName(): void
762+
{
763+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
764+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
765+
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
766+
767+
$this->bucket->renameByName('filename', 'newname');
768+
769+
$this->assertNull($this->bucket->findOne(['filename' => 'filename']), 'No file has the old name');
770+
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('newname'));
771+
}
772+
773+
public function testRenameByNameShouldRequireFileToExist(): void
774+
{
775+
$this->expectException(FileNotFoundException::class);
776+
$this->bucket->renameByName('nonexistent-name', 'b');
777+
}
778+
761779
public function testUploadFromStream(): void
762780
{
763781
$options = [

0 commit comments

Comments
 (0)