|
22 | 22 | use MongoDB\GridFS\Exception\FileNotFoundException; |
23 | 23 | use MongoDB\GridFS\Exception\LogicException; |
24 | 24 |
|
| 25 | +use function array_slice; |
25 | 26 | use function assert; |
26 | 27 | use function explode; |
| 28 | +use function implode; |
27 | 29 | use function in_array; |
28 | 30 | use function is_array; |
29 | 31 | use function is_integer; |
30 | 32 | use function is_resource; |
| 33 | +use function str_starts_with; |
31 | 34 | use function stream_context_get_options; |
32 | 35 | use function stream_get_wrappers; |
33 | 36 | use function stream_wrapper_register; |
@@ -90,6 +93,30 @@ public static function register(string $protocol = 'gridfs'): void |
90 | 93 | stream_wrapper_register($protocol, static::class, STREAM_IS_URL); |
91 | 94 | } |
92 | 95 |
|
| 96 | + /** |
| 97 | + * Rename all revisions of a filename. |
| 98 | + * |
| 99 | + * @return bool True on success or false on failure. |
| 100 | + */ |
| 101 | + public function rename(string $fromPath, string $toPath): bool |
| 102 | + { |
| 103 | + $prefix = implode('/', array_slice(explode('/', $fromPath, 4), 0, 3)) . '/'; |
| 104 | + if (! str_starts_with($toPath, $prefix)) { |
| 105 | + throw LogicException::renamePathMismatch($fromPath, $toPath); |
| 106 | + } |
| 107 | + |
| 108 | + try { |
| 109 | + $this->stream_open($fromPath, 'r', 0, $openedPath); |
| 110 | + } catch (FileNotFoundException $e) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + $newName = explode('/', $toPath, 4)[3] ?? ''; |
| 115 | + assert($this->stream instanceof ReadableStream); |
| 116 | + |
| 117 | + return $this->stream->rename($newName); |
| 118 | + } |
| 119 | + |
93 | 120 | /** |
94 | 121 | * @see Bucket::resolveStreamContext() |
95 | 122 | * |
|
0 commit comments