Files
media-listing/source/lib/MediaListing/Directory.class.php
Ben Roberts 65c560cdf0 Initial commit
Working directory listing. Hardcoded sidebar template.
2011-01-16 23:00:00 +00:00

60 lines
1.6 KiB
PHP

<?php
class MediaListing_Directory extends MediaListing_FileObject {
protected $files;
protected $parent;
public function __construct($path, $parent, $scan) {
if ( ! is_dir($path)) {
throw new MediaListing_Exception_NotADirectory($path);
}
parent::__construct(parent::TYPE_Directory, $path);
$this->parent = $parent;
if ($scan) {
$this->scan();
}
}
protected function scan() {
try {
$iterator = new MediaListing_Utility_VisibleFilesIterator(new DirectoryIterator($this->path));
foreach ($iterator as /** @var SplFileInfo */ $source) {
$filename = $source->getPathname();
$basename = basename($filename);
$this->files["{$basename}"] = MediaListing_FileObject::create($filename, $this, false);
}
} catch (Exception $e) {
echo $e->getMessage();
}
uasort($this->files, function($a, $b) {
return ($a->type() == $b->type()) ? strcasecmp($a->basename(), $b->basename()) : ($a->type() < $b->type());
}
);
}
public function parent() {
return $this->parent;
}
public function files() {
return $this->files;
}
public function file($basename) {
if ( ! key_exists($basename, $this->files)) {
throw new MediaListing_Exception_FileNotFound($basename);
}
$subdir = $this->files["{$basename}"];
$subdir->scan();
return $subdir;
}
}
?>