Skip to content

Commit 0651b79

Browse files
committed
Create destination directory during rename
1 parent 75a3c7e commit 0651b79

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,26 @@ public function it_is_initializable()
2626
$this->shouldHaveType(AdapterInterface::class);
2727
}
2828

29-
public function it_should_rename_files(IShare $share)
29+
public function it_should_rename_files(IShare $share, IFileInfo $file)
3030
{
31+
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
32+
$file->isDirectory()->shouldBeCalled()->willReturn(true);
3133
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willReturn(true);
3234
$this->rename('foo', 'bar')->shouldReturn(true);
3335
}
3436

35-
public function it_should_return_false_during_rename_when_source_does_not_exist(IShare $share)
37+
public function it_should_return_false_during_rename_when_source_does_not_exist(IShare $share, IFileInfo $file)
3638
{
39+
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
40+
$file->isDirectory()->shouldBeCalled()->willReturn(true);
3741
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willThrow(NotFoundException::class);
3842
$this->rename('foo', 'bar')->shouldReturn(false);
3943
}
4044

41-
public function it_should_return_false_during_rename_when_destination_already_exists(IShare $share)
45+
public function it_should_return_false_during_rename_when_destination_already_exists(IShare $share, IFileInfo $file)
4246
{
47+
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
48+
$file->isDirectory()->shouldBeCalled()->willReturn(true);
4349
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willThrow(AlreadyExistsException::class);
4450
$this->rename('foo', 'bar')->shouldReturn(false);
4551
}

src/SmbAdapter.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function __construct(IShare $share, $prefix = null)
4747
*/
4848
public function write($path, $contents, Config $config)
4949
{
50-
$this->createDir(Util::dirname($path), $config);
50+
$this->recursiveCreateDir(Util::dirname($path));
5151

5252
$location = $this->applyPathPrefix($path);
5353
$stream = $this->share->write($location);
@@ -71,7 +71,7 @@ public function write($path, $contents, Config $config)
7171
*/
7272
public function writeStream($path, $resource, Config $config)
7373
{
74-
$this->createDir(Util::dirname($path), $config);
74+
$this->recursiveCreateDir(Util::dirname($path));
7575

7676
$location = $this->applyPathPrefix($path);
7777
$stream = $this->share->write($location);
@@ -120,6 +120,8 @@ public function updateStream($path, $resource, Config $config)
120120
*/
121121
public function rename($path, $newPath)
122122
{
123+
$this->recursiveCreateDir(Util::dirname($newPath));
124+
123125
$location = $this->applyPathPrefix($path);
124126
$destination = $this->applyPathPrefix($newPath);
125127

@@ -187,23 +189,31 @@ public function deleteDir($path)
187189
*/
188190
public function createDir($path, Config $config)
189191
{
190-
$result = compact('path');
192+
$this->recursiveCreateDir($path);
193+
194+
return compact('path');
195+
}
191196

197+
/**
198+
* Recursively create directories.
199+
*
200+
* @param $path
201+
*/
202+
protected function recursiveCreateDir($path)
203+
{
192204
if ($this->isDirectory($path)) {
193-
return $result;
205+
return;
194206
}
195207

196208
$directories = explode($this->pathSeparator, $path);
197209
if (count($directories) > 1) {
198210
$parentDirectories = array_splice($directories, 0, count($directories) - 1);
199-
$this->createDir(implode($this->pathSeparator, $parentDirectories), $config);
211+
$this->recursiveCreateDir(implode($this->pathSeparator, $parentDirectories));
200212
}
201213

202214
$location = $this->applyPathPrefix($path);
203215

204216
$this->share->mkdir($location);
205-
206-
return $result;
207217
}
208218

209219
/**
@@ -425,12 +435,12 @@ protected function deleteContents($path)
425435
*/
426436
protected function isDirectory($path)
427437
{
428-
if (empty($path)) {
438+
$location = $this->applyPathPrefix($path);
439+
440+
if (empty($location)) {
429441
return true;
430442
}
431443

432-
$location = $this->applyPathPrefix($path);
433-
434444
try {
435445
$file = $this->share->stat($location);
436446
} catch (NotFoundException $e) {

0 commit comments

Comments
 (0)