00001 <?php
00002 require_once('Varien/Data/Collection.php');
00003 require_once('Varien/Directory/Factory.php');
00004 require_once('Varien/Directory/IFactory.php');
00005
00006 class Varien_Directory_Collection extends Varien_Data_Collection implements IFactory{
00007 protected $_path='';
00008 protected $_dirName='';
00009 protected $_recursionLevel=0;
00010 protected $_isRecursion;
00011 protected $_filters = array();
00012
00013
00014
00015
00016
00017
00018
00019 public function __construct($path,$isRecursion=true,$recursionLevel = 0)
00020 {
00021 parent::__construct();
00022 $this->setPath($path);
00023 $this->_dirName = $this->lastDir();
00024 $this->setRecursion($isRecursion);
00025 $this->setRecursionLevel($recursionLevel);
00026 if($this->getRecursion() || $this->getRecursionLevel()==0){
00027 $this->parseDir();
00028 }
00029 }
00030
00031
00032
00033
00034
00035 public function getDirName()
00036 {
00037 return $this->_dirName;
00038 }
00039
00040
00041
00042
00043
00044 public function getRecursion()
00045 {
00046 return $this->_isRecursion;
00047 }
00048
00049
00050
00051
00052
00053 public function getRecursionLevel()
00054 {
00055 return $this->_recursionLevel;
00056 }
00057
00058
00059
00060
00061
00062 public function getPath()
00063 {
00064 return $this->_path;
00065 }
00066
00067
00068
00069
00070
00071
00072 public function setPath($path, $isRecursion='')
00073 {
00074 if(is_dir($path)){
00075 if(isset($this->_path) && $this->_path!=$path && $this->_path!=''){
00076 $this->_path = $path;
00077 if($isRecursion!='')$this->_isRecursion = $isRecursion;
00078 $this->parseDir();
00079 } else {
00080 $this->_path = $path;
00081 }
00082 } else {
00083 throw new Exception($path. 'is not dir.');
00084 }
00085 }
00086
00087
00088
00089
00090
00091
00092 public function setRecursion($isRecursion)
00093 {
00094 $this->_isRecursion = $isRecursion;
00095 }
00096
00097
00098
00099
00100
00101
00102 public function setRecursionLevel($recursionLevel)
00103 {
00104 $this->_recursionLevel = $recursionLevel;
00105 }
00106
00107
00108
00109
00110
00111
00112 public function lastDir()
00113 {
00114 return self::getLastDir($this->getPath());
00115 }
00116
00117
00118
00119
00120
00121
00122 static public function getLastDir($path){
00123 if($path=='') $path = $this->getPath();
00124 $last = strrpos($path, "/");
00125 return substr($path,$last+1);
00126 }
00127
00128
00129
00130
00131
00132
00133 public function addItem(IFactory $item)
00134 {
00135 $this->_items[] = $item;
00136 }
00137
00138
00139
00140
00141
00142 protected function parseDir()
00143 {
00144 $this->clear();
00145 $iter = new RecursiveDirectoryIterator($this->getPath());
00146 while ($iter->valid()) {
00147 $curr = (string)$iter->getSubPathname();
00148 if (!$iter->isDot() && $curr[0]!='.'){
00149 $this->addItem(Varien_Directory_Factory::getFactory($iter->current(),$this->getRecursion(),$this->getRecursionLevel()));
00150 }
00151 $iter->next();
00152 }
00153 }
00154
00155
00156
00157
00158
00159
00160 public function useFilter($useFilter)
00161 {
00162 $this->_renderFilters();
00163 $this->walk('useFilter', array($useFilter));
00164 }
00165
00166
00167
00168
00169
00170 public function filesName()
00171 {
00172 $files = array();
00173 $this->getFilesName($files);
00174 return $files;
00175
00176 }
00177
00178
00179
00180
00181
00182
00183 public function getFilesName(&$files)
00184 {
00185 $this->walk('getFilesName', array(&$files));
00186 }
00187
00188
00189
00190
00191
00192 public function filesPaths()
00193 {
00194 $paths = array();
00195 $this->getFilesPaths($paths);
00196 return $paths;
00197 }
00198
00199
00200
00201
00202
00203
00204 public function getFilesPaths(&$paths)
00205 {
00206 $this->walk('getFilesPaths', array(&$paths));
00207 }
00208
00209
00210
00211
00212
00213 public function filesObj()
00214 {
00215 $objs = array();
00216 $this->getFilesObj($objs);
00217 return $objs;
00218 }
00219
00220
00221
00222
00223
00224
00225 public function getFilesObj(&$objs)
00226 {
00227 $this->walk('getFilesObj', array(&$objs));
00228 }
00229
00230
00231
00232
00233
00234 public function dirsName()
00235 {
00236 $dir = array();
00237 $this->getDirsName($dir);
00238 return $dir;
00239 }
00240
00241
00242
00243
00244
00245
00246 public function getDirsName(&$dirs)
00247 {
00248 $this->walk('getDirsName', array(&$dirs));
00249 if($this->getRecursionLevel()>0)
00250 $dirs[] = $this->getDirName();
00251 }
00252
00253
00254
00255
00256
00257
00258 protected function setFilesFilter($filter)
00259 {
00260 $this->walk('setFilesFilter', array($filter));
00261 }
00262
00263
00264
00265
00266
00267 public function __toArray()
00268 {
00269 $arr = array();
00270 $this->toArray($arr);
00271 return $arr;
00272 }
00273
00274
00275
00276
00277
00278 public function toArray(&$arr)
00279 {
00280 if($this->getRecursionLevel()>0){
00281 $arr[$this->getDirName()] = array();
00282 $this->walk('toArray', array(&$arr[$this->getDirName()]));
00283 } else {
00284 $this->walk('toArray', array(&$arr));
00285 }
00286 }
00287
00288
00289
00290
00291
00292
00293 public function __toXml($addOpenTag=true,$rootName='Struct')
00294 {
00295 $xml='';
00296 $this->toXml($xml,$addOpenTag,$rootName);
00297 return $xml;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306 public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
00307 {
00308 if($recursionLevel==0 ){
00309 $xml = '';
00310 if($addOpenTag)
00311 $xml.= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
00312 $xml.= '<'.$rootName.'>'."\n";
00313 }
00314 $recursionLevel = $this->getRecursionLevel();
00315 $xml.= str_repeat("\t",$recursionLevel+1)."<$this->_dirName>\n";
00316 $this->walk('toXml', array(&$xml,$recursionLevel,$addOpenTag,$rootName));
00317 $xml.= str_repeat("\t",$recursionLevel+1)."</$this->_dirName>"."\n";
00318 if($recursionLevel==0 ){
00319 $xml.= '</'.$rootName.'>'."\n";
00320 }
00321 }
00322
00323
00324
00325
00326 protected function _renderFilters()
00327 {
00328 $exts = array();
00329 $names = array();
00330 $regName = array();
00331 foreach ($this->_filters as $filter){
00332 switch ($filter['field']){
00333 case 'extension':
00334 if(is_array($filter['value'])){
00335 foreach ($filter['value'] as $value){
00336 $exts[] = $value;
00337 }
00338 } else {
00339 $exts[] = $filter['value'];
00340 }
00341 break;
00342 case 'name':
00343 if(is_array($filter['value'])){
00344 foreach ($filter['value'] as $value){
00345 $names[] = $filter['value'];
00346 }
00347 } else {
00348 $names[] = $filter['value'];
00349 }
00350 break;
00351 case 'regName':
00352 if(is_array($filter['value'])){
00353 foreach ($filter['value'] as $value){
00354 $regName[] = $filter['value'];
00355 }
00356 } else {
00357 $regName[] = $filter['value'];
00358 }
00359 break;
00360 }
00361 }
00362 $filter = array();
00363 if(count($exts)>0) {
00364 $filter['extension'] = $exts;
00365 } else {
00366 $filter['extension'] = null;
00367 }
00368 if(count($names)>0) {
00369 $filter['name']=$names;
00370 } else {
00371 $filter['name']=null;
00372 }
00373 if(count($regName)>0) {
00374
00375 $filter['regName']=$regName;
00376 } else {
00377 $filter['regName']=null;
00378 }
00379 $this->setFilesFilter($filter);
00380 }
00381
00382
00383
00384
00385 public function addFilter($field, $value)
00386 {
00387 $filter = array();
00388 $filter['field'] = $field;
00389 $filter['value'] = $value;
00390 $this->_filters[] = $filter;
00391 $this->_isFiltersRendered = false;
00392 $this->walk('addFilter',array($field, $value));
00393 return $this;
00394 }
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 ?>