1 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php namespace amekusa\phio; main::required;
class Directory extends File implements \IteratorAggregate {
protected
$filters;
public function getIterator() {
return new \ArrayIterator($this->getFiles());
}
public function getFiles() {
if ($jit = !$this->isOpened()) $this->open();
$r = array ();
while (($file = readdir($this->io)) !== false) {
if ($file == '.' || $file == '..') continue;
if (!$this->filter($file)) continue;
$r[] = File::create("{$this->path}/{$file}");
}
if ($jit) $this->close();
return $r;
}
public function setFilters($Filters) {
$this->filters = $Filters;
return $this;
}
public function addFilter($Filter) {
$this->filters[] = $Filter;
return $this;
}
public function addRegexFilter($Pattern) {
return $this->addFilter(new RegexFilter($Pattern));
}
protected function filter($Filename) {
if (!$this->filters) return true;
foreach ($this->filters as $iFilter) {
if ($iFilter->matches($Filename)) return true;
}
return false;
}
protected function _open() {
$this->io = @opendir($this->path);
}
protected function _close() {
return @closedir($this->io);
}
}