1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00

recursive mkdir, fix #5

This commit is contained in:
Sergey Gelunenko 2016-09-28 13:39:24 +03:00
parent 6eeeeacab2
commit 76701d6918
6 changed files with 79 additions and 13 deletions

View File

@ -203,8 +203,8 @@ class BlockingDriver implements Driver {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function mkdir($path, $mode = 0644) { public function mkdir($path, $mode = 0644, $recursive) {
return new Success((bool) @\mkdir($path, $mode)); return new Success((bool) @\mkdir($path, $mode, $recursive));
} }
/** /**

View File

@ -129,9 +129,10 @@ interface Driver {
* *
* @param string $path * @param string $path
* @param int $mode * @param int $mode
* @param bool $recursive
* @return \Amp\Promise * @return \Amp\Promise
*/ */
public function mkdir($path, $mode = 0644); public function mkdir($path, $mode = 0644, $recursive);
/** /**
* Delete a directory * Delete a directory

View File

@ -356,11 +356,39 @@ class EioDriver implements Driver {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function mkdir($path, $mode = 0644) { public function mkdir($path, $mode = 0644, $recursive) {
\call_user_func($this->incrementor, 1); \call_user_func($this->incrementor, 1);
$promisor = new Deferred; $promisor = new Deferred;
$priority = \EIO_PRI_DEFAULT; $priority = \EIO_PRI_DEFAULT;
if ($recursive) {
$arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path));
$tmpPath = "";
$callback = function() use (
&$callback, &$arrayPath, &$tmpPath, $mode, $priority, $promisor
) {
$tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath);
if (empty($arrayPath)) {
\eio_mkdir($tmpPath, $mode, $priority, [$this, "onGenericResult"], $promisor);
} else {
$this->isdir($tmpPath)->when(function ($error, $result) use (
$callback, $tmpPath, $mode, $priority
) {
if ($result) {
$callback();
} else {
\eio_mkdir($tmpPath, $mode, $priority, $callback);
}
});
}
};
$callback();
} else {
\eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $promisor); \eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $promisor);
}
return $promisor->promise(); return $promisor->promise();
} }

View File

@ -300,13 +300,44 @@ class UvDriver implements Driver {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function mkdir($path, $mode = 0644) { public function mkdir($path, $mode = 0644, $recursive) {
$this->reactor->addRef(); $this->reactor->addRef();
$promisor = new Deferred; $promisor = new Deferred;
if ($recursive) {
$arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path));
$tmpPath = "";
$callback = function() use (
&$callback, &$arrayPath, &$tmpPath, $mode, $promisor
) {
$tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath);
if (empty($arrayPath)) {
\uv_fs_mkdir($this->loop, $tmpPath, $mode, function($fh) use ($promisor) {
$this->reactor->delRef();
$promisor->succeed((bool)$fh);
});
} else {
$this->isdir($tmpPath)->when(function ($error, $result) use (
$callback, $tmpPath, $mode
) {
if ($result) {
$callback();
} else {
\uv_fs_mkdir($this->loop, $tmpPath, $mode, $callback);
}
});
}
};
$callback();
} else {
\uv_fs_mkdir($this->loop, $path, $mode, function($fh) use ($promisor) { \uv_fs_mkdir($this->loop, $path, $mode, function($fh) use ($promisor) {
$this->reactor->delRef(); $this->reactor->delRef();
$promisor->succeed((bool)$fh); $promisor->succeed((bool)$fh);
}); });
}
return $promisor->promise(); return $promisor->promise();
} }

View File

@ -197,10 +197,11 @@ function unlink($path) {
* *
* @param string $path * @param string $path
* @param int $mode * @param int $mode
* @param bool $recursive
* @return \Amp\Promise<null> * @return \Amp\Promise<null>
*/ */
function mkdir($path, $mode = 0644) { function mkdir($path, $mode = 0644, $recursive = false) {
return filesystem()->mkdir($path, $mode); return filesystem()->mkdir($path, $mode, $recursive);
} }
/** /**

View File

@ -261,9 +261,14 @@ abstract class DriverTest extends \PHPUnit_Framework_TestCase {
$dir = "{$fixtureDir}/newdir"; $dir = "{$fixtureDir}/newdir";
yield file\mkdir($dir); yield file\mkdir($dir);
$stat = (yield file\stat($dir)); $this->assertNotNull(yield file\stat($dir));
yield file\rmdir($dir); yield file\rmdir($dir);
$this->assertNull(yield file\stat($dir)); $this->assertNull(yield file\stat($dir));
$dir = "{$fixtureDir}/newdir/with/recursive/creation";
yield file\mkdir($dir, 0744, true);
$this->assertNotNull(yield file\stat($dir));
}); });
} }