Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50766f6

Browse files
committed
Add feature to rename directories
1 parent 76494e6 commit 50766f6

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

examples/directory_rename.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\Filesystem\Filesystem;
5+
6+
require '../vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
$filesystem = Filesystem::create($loop);
10+
$dir = $filesystem->dir('new');
11+
12+
$dir->rename('new_name')->then(function(\React\Filesystem\Node\DirectoryInterface $newDir){
13+
echo 'Renamed to ' . $newDir->getPath() . PHP_EOL;
14+
}, function(Exception $e) {
15+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
16+
});
17+
18+
$loop->run();

src/Node/Directory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ public function remove()
164164
return $this->adapter->rmdir($this->path);
165165
}
166166

167+
168+
/**
169+
* {@inheritdoc}
170+
*/
171+
public function rename($toDirectoryName)
172+
{
173+
return $this->adapter->rename($this->path, $toDirectoryName)->then(function () use ($toDirectoryName) {
174+
return $this->filesystem->dir($toDirectoryName);
175+
});
176+
}
177+
167178
/**
168179
* {@inheritDoc}
169180
*/

src/Node/DirectoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public function createRecursive($mode = AdapterInterface::CREATION_MODE);
2828
*/
2929
public function remove();
3030

31+
/**
32+
* Rename the directory and return the new directory through a promise
33+
*
34+
* @param string $toDirectoryName
35+
* @return PromiseInterface<FileInterface>
36+
*/
37+
public function rename($toDirectoryName);
38+
3139
/**
3240
* List contents of the directory.
3341
*

tests/Node/DirectoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Tests\Filesystem\Node;
44

5+
use React\EventLoop\Factory;
56
use React\Filesystem\Filesystem;
67
use React\Filesystem\Node\Directory;
78
use React\Filesystem\Node\File;
@@ -81,6 +82,24 @@ public function testCreate()
8182
$this->assertInstanceOf('React\Promise\PromiseInterface', (new Directory($path, Filesystem::createFromAdapter($filesystem)))->create());
8283
}
8384

85+
public function testRename()
86+
{
87+
$pathFrom = 'foo.bar';
88+
$pathTo = 'bar.foo';
89+
$filesystem = $this->mockAdapter();
90+
91+
$filesystem
92+
->expects($this->once())
93+
->method('rename')
94+
->with($pathFrom, $pathTo)
95+
->will($this->returnValue(new FulfilledPromise()))
96+
;
97+
98+
$newDirectory = \Clue\React\Block\await((new Directory($pathFrom, Filesystem::createFromAdapter($filesystem)))->rename($pathTo), Factory::create());
99+
$this->assertInstanceOf('React\Filesystem\Node\DirectoryInterface', $newDirectory);
100+
$this->assertSame($pathTo . NodeInterface::DS, $newDirectory->getPath());
101+
}
102+
84103
public function testRemove()
85104
{
86105
$path = 'foo.bar';

0 commit comments

Comments
 (0)