From ba4b12ca565acab12ca8cc768fbcab69d56c6ae6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sat, 16 Jul 2011 17:02:08 +0200 Subject: [PATCH] Make use of the new patterns in the file itself --- .../files/lib/util/DirectoryUtil.class.php | 79 ++++++------------- 1 file changed, 26 insertions(+), 53 deletions(-) diff --git a/wcfsetup/install/files/lib/util/DirectoryUtil.class.php b/wcfsetup/install/files/lib/util/DirectoryUtil.class.php index 0255e4d011..3614e7462c 100644 --- a/wcfsetup/install/files/lib/util/DirectoryUtil.class.php +++ b/wcfsetup/install/files/lib/util/DirectoryUtil.class.php @@ -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"); } -- 2.20.1