/** * {@inheritdoc} */ public function rename($origin, $target, $overwrite = false) { //we check that target does not exist $targetFileInfo = new SplFileInfo($target, $this->fs); if ($targetFileInfo->isReadable()) { if ($overwrite) { $this->remove($target); } else { throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target)); } } $pattern = '/^' . preg_quote(rtrim($origin, '/'), '/') . '/'; $files = $this->fs->find(array('filename' => new \MongoRegex($pattern))); $n = 0; foreach ($files as $file) { $result = $this->fs->update(array('_id' => $file->file['_id']), array('$set' => array('filename' => preg_replace($pattern, $target, $file->file['filename'])))); $n += $result['n']; } if ($n < 1) { throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target)); } }