浏览代码

Merge remote branch 'stealth35/use_filesystemiterator'

* stealth35/use_filesystemiterator:
  simplify folders deletion
  work with Traversable
  added FilesystemIterator namespace
  tab to spaces
  use FilesytemIterator insteed opendir
Fabien Potencier 14 年之前
父节点
当前提交
3553ed89d4
共有 1 个文件被更改,包括 6 次插入9 次删除
  1. 6 9
      src/Symfony/Bundle/FrameworkBundle/Util/Filesystem.php

+ 6 - 9
src/Symfony/Bundle/FrameworkBundle/Util/Filesystem.php

@@ -91,9 +91,13 @@ class Filesystem
      */
     public function remove($files)
     {
-        if (!is_array($files)) {
+        if (!is_array($files) && !$files instanceof \Traversable) {
             $files = array($files);
         }
+        
+        if($files instanceof \Traversable) {
+            $files = iterator_to_array($files);
+        }
 
         $files = array_reverse($files);
         foreach ($files as $file) {
@@ -102,14 +106,7 @@ class Filesystem
             }
 
             if (is_dir($file) && !is_link($file)) {
-                $fp = opendir($file);
-                while (false !== $item = readdir($fp)) {
-                    if (!in_array($item, array('.', '..'))) {
-                        $this->remove($file.'/'.$item);
-                    }
-                }
-                closedir($fp);
-
+                $this->remove(new \FilesystemIterator($file));
                 rmdir($file);
             } else {
                 unlink($file);