4 * Represents a directory in the filesystem
6 class XHTMLCompiler_Directory
9 /** Location of directory this object represents, w/o trailing slash */
13 * Path to directory you wish to instantiate.
14 * @note if $name is empty, that means .
16 public function __construct($name) {
17 if (empty($name)) $name = '.';
18 $name = str_replace('\\', '/', $name);
19 $l = strlen($name) - 1; // index of last character
20 if ($name[$l] == '/') $name = substr($name, 0, $l);
22 throw new Exception("Directory $name does not exist");
27 /** Returns name of directory without trailing slash */
28 public function getName() {return $this->name
;}
29 /** Returns name of directory with trailing slash */
30 public function getSName() {return $this->name
. '/';}
33 * Recursively scans directory for files
34 * @return Tree of files in the directory, file => size
36 public function getTree() {
37 $dir = new RecursiveDirectoryIterator($this->name
);
39 $dirs = array(array($dir, &$tree));
41 for($i = 0; $i < count($dirs); ++
$i) {
42 $d =& $dirs[$i][0]; // current directory iterator
43 $tier =& $dirs[$i][1]; // current directory tree to write to
44 for($d->rewind(); $d->valid(); $d->next()) {
46 // initialize new directory tree
47 $tier[$d->getFilename()] = array();
48 // file away another directory to process
49 $dirs[] = array($d->getChildren(), &$tier[$d->getFilename()]);
51 // add the file to the directory tree
52 $tier[$d->getFilename()] = $d->getSize();
61 * Scans directory recursively for files with a certain file extension
62 * @param $ext_match Extension with period to look for, cannot have
63 * more than one period within it
64 * @return List of matching files, with fully qualified relative
65 * paths (not tree format)
67 public function scanRecursively($ext_match) {
68 $tree = $this->getTree();
69 $dirs = array(array('', &$tree));
71 for ($i = 0; $i < count($dirs); ++
$i) {
72 $base = $dirs[$i][0]; // base directory prefix
73 $tier = $dirs[$i][1]; // tree node we're reading from
74 foreach ($tier as $name => $contents) {
75 if (is_array($contents)) {
77 $dirs[] = array($base . $name . '/', $contents);
80 $ext = strrchr($name, '.');
81 if ($ext != $ext_match) continue;
82 $ret[] = $this->name
. '/' . $base . $name;
90 * Scans just the current directory for files matching extension
91 * @param $ext_match Extension with period to look for, cannot have
92 * more than one period within it
93 * @return List of matching files
95 public function scanFlat($ext_match) {
96 $files = scandir($this->name ?
$this->name
: '.');
98 foreach ($files as $name) {
99 if (empty($name) ||
$name[0] == '.') continue;
100 $ext = strrchr($name, '.');
101 if ($ext != $ext_match) continue;
102 $ret[] = $this->name
. '/' . $name;
108 * Scans directory for files with matching extension
109 * @param $ext_match File extension to match
110 * @param $recursive Whether or not to search recursively
112 public function scan($ext, $recursive) {
113 if ($recursive) return $this->scanRecursively($ext);
114 else return $this->scanFlat($ext);