Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fallback to PHP early if proc_open not allowed.

  • Loading branch information...
commit d26932cc7ef9c22558a01a95840a7bc71ce5808e 1 parent 2719fb7
@beberlei beberlei authored
View
46 src/Composer/Util/Filesystem.php
@@ -12,6 +12,9 @@
namespace Composer\Util;
+use RecursiveDirectoryIterator;
+use RecursiveIteratorIterator;
+
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
@@ -38,12 +41,25 @@ public function remove($file)
return false;
}
+ /**
+ * Recursively remove a directory
+ *
+ * Uses the process component if proc_open is enabled on the PHP
+ * installation.
+ *
+ * @param string $directory
+ * @return bool
+ */
public function removeDirectory($directory)
{
if (!is_dir($directory)) {
return true;
}
+ if (!function_exists('proc_open')) {
+ return $this->removeDirectoryPhp($directory);
+ }
+
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$cmd = sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory)));
} else {
@@ -58,6 +74,36 @@ public function removeDirectory($directory)
return $result && !is_dir($directory);
}
+ /**
+ * Recursively delete directory using PHP iterators.
+ *
+ * Uses a CHILD_FIRST RecursiveIteratorIterator to sort files
+ * before directories, creating a single non-recursive loop
+ * to delete files/directories in the correct order.
+ *
+ * @param string $directory
+ * @return bool
+ */
+ public function removeDirectoryPhp($directory)
+ {
+ $it = new RecursiveDirectoryIterator($directory);
+ $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
+
+ foreach ($ri as $file) {
+ if ($file->getFilename() == "." || $file->getFilename() == "..") {
+ continue;
+ }
+
+ if ($file->isDir()) {
+ rmdir($file->getPathname());
+ } else {
+ unlink($file->getPathname());
+ }
+ }
+
+ return rmdir($directory);
+ }
+
public function ensureDirectoryExists($directory)
{
if (!is_dir($directory)) {
View
15 tests/Composer/Test/Util/FilesystemTest.php
@@ -93,4 +93,19 @@ public function providePathCouples()
array('C:/Temp', 'c:\Temp\test', "test"),
);
}
+
+ /**
+ * @group GH-1339
+ */
+ public function testRemoveDirectoryPhp()
+ {
+ $tmp = sys_get_temp_dir();
+ @mkdir($tmp . "/composer_testdir/level1/level2", 0777, true);
+ file_put_contents($tmp . "/composer_testdir/level1/level2/hello.txt", "hello world");
+
+ $fs = new Filesystem;
+ $this->assertTrue($fs->removeDirectoryPhp($tmp . "/composer_testdir"));
+ $this->assertFalse(file_exists($tmp . "/composer_testdir/level1/level2/hello.txt"));
+ }
}
+
Please sign in to comment.
Something went wrong with that request. Please try again.