mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-10 04:24:14 +09:00
Add Storage::deleteEmptyDirectory() to recursively clear empty directories
This commit is contained in:
parent
91ff3c6323
commit
3dc736817d
2 changed files with 52 additions and 0 deletions
|
|
@ -714,6 +714,35 @@ class Storage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a directory only if it is empty.
|
||||||
|
*
|
||||||
|
* @param string $dirname
|
||||||
|
* @param bool $delete_empty_parents (optional)
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function deleteEmptyDirectory($dirname, $delete_empty_parents = false)
|
||||||
|
{
|
||||||
|
if (!self::isDirectory($dirname) || !self::isEmptyDirectory($dirname))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = @rmdir($dirname);
|
||||||
|
if (!$result)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ($delete_empty_parents)
|
||||||
|
{
|
||||||
|
self::deleteEmptyDirectory(dirname($dirname), true);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current umask.
|
* Get the current umask.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -356,6 +356,29 @@ class StorageTest extends \Codeception\TestCase\Test
|
||||||
$this->assertFalse(Rhymix\Framework\Storage::deleteDirectory($nonexistent));
|
$this->assertFalse(Rhymix\Framework\Storage::deleteDirectory($nonexistent));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDeleteEmptyDirectory()
|
||||||
|
{
|
||||||
|
$sourcedir = \RX_BASEDIR . 'tests/_output/sourcedir';
|
||||||
|
mkdir($sourcedir);
|
||||||
|
file_put_contents($sourcedir . '/foo', 'bar');
|
||||||
|
mkdir($sourcedir . '/subdir');
|
||||||
|
mkdir($sourcedir . '/subdir/subsubdir');
|
||||||
|
mkdir($sourcedir . '/subdir/subsubdir/subsubdir');
|
||||||
|
file_put_contents($sourcedir . '/subdir/subsubdir/subsubdir/foo', 'bar');
|
||||||
|
|
||||||
|
$this->assertFalse(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir/subsubdir'));
|
||||||
|
Rhymix\Framework\Storage::delete($sourcedir . '/subdir/subsubdir/subsubdir/foo');
|
||||||
|
$this->assertTrue(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir/subsubdir'));
|
||||||
|
$this->assertFalse(file_exists($sourcedir . '/subdir/subsubdir/subsubdir'));
|
||||||
|
$this->assertTrue(file_exists($sourcedir . '/subdir/subsubdir'));
|
||||||
|
$this->assertTrue(file_exists($sourcedir . '/foo'));
|
||||||
|
$this->assertTrue(Rhymix\Framework\Storage::deleteEmptyDirectory($sourcedir . '/subdir/subsubdir', true));
|
||||||
|
$this->assertFalse(file_exists($sourcedir . '/subdir/subsubdir'));
|
||||||
|
$this->assertFalse(file_exists($sourcedir . '/subdir'));
|
||||||
|
$this->assertTrue(file_exists($sourcedir));
|
||||||
|
$this->assertTrue(file_exists($sourcedir . '/foo'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testRecommendUmask()
|
public function testRecommendUmask()
|
||||||
{
|
{
|
||||||
$umask = Rhymix\Framework\Storage::recommendUmask();
|
$umask = Rhymix\Framework\Storage::recommendUmask();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue