oxfilecollector.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxFileCollector
00010 {
00011 
00017     protected $_sBaseDirectory;
00018 
00024     protected $_aFiles;
00025 
00031     public function setBaseDirectory($sDir)
00032     {
00033         if (!empty($sDir)) {
00034             $this->_sBaseDirectory = $sDir;
00035         }
00036     }
00037 
00043     public function getFiles()
00044     {
00045         return $this->_aFiles;
00046     }
00047 
00056     public function addFile($sFile)
00057     {
00058         if (empty($sFile)) {
00059             throw new Exception('Parameter $sFile is empty!');
00060         }
00061 
00062         if (empty($this->_sBaseDirectory)) {
00063             throw new Exception('Base directory is not set, please use setter setBaseDirectory!');
00064         }
00065 
00066         if (is_file($this->_sBaseDirectory . $sFile)) {
00067 
00068             $this->_aFiles[] = $sFile;
00069 
00070             return true;
00071         }
00072 
00073         return false;
00074     }
00075 
00076 
00087     public function addDirectoryFiles($sFolder, $aExtensions = array(), $blRecursive = false)
00088     {
00089         if (empty($sFolder)) {
00090             throw new Exception('Parameter $sFolder is empty!');
00091         }
00092 
00093         if (empty($this->_sBaseDirectory)) {
00094             throw new Exception('Base directory is not set, please use setter setBaseDirectory!');
00095         }
00096 
00097         $aCurrentList = array();
00098 
00099         if (!is_dir($this->_sBaseDirectory . $sFolder)) {
00100             return;
00101         }
00102 
00103         $handle = opendir($this->_sBaseDirectory . $sFolder);
00104 
00105         while ($sFile = readdir($handle)) {
00106 
00107             if ($sFile != "." && $sFile != "..") {
00108                 if (is_dir($this->_sBaseDirectory . $sFolder . $sFile)) {
00109                     if ($blRecursive) {
00110                         $aResultList = $this->addDirectoryFiles($sFolder . $sFile . '/', $aExtensions, $blRecursive);
00111 
00112                         if (is_array($aResultList)) {
00113                             $aCurrentList = array_merge($aCurrentList, $aResultList);
00114                         }
00115                     }
00116                 } else {
00117                     $sExt = substr(strrchr($sFile, '.'), 1);
00118 
00119                     if ((!empty($aExtensions) && is_array($aExtensions) && in_array($sExt, $aExtensions)) ||
00120                         (empty($aExtensions))
00121                     ) {
00122 
00123                         $this->addFile($sFolder . $sFile);
00124                     }
00125                 }
00126             }
00127         }
00128         closedir($handle);
00129     }
00130 }