Code
<?php
/**
* Cogent - a content generation "agent"
* An abstract class for generating HTML content by scanning a directory
* containing sub-directories with presentable content. The simple goal
* is to make it quick to throw together code that generates HTML by scanning
* the filesystem in cases where a bunch of content is organized over a flat
* directory structure with meaningful folder names.
*
* Cogent provides generic methods for listing directories under a root
* path, and files matching a filter. The indended use for this class is
* for children to define a public method called "generate" which does the
* actual work of writing HTML based on what it found during the directory scan.
*
* Outline:
* Cogent("<directory name>", <array of filename suffixes>)
* Cogent->scan(); // scans all directories under <directory name> for content matching provided filters (conjunctive)
* Cogent->generate(); // generates HTML output for all images found, organized by album
*
*
* Author: Jonathan Scholz
* 2/3/2010
*/
abstract class Cogent
{
/**
* Property declarations:
*/
protected $rootdir = "/~jscholz/cc.gatech~jscholz6/";
protected $filters;
protected $content_dirs;
protected $recursive = false;
protected $depth = 0;
function __construct($rdir, $suffixes, $recursive) {
$this->rootdir = $rdir;
$this->filters = $suffixes;
$this->recursive = $recursive;
}
/**
* Utility function: a case-insensitive version of in_array
*/
protected static function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
/**
* Produces an array of all directories under the rootdir
*/
protected function scan(){
$this->content_dirs = array();
// Scan the root directory for all subdirs
$roothndl = @opendir($this->rootdir); // @: suppress warning
if ($roothndl == false) {
// print "<!-- Warning: could not find directory \"$this->rootdir\" -->";
// return;
throw new Exception("Could not find directory \"$this->rootdir\"");
} else {
// get each entry, filtered (case-insensitive)
while (false !== ($dir_item = readdir($roothndl))) {
if (is_dir($this->rootdir."/".$dir_item) &&
$dir_item!="." &&
$dir_item!=".." &&
$dir_item!=".svn") {
$this->content_dirs[] = $dir_item;
}
}
closedir($roothndl); // close directory
}
}
/**
* Produces an array of all filenames matching the filter for each directory
*/
protected function list_content($directory) {
$this->content_array = array();
// open appropriate album
$hndl = @opendir($directory);
if ($hndl == false) {
print "<!-- Warning: could not find directory \"$directory\" -->";
exit;
} else {
// get each entry, filtered (case-insensitive)
while (false !== ($dir_item = readdir($hndl))) {
// Get file extension
$ext = substr(strrchr($dir_item, '.'), 1);
if (Cogent::in_arrayi($ext, $this->filters)) {
$content_array[] = $dir_item;
}
}
closedir($hndl); // close directory
if (isset($content_array)) {
return $content_array;
}
return;
}
}
/**
* Top-level executable method: scans from root directory and issues call to generate method
*/
public function run(){
try {
print "<!-- (Content generated by Cogent.php) -->\n";
// Process subdirs
$this->scan();
for ($i=0; $i < count($this->content_dirs); $i++) {
$dir = $this->content_dirs[$i];
// scan topic directories for content (matching provided extensions):
$files = $this->list_content($this->rootdir."/".$dir);
// generate html for content
$this->generate($dir, $files);
if ($this->recursive) {
$cg = clone $this; // shallow copy to new object
$cg->rootdir = $this->rootdir.'/'.$dir; // update rootdir
$cg->depth++;
$cg->run();
}
}
print "<!-- (End Cogent.php content) -->\n";
}
catch (Exception $e) {
echo 'Message: ' .$e->getMessage();
}
}
/**
* Generates HTML for content found in specified directory
*/
abstract protected function generate($dirname, $files);
} // End: class Cogent
?>
About me