Permalink
Browse files

Making Folder::delete() code more PHP5ish

  • Loading branch information...
1 parent a8d0447 commit 16d29a8d0e1b8a20513e7711d3be1f8d84ef7fd0 @ADmad ADmad committed Dec 14, 2011
Showing with 35 additions and 29 deletions.
  1. +16 −6 lib/Cake/Test/Case/Utility/FolderTest.php
  2. +19 −23 lib/Cake/Utility/Folder.php
@@ -723,10 +723,16 @@ public function testDirSize() {
*/
public function testDelete() {
$path = TMP . 'folder_delete_test';
- $Folder = new Folder($path, true);
- touch(TMP . 'folder_delete_test' . DS . 'file1');
- touch(TMP . 'folder_delete_test' . DS . 'file2');
+ mkdir($path);
+ touch($path . DS . 'file_1');
+ mkdir($path . DS . 'level_1_1');
+ touch($path . DS . 'level_1_1' . DS . 'file_1_1');
+ mkdir($path . DS . 'level_1_1' . DS . 'level_2_1');
+ touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1');
+ touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2');
+ mkdir($path . DS . 'level_1_1' . DS . 'level_2_2');
+ $Folder = new Folder($path, true);
$return = $Folder->delete();
$this->assertTrue($return);
@@ -735,9 +741,13 @@ public function testDelete() {
$this->assertEquals($errors, array());
$expected = array(
- $path . ' created',
- $path . DS . 'file1 removed',
- $path . DS . 'file2 removed',
+ $path . DS . 'file_1 removed',
+ $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
+ $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
+ $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
+ $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
+ $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
+ $path . DS . 'level_1_1 removed',
$path . ' removed'
);
$this->assertEquals($expected, $messages);
@@ -554,36 +554,32 @@ public function delete($path = null) {
return null;
}
$path = Folder::slashTerm($path);
- if (is_dir($path) === true) {
- $normalFiles = glob($path . '*');
- $hiddenFiles = glob($path . '\.?*');
-
- $normalFiles = $normalFiles ? $normalFiles : array();
- $hiddenFiles = $hiddenFiles ? $hiddenFiles : array();
-
- $files = array_merge($normalFiles, $hiddenFiles);
- if (is_array($files)) {
- foreach ($files as $file) {
- if (preg_match('/(\.|\.\.)$/', $file)) {
- continue;
+ if (is_dir($path)) {
+ $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
+ foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
+ $filePath = $file->getPathname();
+ if ($file->isFile() || $file->isLink()) {
+ if (@unlink($filePath)) {
+ $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
+ } else {
+ $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
}
- if (is_file($file) === true) {
- if (@unlink($file)) {
- $this->_messages[] = __d('cake_dev', '%s removed', $file);
- } else {
- $this->_errors[] = __d('cake_dev', '%s NOT removed', $file);
- }
- } elseif (is_dir($file) === true && $this->delete($file) === false) {
+ } elseif ($file->isDir()) {
+ if (@rmdir($filePath)) {
+ $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
+ } else {
+ $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
return false;
}
}
}
- $path = substr($path, 0, strlen($path) - 1);
- if (rmdir($path) === false) {
+
+ $path = rtrim($path, DS);
+ if (@rmdir($path)) {
+ $this->_messages[] = __d('cake_dev', '%s removed', $path);
+ } else {
$this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
return false;
- } else {
- $this->_messages[] = __d('cake_dev', '%s removed', $path);
}
}
return true;

0 comments on commit 16d29a8

Please sign in to comment.