Make use of the new patterns in the file itself
authorTim Düsterhus <timwolla@arcor.de>
Sat, 16 Jul 2011 15:02:08 +0000 (17:02 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Sat, 16 Jul 2011 15:06:51 +0000 (17:06 +0200)
wcfsetup/install/files/lib/util/DirectoryUtil.class.php

index 0255e4d01131bfc0bf815990e2dabd9e1e5ac6ac..3614e7462c1ed50cf6b0fa29ac0a20e1942d7093 100644 (file)
@@ -133,34 +133,6 @@ class DirectoryUtil {
                return static::$instances[$recursive][$directory];
        }
 
-       /**
-        * Executes a callback on each file
-        *
-        * @param       callback        $callback       Valid callback
-        * @param       string          $pattern        Apply callback only to files matching the given pattern
-        * @return      boolean                         Returns false if callback is missing or no files available
-        */
-       public function executeCallback($callback, $pattern = '') {
-               if (!is_callable($callback) || empty($this->files)) return false;
-
-               $files = $this->getFiles();
-               // check for pattern only once -> faster
-               if (empty($pattern)) {
-                       foreach ($files as $filename) {
-                               call_user_func($callback, $filename);
-                       }
-               }
-               else {
-                       foreach ($files as $filename) {
-                               if (!preg_match($pattern, $filename)) continue;
-
-                               call_user_func($callback, $filename);
-                       }
-               }
-
-               return true;
-       }
-
        /**
         * Returns a sorted list of files
         *
@@ -254,6 +226,9 @@ class DirectoryUtil {
                                $this->files[$obj->getFilename()] = $obj->getFilename();
                        }
                }
+
+               // add the directory itself
+               $this->filesObj[$this->directory] = $this->directory;
        }
 
        /**
@@ -281,35 +256,34 @@ class DirectoryUtil {
                                $this->filesObj[$obj->getFilename()] = $obj;
                        }
                }
-       }
 
+               // add the directory itself
+               $this->filesObj[$this->directory] = new \SPLFileInfo($this->directory);
+       }
+       
        /**
-        * Recursive remove of directory
+        * Executes a callback on each file
+        *
+        * @param       callback        $callback       Valid callback
+        * @param       string          $pattern        Apply callback only to files matching the given pattern
+        * @return      boolean                         Returns false if callback is invalid
         */
-       public function removeAll() {
-               if (!$this->recursive) throw new SystemException('Removing of directory only works in recursive mode');
+       public function executeCallback($callback, $pattern = '') {
+               if (!is_callable($callback)) return false;
 
-               $files = $this->getFilesObj(self::SORT_NONE);
+               $files = $this->getFilesObj(self::SORT_NONE, $pattern);
                foreach ($files as $filename => $obj) {
-                       if (!is_writable($obj->getPath())) {
-                               throw new SystemException("Could not remove directory: '".$obj->getPath()."' is not writable");
-                       }
-
-                       if ($obj->isDir()) {
-                               rmdir($filename);
-                       }
-                       else if ($obj->isFile()) {
-                               unlink($filename);
-                       }
+                       call_user_func($callback, $filename, $obj);
                }
 
-               rmdir($this->directory);
-               // clear cache
-               $this->filesObj = array();
-               $this->scanFilesObj();
-
-               $this->files = array();
-               $this->scanFiles();
+               return true;
+       }
+       
+       /**
+        * Recursive remove of directory
+        */
+       public function removeAll() {
+               $this->removePattern('');
                
                // destroy cached instance
                unset(static::$instances[$this->recursive][$this->directory]);
@@ -323,10 +297,9 @@ class DirectoryUtil {
        public function removePattern($pattern) {
                if (!$this->recursive) throw new SystemException('Removing of files only works in recursive mode');
 
-               $files = $this->getFilesObj(self::SORT_NONE);
-               foreach ($files as $filename => $obj) {
-                       if (!preg_match($pattern, $filename)) continue;
+               $files = $this->getFilesObj(self::SORT_NONE, $pattern);
 
+               foreach ($files as $filename => $obj) {
                        if (!is_writable($obj->getPath())) {
                                throw new SystemException("Could not remove directory: '".$obj->getPath()."' is not writable");
                        }