From 7f615f19f7d8707d37c9882ddf976a94b43820ba Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 9 Oct 2014 18:03:32 -0600 Subject: [PATCH] Unified multiple versions of "remove directory" method --- system/src/Grav/Common/Utils.php | 24 +++++++++++++++++++ system/src/Grav/Console/Cli/CleanCommand.php | 19 ++------------- .../Grav/Console/Cli/ClearCacheCommand.php | 19 ++------------- .../src/Grav/Console/Cli/SandboxCommand.php | 21 ++-------------- 4 files changed, 30 insertions(+), 53 deletions(-) diff --git a/system/src/Grav/Common/Utils.php b/system/src/Grav/Common/Utils.php index f6e9ba823..ecee73676 100644 --- a/system/src/Grav/Common/Utils.php +++ b/system/src/Grav/Common/Utils.php @@ -40,6 +40,30 @@ abstract class Utils return (object) array_merge((array) $obj1, (array) $obj2); } + /** + * Recurseive remove a directory - DANGEROUS! USE WITH CARE!!!! + * + * @param $dir + * @return bool + */ + public static function rrmdir($dir) { + $files = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::CHILD_FIRST + ); + + /** @var \DirectoryIterator $fileinfo */ + foreach ($files as $fileinfo) { + if ($fileinfo->isDir()) { + if (false === rmdir($fileinfo->getRealPath())) return false; + } else { + if (false === unlink($fileinfo->getRealPath())) return false; + } + } + + return rmdir($dir); + } + /** * Truncate HTML by text length. * diff --git a/system/src/Grav/Console/Cli/CleanCommand.php b/system/src/Grav/Console/Cli/CleanCommand.php index bab37ec33..35232225b 100644 --- a/system/src/Grav/Console/Cli/CleanCommand.php +++ b/system/src/Grav/Console/Cli/CleanCommand.php @@ -1,6 +1,7 @@ paths_to_remove as $path) { $path = ROOT_DIR . $path; - if (is_dir($path) && @$this->rrmdir($path)) { + if (is_dir($path) && @Utils::rrmdir($path)) { $anything = true; $output->writeln('dir: ' . $path); } elseif (is_file($path) && @unlink($path)) { @@ -166,20 +167,4 @@ class CleanCommand extends Command { } } - - // Recursively Delete folder - DANGEROUS! USE WITH CARE!!!! - private function rrmdir($dir) { - if (is_dir($dir)) { - $objects = scandir($dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object); - } - } - reset($objects); - rmdir($dir); - return true; - } - return false; - } } diff --git a/system/src/Grav/Console/Cli/ClearCacheCommand.php b/system/src/Grav/Console/Cli/ClearCacheCommand.php index 8d16d49f0..948b0d6f0 100644 --- a/system/src/Grav/Console/Cli/ClearCacheCommand.php +++ b/system/src/Grav/Console/Cli/ClearCacheCommand.php @@ -1,6 +1,7 @@ rrmdir($file)) $anything = true; + if (@Utils::rrmdir($file)) $anything = true; } } @@ -95,21 +96,5 @@ class ClearCacheCommand extends Command { } } - - // Recursively Delete folder - DANGEROUS! USE WITH CARE!!!! - private function rrmdir($dir) { - if (is_dir($dir)) { - $objects = scandir($dir); - foreach ($objects as $object) { - if ($object != "." && $object != "..") { - if (filetype($dir."/".$object) == "dir") $this->rrmdir($dir."/".$object); else unlink($dir."/".$object); - } - } - reset($objects); - rmdir($dir); - return true; - } - return false; - } } diff --git a/system/src/Grav/Console/Cli/SandboxCommand.php b/system/src/Grav/Console/Cli/SandboxCommand.php index 52d97c2e3..fc92cfc61 100644 --- a/system/src/Grav/Console/Cli/SandboxCommand.php +++ b/system/src/Grav/Console/Cli/SandboxCommand.php @@ -1,6 +1,7 @@ writeln(' ' . $source . ' -> ' . $to); if (is_dir($to)) { - $this->rmdir($to); + @Utils::rrmdir(to); } else { @unlink($to); } @@ -275,22 +276,4 @@ class SandboxCommand extends Command } return true; } - - private function rmdir($dir) { - $files = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), - \RecursiveIteratorIterator::CHILD_FIRST - ); - - /** @var \DirectoryIterator $fileinfo */ - foreach ($files as $fileinfo) { - if ($fileinfo->isDir()) { - if (false === rmdir($fileinfo->getRealPath())) return false; - } else { - if (false === unlink($fileinfo->getRealPath())) return false; - } - } - - return rmdir($dir); - } }