Skip to content

Commit

Permalink
Create destination directory during rename
Browse files Browse the repository at this point in the history
  • Loading branch information
robgridley committed Mar 10, 2017
1 parent 75a3c7e commit 0651b79
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
12 changes: 9 additions & 3 deletions spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,26 @@ public function it_is_initializable()
$this->shouldHaveType(AdapterInterface::class);
}

public function it_should_rename_files(IShare $share)
public function it_should_rename_files(IShare $share, IFileInfo $file)
{
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
$file->isDirectory()->shouldBeCalled()->willReturn(true);
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willReturn(true);
$this->rename('foo', 'bar')->shouldReturn(true);
}

public function it_should_return_false_during_rename_when_source_does_not_exist(IShare $share)
public function it_should_return_false_during_rename_when_source_does_not_exist(IShare $share, IFileInfo $file)
{
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
$file->isDirectory()->shouldBeCalled()->willReturn(true);
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willThrow(NotFoundException::class);
$this->rename('foo', 'bar')->shouldReturn(false);
}

public function it_should_return_false_during_rename_when_destination_already_exists(IShare $share)
public function it_should_return_false_during_rename_when_destination_already_exists(IShare $share, IFileInfo $file)
{
$share->stat('prefix/')->shouldBeCalled()->willReturn($file);
$file->isDirectory()->shouldBeCalled()->willReturn(true);
$share->rename('prefix/foo', 'prefix/bar')->shouldBeCalled()->willThrow(AlreadyExistsException::class);
$this->rename('foo', 'bar')->shouldReturn(false);
}
Expand Down
30 changes: 20 additions & 10 deletions src/SmbAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function __construct(IShare $share, $prefix = null)
*/
public function write($path, $contents, Config $config)
{
$this->createDir(Util::dirname($path), $config);
$this->recursiveCreateDir(Util::dirname($path));

$location = $this->applyPathPrefix($path);
$stream = $this->share->write($location);
Expand All @@ -71,7 +71,7 @@ public function write($path, $contents, Config $config)
*/
public function writeStream($path, $resource, Config $config)
{
$this->createDir(Util::dirname($path), $config);
$this->recursiveCreateDir(Util::dirname($path));

$location = $this->applyPathPrefix($path);
$stream = $this->share->write($location);
Expand Down Expand Up @@ -120,6 +120,8 @@ public function updateStream($path, $resource, Config $config)
*/
public function rename($path, $newPath)
{
$this->recursiveCreateDir(Util::dirname($newPath));

$location = $this->applyPathPrefix($path);
$destination = $this->applyPathPrefix($newPath);

Expand Down Expand Up @@ -187,23 +189,31 @@ public function deleteDir($path)
*/
public function createDir($path, Config $config)
{
$result = compact('path');
$this->recursiveCreateDir($path);

return compact('path');
}

/**
* Recursively create directories.
*
* @param $path
*/
protected function recursiveCreateDir($path)
{
if ($this->isDirectory($path)) {
return $result;
return;
}

$directories = explode($this->pathSeparator, $path);
if (count($directories) > 1) {
$parentDirectories = array_splice($directories, 0, count($directories) - 1);
$this->createDir(implode($this->pathSeparator, $parentDirectories), $config);
$this->recursiveCreateDir(implode($this->pathSeparator, $parentDirectories));
}

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

$this->share->mkdir($location);

return $result;
}

/**
Expand Down Expand Up @@ -425,12 +435,12 @@ protected function deleteContents($path)
*/
protected function isDirectory($path)
{
if (empty($path)) {
$location = $this->applyPathPrefix($path);

if (empty($location)) {
return true;
}

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

try {
$file = $this->share->stat($location);
} catch (NotFoundException $e) {
Expand Down

0 comments on commit 0651b79

Please sign in to comment.