Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/directory_rename.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require dirname(__DIR__) . '/vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$filesystem = React\Filesystem\Filesystem::create($loop);
$dir = $filesystem->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();
11 changes: 11 additions & 0 deletions src/Node/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Node/DirectoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<DirectoryInterface>
*/
public function rename($toDirectoryName);

/**
* List contents of the directory.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Node/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down