Merge pull request #27 from wbb3addons/directoryUtilUse
authorAlexander Ebert <ebert@woltlab.com>
Mon, 25 Jul 2011 14:04:58 +0000 (16:04 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 25 Jul 2011 14:04:58 +0000 (16:04 +0200)
wcfsetup/install/files/lib/data/language/LanguageEditor.class.php
wcfsetup/install/files/lib/system/cache/source/DiskCacheSource.class.php
wcfsetup/install/files/lib/system/template/TemplateEngine.class.php

index 57d4bea1d8b9c955697faba3f48c578aac234e1c..0f7037585c52a0302f6ee79ae2be99900fb4eb63 100644 (file)
@@ -12,6 +12,7 @@ use wcf\system\language\LanguageFactory;
 use wcf\system\package\PackageDependencyHandler;
 use wcf\system\WCF;
 use wcf\util\XML;
+use wcf\util\DirectoryUtil;
 
 /**
  * Provides functions to edit languages.
@@ -284,13 +285,12 @@ class LanguageEditor extends DatabaseObjectEditor {
         * @param       string          $category
         * @param       string          $packageID
         */
-       public static function deleteLanguageFiles($languageID = '*', $category = '*', $packageID = '*') {
-               $files = @glob(WCF_DIR."language/".$packageID."_".$languageID."_".$category.".php");
-               if (is_array($files)) {
-                       foreach ($files as $filename) {
-                               @unlink($filename);
-                       }
-               }
+       public static function deleteLanguageFiles($languageID = '.*', $category = '.*', $packageID = '.*') {
+               if ($category != '.*') $category = preg_quote($category, '~');
+               if ($languageID != '.*') $languageID = intval($languageID);
+               if ($packageID != '.*') $packageID = intval($packageID);
+
+               DirectoryUtil::getInstance(WCF_DIR.'language/')->deletePattern('~'.$packageID.'_'.$languageID.'_'.$category.'\.php$~');
        }
        
        /**
@@ -298,12 +298,9 @@ class LanguageEditor extends DatabaseObjectEditor {
         */
        public function deleteCompiledTemplates() {
                // templates
-               $filenames = glob(WCF_DIR.'templates/compiled/*_'.$this->languageID.'_*.php');
-               if ($filenames) foreach ($filenames as $filename) @unlink($filename);
-               
+               DirectoryUtil::getInstance(WCF_DIR.'templates/compiled/')->deletePattern('~.*_'.$this->languageID.'_.*\.php$~');
                // acp templates
-               $filenames = glob(WCF_DIR.'acp/templates/compiled/*_'.$this->languageID.'_*.php');
-               if ($filenames) foreach ($filenames as $filename) @unlink($filename);
+               DirectoryUtil::getInstance(WCF_DIR.'acp/templates/compiled/')->deletePattern('~.*_'.$this->languageID.'_.*\.php$~');
        }
        
        /**
index 6b9d9f4b5c374cf1fd2fa30ad830f31a8ca0a8bb..aff0441963939b775edc4f58ed6fa518f7eab530 100644 (file)
@@ -4,6 +4,7 @@ use wcf\system\exception\SystemException;
 use wcf\system\io\File;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
+use wcf\util\DirectoryUtil;
 
 /**
  * DiskCacheSource is an implementation of CacheSource that stores the cache as simple files in the file system.
@@ -87,19 +88,15 @@ class DiskCacheSource implements ICacheSource {
         */
        public function clear($directory, $filepattern, $forceDelete = false) {
                $filepattern = str_replace('*', '.*', str_replace('.', '\.', $filepattern));
-               $directory = FileUtil::addTrailingSlash($directory);
-               
-               if (@file_exists($directory)) {
-                       $dirh = opendir($directory);
-                       while ($filename = readdir($dirh)) {
-                               if ($filename != '.' && $filename != '..' && preg_match('%^'.$filepattern.'$%i', $filename)) {
-                                       if ($forceDelete || !@touch($directory.$filename, 1)) {
-                                               @unlink($directory.$filename);
-                                       }
-                               }
-                       }
-                       closedir($dirh);
+               if (substr($directory, -1) != '/') {
+                       $directory .= '/';      
                }
+
+               DirectoryUtil::getInstance($directory)->executeCallback(function ($filename) use ($forceDelete) {
+                       if ($forceDelete || !@touch($filename, 1)) {
+                               @unlink($filename);
+                       }
+               }, '%^'.$directory.$filepattern.'$%i');
        }
        
        /**
@@ -205,15 +202,7 @@ class DiskCacheSource implements ICacheSource {
                while ($row = $statement->fetchArray()) {
                        $packageDir = FileUtil::getRealPath(WCF_DIR.$row['packageDir']);
                        $cacheDir = $packageDir.'cache';
-                       if (file_exists($cacheDir)) {
-                               // get files in cache directory
-                               $files = glob($cacheDir.'/*.php');
-                               if (is_array($files)) {
-                                       foreach ($files as $file) {
-                                               @unlink($file);
-                                       }
-                               }
-                       }
+                       DirectoryUtil::getInstance($cacheDir)->deletePattern('~.*\.php$');
                }
        }
 }
index 2db0782ad48df816146eed11c66fe16bedf97375..f17a10cbd3b0cacd9ad21d3c54bdfa5d6e843030 100644 (file)
@@ -6,6 +6,7 @@ use wcf\system\exception\SystemException;
 use wcf\system\SingletonFactory;
 use wcf\util\HeaderUtil;
 use wcf\util\StringUtil;
+use wcf\util\DirectoryUtil;
 
 /**
  * TemplateEngine loads and displays template.
@@ -528,12 +529,9 @@ class TemplateEngine extends SingletonFactory {
         */
        public static function deleteCompiledTemplates($compileDir = '') {
                if (empty($compileDir)) $compileDir = WCF_DIR.'templates/compiled/';
-               
+
                // delete compiled templates
-               $matches = glob($compileDir . '*_*_*.php');
-               if (is_array($matches)) {
-                       foreach ($matches as $match) @unlink($match);
-               }
+               DirectoryUtil::getInstance($compileDir)->deletePattern('~.*_.*_.*\.php$');
        }
        
        /**