Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f582b7

Browse files
committed
Thrown a FileNotFoundException in rename and unlink
1 parent 981ede2 commit 9f582b7

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

‎docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ In write mode, the stream context can contain the option ``gridfs['chunkSizeByte
5959
If omitted, the defaults are inherited from the ``Bucket`` instance option.
6060

6161
The functions `rename` and `unlink` will rename or remove all revisions of a
62-
filename. If the filename does not exist, these functions will return ``false``.
62+
filename. If the filename does not exist, these functions throw a ``FileNotFoundException``.
6363

6464
Example
6565
-------

‎src/GridFS/Exception/FileNotFoundException.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525

2626
class FileNotFoundException extends RuntimeException
2727
{
28+
/**
29+
* Thrown when a file cannot be found by its filename.
30+
*
31+
* @param string $filename Filename
32+
* @return self
33+
*/
34+
public static function byStreamWrapperFilename(string $filename)
35+
{
36+
return new self(sprintf('File with name "%s" not found', $filename));
37+
}
38+
2839
/**
2940
* Thrown when a file cannot be found by its filename and revision.
3041
*

‎src/GridFS/StreamWrapper.php‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public static function register(string $protocol = 'gridfs'): void
9696
/**
9797
* Rename all revisions of a filename.
9898
*
99-
* @return bool True on success or false on failure.
99+
* @return true
100+
* @throws FileNotFoundException
100101
*/
101102
public function rename(string $fromPath, string $toPath): bool
102103
{
@@ -110,9 +111,12 @@ public function rename(string $fromPath, string $toPath): bool
110111
$newFilename = explode('/', $toPath, 4)[3] ?? '';
111112
$count = $context['collectionWrapper']->updateFilenameForFilename($context['filename'], $newFilename);
112113

113-
// If $count === 0, the file does not exist.
114+
if ($count === 0) {
115+
throw FileNotFoundException::byStreamWrapperFilename($fromPath);
116+
}
117+
114118
// If $count is null, the update is unacknowledged, the operation is considered successful.
115-
return $count !== 0;
119+
return true;
116120
}
117121

118122
/**
@@ -293,12 +297,23 @@ public function stream_write(string $data): int
293297
return $this->stream->writeBytes($data);
294298
}
295299

300+
/**
301+
* Remove all revisions of a filename.
302+
*
303+
* @return true
304+
* @throws FileNotFoundException
305+
*/
296306
public function unlink(string $path): bool
297307
{
298308
$context = $this->getContext($path, 'w');
299309
$count = $context['collectionWrapper']->deleteFileAndChunksByFilename($context['filename']);
300310

301-
return $count !== 0;
311+
if ($count === 0) {
312+
throw FileNotFoundException::byStreamWrapperFilename($path);
313+
}
314+
315+
// If $count is null, the update is unacknowledged, the operation is considered successful.
316+
return true;
302317
}
303318

304319
/** @return false|array */

‎tests/GridFS/StreamWrapperFunctionalTest.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,9 @@ public function testRenameAllRevisions(): void
381381
$this->assertFalse(file_exists($path));
382382
$this->assertSame('foobar', file_get_contents($path . '.renamed'));
383383

384-
$result = rename($path, $path . '.renamed');
385-
$this->assertFalse($result, 'File does not exist anymore');
384+
$this->expectException(FileNotFoundException::class);
385+
$this->expectExceptionMessage('File with name "gridfs://bucket/filename" not found');
386+
rename($path, $path . '.renamed');
386387
}
387388

388389
public function testRenamePathMismatch(): void
@@ -406,7 +407,8 @@ public function testUnlinkAllRevisions(): void
406407
$this->assertTrue($result);
407408
$this->assertFalse(file_exists($path));
408409

409-
$result = unlink($path);
410-
$this->assertFalse($result, 'File does not exist anymore');
410+
$this->expectException(FileNotFoundException::class);
411+
$this->expectExceptionMessage('File with name "gridfs://bucket/path/to/filename" not found');
412+
unlink($path);
411413
}
412414
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /