Unified multiple versions of "remove directory" method

This commit is contained in:
Andy Miller
2014-10-09 18:03:32 -06:00
parent 79268130f4
commit 7f615f19f7
4 changed files with 30 additions and 53 deletions

View File

@@ -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.
*

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Console\Cli;
use Grav\Common\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -151,7 +152,7 @@ class CleanCommand extends Command {
foreach($this->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('<red>dir: </red>' . $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;
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Console\Cli;
use Grav\Common\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -75,7 +76,7 @@ class ClearCacheCommand extends Command {
if (@unlink($file)) $anything = true;
}
elseif (is_dir($file)) {
if (@$this->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;
}
}

View File

@@ -1,6 +1,7 @@
<?php
namespace Grav\Console\Cli;
use Grav\Common\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -149,7 +150,7 @@ class SandboxCommand extends Command
$output->writeln(' <cyan>' . $source . '</cyan> <comment>-></comment> ' . $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);
}
}