1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33:
34: 35: 36: 37: 38:
39: class Eabi_Livehandler_Model_Directory_Collection implements IteratorAggregate, Countable {
40: protected $_path='';
41: protected $_dirName='';
42: protected $_recursionLevel=0;
43: protected $_isRecursion;
44: protected $_filters = array();
45:
46: 47: 48: 49: 50: 51: 52:
53: public function __construct($path,$isRecursion=true,$recursionLevel = 0) {
54: $this->setPath($path);
55: $this->_dirName = $this->lastDir();
56: $this->setRecursion($isRecursion);
57: $this->setRecursionLevel($recursionLevel);
58: if($this->getRecursion() || $this->getRecursionLevel()==0){
59: $this->parseDir();
60: }
61: }
62: 63: 64: 65: 66:
67: public function getDirName() {
68: return $this->_dirName;
69: }
70: 71: 72: 73: 74:
75: public function getRecursion() {
76: return $this->_isRecursion;
77: }
78: 79: 80: 81: 82:
83: public function getRecursionLevel() {
84: return $this->_recursionLevel;
85: }
86: 87: 88: 89: 90:
91: public function getPath() {
92: return $this->_path;
93: }
94: 95: 96: 97: 98: 99:
100: public function setPath($path, $isRecursion='') {
101: if(is_dir($path)){
102: if(isset($this->_path) && $this->_path!=$path && $this->_path!=''){
103: $this->_path = $path;
104: if($isRecursion!='')$this->_isRecursion = $isRecursion;
105: $this->parseDir();
106: } else {
107: $this->_path = $path;
108: }
109: } else {
110: throw new Exception($path. 'is not dir.');
111: }
112: }
113: 114: 115: 116: 117: 118:
119: public function setRecursion($isRecursion) {
120: $this->_isRecursion = $isRecursion;
121: }
122: 123: 124: 125: 126: 127:
128: public function setRecursionLevel($recursionLevel) {
129: $this->_recursionLevel = $recursionLevel;
130: }
131: 132: 133: 134: 135: 136:
137: public function lastDir() {
138: return self::getLastDir($this->getPath());
139: }
140: 141: 142: 143: 144: 145:
146: static public function getLastDir($path) {
147: if($path=='') $path = $this->getPath();
148: $last = strrpos($path, "/");
149: return substr($path,$last+1);
150: }
151: 152: 153: 154: 155: 156:
157: public function addItem($item) {
158: $this->_items[] = $item;
159: }
160: 161: 162: 163: 164:
165: protected function parseDir() {
166: $this->clear();
167: $iter = new RecursiveDirectoryIterator($this->getPath());
168: while ($iter->valid()) {
169: $curr = (string)$iter->getSubPathname();
170: if (!$iter->isDot() && $curr[0]!='.'){
171: $this->addItem(self::getFactory($iter->current(),$this->getRecursion(),$this->getRecursionLevel()));
172: }
173: $iter->next();
174: }
175: }
176:
177: static public function getFactory($path,$is_recursion = true,$recurse_level=0) {
178: if(is_dir($path)){
179: $obj = new Eabi_Livehandler_Model_Directory_Collection($path,$is_recursion,$recurse_level+1);
180: return $obj;
181: } else {
182: return new Eabi_Livehandler_Model_File_Object($path);
183: }
184: }
185:
186:
187: 188: 189: 190: 191: 192:
193: public function useFilter($useFilter) {
194: $this->_renderFilters();
195: $this->walk('useFilter', array($useFilter));
196: }
197: 198: 199: 200: 201:
202: public function filesName() {
203: $files = array();
204: $this->getFilesName($files);
205: return $files;
206:
207: }
208: 209: 210: 211: 212: 213:
214: public function getFilesName(&$files) {
215: $this->walk('getFilesName', array(&$files));
216: }
217: 218: 219: 220: 221:
222: public function filesPaths() {
223: $paths = array();
224: $this->getFilesPaths($paths);
225: return $paths;
226: }
227: 228: 229: 230: 231: 232:
233: public function getFilesPaths(&$paths) {
234: $this->walk('getFilesPaths', array(&$paths));
235: }
236: 237: 238: 239: 240:
241: public function filesObj() {
242: $objs = array();
243: $this->getFilesObj($objs);
244: return $objs;
245: }
246: 247: 248: 249: 250: 251:
252: public function getFilesObj(&$objs) {
253: $this->walk('getFilesObj', array(&$objs));
254: }
255: 256: 257: 258: 259:
260: public function dirsName() {
261: $dir = array();
262: $this->getDirsName($dir);
263: return $dir;
264: }
265: 266: 267: 268: 269: 270:
271: public function getDirsName(&$dirs) {
272: $this->walk('getDirsName', array(&$dirs));
273: if($this->getRecursionLevel()>0)
274: $dirs[] = $this->getDirName();
275: }
276: 277: 278: 279: 280: 281:
282: protected function setFilesFilter($filter) {
283: $this->walk('setFilesFilter', array($filter));
284: }
285: 286: 287: 288: 289:
290: public function __toArray() {
291: $arr = array();
292: $this->toArray($arr);
293: return $arr;
294: }
295: 296: 297: 298: 299:
300: public function toArray($arrRequiredFields = array()) {
301: return array();
302: }
303: 304: 305: 306: 307: 308:
309: public function __toXml($addOpenTag=true,$rootName='Struct') {
310: $xml='';
311: $this->toXml($xml,$addOpenTag,$rootName);
312: return $xml;
313: }
314: 315: 316: 317: 318: 319: 320:
321: public function toXml() {
322: return '';
323: }
324: 325: 326: 327:
328: protected function _renderFilters() {
329: $exts = array();
330: $names = array();
331: $regName = array();
332: foreach ($this->_filters as $filter){
333: switch ($filter['field']){
334: case 'extension':
335: if(is_array($filter['value'])){
336: foreach ($filter['value'] as $value){
337: $exts[] = $value;
338: }
339: } else {
340: $exts[] = $filter['value'];
341: }
342: break;
343: case 'name':
344: if(is_array($filter['value'])){
345: foreach ($filter['value'] as $value){
346: $names[] = $filter['value'];
347: }
348: } else {
349: $names[] = $filter['value'];
350: }
351: break;
352: case 'regName':
353: if(is_array($filter['value'])){
354: foreach ($filter['value'] as $value){
355: $regName[] = $filter['value'];
356: }
357: } else {
358: $regName[] = $filter['value'];
359: }
360: break;
361: }
362: }
363: $filter = array();
364: if(count($exts)>0) {
365: $filter['extension'] = $exts;
366: } else {
367: $filter['extension'] = null;
368: }
369: if(count($names)>0) {
370: $filter['name']=$names;
371: } else {
372: $filter['name']=null;
373: }
374: if(count($regName)>0) {
375:
376: $filter['regName']=$regName;
377: } else {
378: $filter['regName']=null;
379: }
380: $this->setFilesFilter($filter);
381: }
382: 383: 384: 385:
386: public function addFilter($field, $value, $type = 'and') {
387: $filter = array();
388: $filter['field'] = $field;
389: $filter['value'] = $value;
390: $this->_filters[] = $filter;
391: $this->_isFiltersRendered = false;
392: $this->walk('addFilter',array($field, $value));
393: return $this;
394: }
395:
396: public function count() {
397: $this->load();
398: return count($this->_items);
399:
400: }
401:
402: public function getIterator() {
403: $this->load();
404: return new ArrayIterator($this->_items);
405:
406: }
407: 408: 409: 410: 411:
412: public function loadData($printQuery = false, $logQuery = false) {
413: return $this;
414: }
415:
416: 417: 418: 419: 420:
421: public function load($printQuery = false, $logQuery = false) {
422: return $this->loadData($printQuery, $logQuery);
423: }
424: 425: 426: 427: 428:
429: public function clear() {
430: $this->_setIsLoaded(false);
431: $this->_items = array();
432: return $this;
433: }
434: 435: 436: 437: 438:
439: public function isLoaded() {
440: return $this->_isCollectionLoaded;
441: }
442:
443: 444: 445: 446: 447: 448:
449: protected function _setIsLoaded($flag = true) {
450: $this->_isCollectionLoaded = $flag;
451: return $this;
452: }
453: 454: 455: 456: 457: 458: 459: 460: 461: 462:
463: public function walk($callback, array $args=array()) {
464: $results = array();
465: $useItemCallback = is_string($callback) && strpos($callback, '::')===false;
466: foreach ($this->getItems() as $id=>$item) {
467: if ($useItemCallback) {
468: $cb = array($item, $callback);
469: } else {
470: $cb = $callback;
471: array_unshift($args, $item);
472: }
473: $results[$id] = call_user_func_array($cb, $args);
474: }
475: return $results;
476: }
477:
478: 479: 480: 481: 482:
483: public function getItems() {
484: $this->load();
485: return $this->_items;
486: }
487:
488:
489:
490: }
491:
492: