From c69d60459be79c129aab3dce042d2254c2c91ee6 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Thu, 22 Feb 2018 17:35:39 +0300 Subject: [PATCH] Add feature to rename directories --- examples/directory_rename.php | 15 +++++++++++++++ src/Node/Directory.php | 11 +++++++++++ src/Node/DirectoryInterface.php | 8 ++++++++ tests/Node/DirectoryTest.php | 19 +++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 examples/directory_rename.php diff --git a/examples/directory_rename.php b/examples/directory_rename.php new file mode 100644 index 00000000..9fd41948 --- /dev/null +++ b/examples/directory_rename.php @@ -0,0 +1,15 @@ +dir('new'); + +$dir->rename('new_name')->then(function(\React\Filesystem\Node\DirectoryInterface $newDir){ + echo 'Renamed to ' . $newDir->getPath() . PHP_EOL; +}, function(Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); + +$loop->run(); diff --git a/src/Node/Directory.php b/src/Node/Directory.php index 9da87541..65b11669 100644 --- a/src/Node/Directory.php +++ b/src/Node/Directory.php @@ -164,6 +164,17 @@ public function remove() return $this->adapter->rmdir($this->path); } + + /** + * {@inheritdoc} + */ + public function rename($toDirectoryName) + { + return $this->adapter->rename($this->path, $toDirectoryName)->then(function () use ($toDirectoryName) { + return $this->filesystem->dir($toDirectoryName); + }); + } + /** * {@inheritDoc} */ diff --git a/src/Node/DirectoryInterface.php b/src/Node/DirectoryInterface.php index 36a60f1a..fea6bc97 100644 --- a/src/Node/DirectoryInterface.php +++ b/src/Node/DirectoryInterface.php @@ -28,6 +28,14 @@ public function createRecursive($mode = AdapterInterface::CREATION_MODE); */ public function remove(); + /** + * Rename the directory and return the new directory through a promise + * + * @param string $toDirectoryName + * @return PromiseInterface + */ + public function rename($toDirectoryName); + /** * List contents of the directory. * diff --git a/tests/Node/DirectoryTest.php b/tests/Node/DirectoryTest.php index 0882e2b4..cbb8a0b8 100644 --- a/tests/Node/DirectoryTest.php +++ b/tests/Node/DirectoryTest.php @@ -2,6 +2,7 @@ namespace React\Tests\Filesystem\Node; +use React\EventLoop\Factory; use React\Filesystem\Filesystem; use React\Filesystem\Node\Directory; use React\Filesystem\Node\File; @@ -81,6 +82,24 @@ public function testCreate() $this->assertInstanceOf('React\Promise\PromiseInterface', (new Directory($path, Filesystem::createFromAdapter($filesystem)))->create()); } + public function testRename() + { + $pathFrom = 'foo.bar'; + $pathTo = 'bar.foo'; + $filesystem = $this->mockAdapter(); + + $filesystem + ->expects($this->once()) + ->method('rename') + ->with($pathFrom, $pathTo) + ->will($this->returnValue(new FulfilledPromise())) + ; + + $newDirectory = \Clue\React\Block\await((new Directory($pathFrom, Filesystem::createFromAdapter($filesystem)))->rename($pathTo), Factory::create()); + $this->assertInstanceOf('React\Filesystem\Node\DirectoryInterface', $newDirectory); + $this->assertSame($pathTo . NodeInterface::DS, $newDirectory->getPath()); + } + public function testRemove() { $path = 'foo.bar';