diff --git a/spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php b/spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php index 2c78440..7433d53 100644 --- a/spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php +++ b/spec/RobGridley/Flysystem/Smb/SmbAdapterSpec.php @@ -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); } diff --git a/src/SmbAdapter.php b/src/SmbAdapter.php index cfe3813..2df35da 100644 --- a/src/SmbAdapter.php +++ b/src/SmbAdapter.php @@ -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); @@ -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); @@ -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); @@ -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; } /** @@ -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) {