PhiO: Object-oriented filesystem library for PHP
  • Namespace
  • Class
  • Tree
  • Todo

Namespaces

  • amekusa
    • phio
  • PHP

Classes

  • amekusa\phio\Directory
  • amekusa\phio\File
  • amekusa\phio\FilePool
  • amekusa\phio\Filter
  • amekusa\phio\Perms
  • amekusa\phio\RegexFilter
  • amekusa\phio\RegFile

Exceptions

  • amekusa\phio\ErrorException
  • amekusa\phio\IOException
 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;

/**
 * Directory abstraction
 * @author amekusa <post@amekusa.com>
 */
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;
    }

    /**
     * @param array $Filters
     * @return Directory This
     */
    public function setFilters($Filters) {
        $this->filters = $Filters;
        return $this;
    }

    /**
     * @param string|Filter $Filter
     * @return Directory The object itself
     */
    public function addFilter($Filter) {
        $this->filters[] = $Filter;
        return $this;
    }

    /**
     * Adds a regular expression filter
     *
     * The operation is the same as:
     * ```php
     * $this->addFilter(new RegexFilter($Pattern));
     * ```
     * @param string $Pattern Regular expression
     * @return Directory The object itself
     */
    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);
    }
}
PhiO: Object-oriented filesystem library for PHP API documentation generated by ApiGen